PHP文件上传部分上传错误
PHP File Upload Partial Upload Error
我被一个关于文件上传的问题卡住了。我正在尝试将 JPG、JPEG 或 PNG 格式的图像上传到文件夹。我正在阅读一本书并遵循他们的示例,我的代码现在是这样的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload an Image</title>
<style type="text/css" title="text/css" media="all">
.error {
font-weight: bold;
color: #C00;
}
</style>
</head>
<body>
<?php # Script 11.2 - upload_image.php
// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for an uploaded file:
if (isset($_FILES['upload'])) {
// Validate the type. Should be JPEG or PNG.
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload']['type'], $allowed)) {
// Move the file over.
if (move_uploaded_file ($_FILES['upload']['tmp_name'], "upload/{$_FILES['upload']['name']}")) {
echo '<p><em>The file has been uploaded!</em></p>';
} // End of move... IF.
} else { // Invalid type.
echo '<p class="error">Please upload a JPEG or PNG image.</p>';
}
} // End of isset($_FILES['upload']) IF.
// Check for an error:
if ($_FILES['upload']['error'] > 0) {
echo '<p class="error">The file could not be uploaded because: <strong>';
// Print a message based upon the error.
switch ($_FILES['upload']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded.';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write to the disk.';
break;
case 8:
print 'File upload stopped.';
break;
default:
print 'A system error occurred.';
break;
} // End of switch.
print '</strong></p>';
} // End of error IF.
// Delete the file if it still exists:
if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
unlink ($_FILES['upload']['tmp_name']);
}
} // End of the submitted conditional.
?>
<form enctype="multipart/form-data" action="upload_image.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend>
<p><b>File:</b> <input type="file" name="upload" /></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
</form>
</body>
</html>
很奇怪,因为我创建了上传文件夹并设置了相关权限,我可以完美地上传 PNG 文件,但是当我尝试上传 JPG 时,我一直收到 "file was only partially uploaded" 根据 $errors 数组输出。 有问题的文件不大,大约 60-80 Kb。 我是新手,只是想知道上面的代码是否有任何明显的错误会导致此错误,或者是否是服务器?
我还配置了我的 php.ini 并设置了 file_uploads。我是 运行 PHP 5.6,Nginx Ubuntu。
感谢任何帮助。谢谢!
可能是因为 in_array
区分大小写。而是使用 strtolower()
并将您的 mime 类型设为小写。
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/jpg', 'image/x-png', 'image/png');
if (in_array(strtolower($_FILES['upload']['type']), $allowed)) {
...
<?php
if($_FILES['upload']['name'] != ''){
$allowed_ext = array("gif","jpg","jpeg","png","GIF","JPG","JPEG","PNG");
$filename = $_FILES['upload']['name'];
$ext = end(explode(".", $filename));
if(!in_array($ext,$allowed_ext)){echo '<p class="error">Please upload a JPEG or PNG image.</p>';}
else{
$rand1 = time();
$image_name = $rand1.'.'.$ext; //for rename image name
$path = "upload/".$image_name; //for image path
move_uploaded_file($_FILES["upload"]["tmp_name"], $path);
}
}
?>
我被一个关于文件上传的问题卡住了。我正在尝试将 JPG、JPEG 或 PNG 格式的图像上传到文件夹。我正在阅读一本书并遵循他们的示例,我的代码现在是这样的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload an Image</title>
<style type="text/css" title="text/css" media="all">
.error {
font-weight: bold;
color: #C00;
}
</style>
</head>
<body>
<?php # Script 11.2 - upload_image.php
// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for an uploaded file:
if (isset($_FILES['upload'])) {
// Validate the type. Should be JPEG or PNG.
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload']['type'], $allowed)) {
// Move the file over.
if (move_uploaded_file ($_FILES['upload']['tmp_name'], "upload/{$_FILES['upload']['name']}")) {
echo '<p><em>The file has been uploaded!</em></p>';
} // End of move... IF.
} else { // Invalid type.
echo '<p class="error">Please upload a JPEG or PNG image.</p>';
}
} // End of isset($_FILES['upload']) IF.
// Check for an error:
if ($_FILES['upload']['error'] > 0) {
echo '<p class="error">The file could not be uploaded because: <strong>';
// Print a message based upon the error.
switch ($_FILES['upload']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded.';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write to the disk.';
break;
case 8:
print 'File upload stopped.';
break;
default:
print 'A system error occurred.';
break;
} // End of switch.
print '</strong></p>';
} // End of error IF.
// Delete the file if it still exists:
if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
unlink ($_FILES['upload']['tmp_name']);
}
} // End of the submitted conditional.
?>
<form enctype="multipart/form-data" action="upload_image.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend>
<p><b>File:</b> <input type="file" name="upload" /></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
</form>
</body>
</html>
很奇怪,因为我创建了上传文件夹并设置了相关权限,我可以完美地上传 PNG 文件,但是当我尝试上传 JPG 时,我一直收到 "file was only partially uploaded" 根据 $errors 数组输出。 有问题的文件不大,大约 60-80 Kb。 我是新手,只是想知道上面的代码是否有任何明显的错误会导致此错误,或者是否是服务器?
我还配置了我的 php.ini 并设置了 file_uploads。我是 运行 PHP 5.6,Nginx Ubuntu。
感谢任何帮助。谢谢!
可能是因为 in_array
区分大小写。而是使用 strtolower()
并将您的 mime 类型设为小写。
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/jpg', 'image/x-png', 'image/png');
if (in_array(strtolower($_FILES['upload']['type']), $allowed)) {
...
<?php
if($_FILES['upload']['name'] != ''){
$allowed_ext = array("gif","jpg","jpeg","png","GIF","JPG","JPEG","PNG");
$filename = $_FILES['upload']['name'];
$ext = end(explode(".", $filename));
if(!in_array($ext,$allowed_ext)){echo '<p class="error">Please upload a JPEG or PNG image.</p>';}
else{
$rand1 = time();
$image_name = $rand1.'.'.$ext; //for rename image name
$path = "upload/".$image_name; //for image path
move_uploaded_file($_FILES["upload"]["tmp_name"], $path);
}
}
?>