我的PDF上传脚本存在2个问题。
我注意到只会上传某些PDF文件而某些PDF文件不会上传。我注意到那些工作的不到几百kbs。不属于MB的那些。有没有办法改变这个?
如果PDF文件上传并且名称中包含空格,则会导致链接断开。有没有办法对它进行编码,以便html正确指导它?尝试通过谷歌搜索,无法找到任何东西。希望你们能够对此有所了解......
这是我的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>";
}
}
?>
PHP代码中有echo "hiii";
。如果有一个PDF文件无法被接受,它就不会回显,所以我假设它与文件大小有关?
这是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>
答案 0 :(得分:1)
对于问题#2,您可以重命名上传的文件,用下划线替换空格,这样您就不会收到断开的链接
$targetfolder = $targetfolder . basename( $_FILES['file']['name']);
将是
$targetfolder = $targetfolder . basename( str_replace(' ', '_', $_FILES['file']['name']) );