Having 2 issues with my PDF upload script.
I've noticed only certain PDF files will upload and certain PDF files will not. I've noticed that the ones that work are less than a couple 100 kbs. The ones that don't are in the MB. Is there a way to change this?
If the PDF file uploads and has spaces in its name, it will cause a broken link. Is there a way to encode it so the html will properly direct it? Tried searching through google and can't find anything. Hopefully you guys can shed some light on this...
Here is my PHP:
<?php
include_once 'includes/db_connect.php';
include_once 'includes/psl-config.php';
$title = $_POST['title'];
$id = $_POST['id'];
if ($_FILES["file"]["size"] > 0)
{
$targetfolder = "uploads/";
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
$fileName = basename( $_FILES['file']['name']) ;
$url = $_POST['url'];
$file_type=$_FILES['file']['type'];
echo "hiii";
if ($file_type=="application/pdf" || $file_type=="application/x-pdf")
{
unlink($url);
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
{
if ($insert_stmt = $mysqli->prepare("UPDATE content SET title = ?, fileName = ?, url = ? WHERE id = ?;"))
{
$insert_stmt->bind_param('sssd', $title, $fileName, $targetfolder, $id );
if (! $insert_stmt->execute())
{
echo "Error processing your request";
}
echo "<p>The file is updated</p>";
echo"<p>Return to the <a href='protected_page.php'>content page</a></p>";
}
else
{
echo "<p> Failed to upload to the server</p>
<p> Return back to <a href='edit_content.php'>edit page</a></p>";
}
}
else
{
echo "<p>Problem uploading file</p>";
}
}
else
{
echo "<p>You may only upload PDFs</p>
<p> Return back to <a href='edit_content.php'>edit page</a></p>
";
}
}
else
{
if ($insert_stmt = $mysqli->prepare("UPDATE content SET title = ? WHERE id = ?;"))
{
$insert_stmt->bind_param('sd', $title, $id );
if (! $insert_stmt->execute())
{
header('Location: ../error.php?err=Registration failure: update');
}
echo "<p>The file was updated</p>";
echo"<p>Return to the <a href='protected_page.php'>content page</a></p>";
}
else
{
echo "<p> Failed to upload to the server</p>
<p> Return back to <a href='add_content.php'>edit page</a></p>";
}
}
?>
There is an echo "hiii";
in the PHP code. If there is a PDF file that it will not get accepted, it doesn't echo, so I'm assuming it has to do with file size?
Here is the html:
<?php
include_once 'includes/db_connect.php';
include_once 'includes/psl-config.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Secure Login: Edit</title>
<script type="text/JavaScript" src="js/sha512.js"></script>
<script type="text/JavaScript" src="js/forms.js"></script>
<script type="text/Javascript" src="js/jQuery-1.7.js"></script>
<link rel="stylesheet" href="styles/main.css" />
</head>
<body>
<?php if (login_check($mysqli) == true) : ?>
<?php
$username = htmlentities($_SESSION['username']);
$privileges = mysqli_query($mysqli, "Select privileges From members Where privileges = 'Admin' AND username = '$username' LIMIT 1");
$rowPriv =mysqli_fetch_array($privileges);
if ($rowPriv[0] == "Admin")
{
$id = $_GET['id'];
$editRow = mysqli_query($mysqli, "Select ID, title, url, fileName From content Where ID = $id;");
echo "<h1>Edit Content</h2>";
while ($rowEdit = mysqli_fetch_array($editRow))
{
echo "<form action='edit_file.php' method='post' id='editForm' enctype='multipart/form-data'>
<table cellspacing='10'>
<tr>
<td>
Title:
</td>
<td>
<input name='title' id='title' type='text' value='".$rowEdit['title']."' size='40' >
</td>
</tr>
<tr>
<td>
Upload Different File
</td>
<td>
<input type='checkbox' id='checkbox' checked>
</td>
</tr>
<tr id='file'>
<td>
File:
</td>
<td>
<input type='file' name='file' size='50'/>
</td>
</tr>
<tr>
<td>
<input name='id' type='hidden' value='".$rowEdit['ID']."'>
</td>
</tr>
<tr>
<td>
<input name='url' type='hidden' value='".$rowEdit['url']."'>
</td>
</tr>
</table>";
}
echo "<br />
<input type='button' id='edit' value='Submit' />
</form>
<script>
$('#edit').click(function(){
var title = $('#title').val();
if (title != '')
{
$('#editForm').submit();
return true;
}
else{
alert('Must enter a Title');
return false;
}
});
$('#checkbox').live('change', function(){
if ( $(this).is(':checked') ) {
$('#file').show();
} else {
$('#file').hide();
}
});
</script>";
}
else
{
echo "<p><span>Sorry, you do not have privileges</span></p>";
}
echo"<p>Return to the <a href='protected_page.php'>content page</a>.</p>
<p>Return to the <a href='login.php'>login page</a>.</p>";
?>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="login.php">login</a> with a privileged user.
</p>
<?php endif; ?>
</body>
</html>