php 文件上传问题

fdasf 2009-06-07 04:39:31
先上代码:

<!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=iso-8859-1" />
<title>Upload an Image</title>
</head>
<body>
<?php # Script 11.1 - upload_image.php

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

// Check for an uploaded file.
if (isset($_FILES['upload'])) {

// Validate the type. Should be jpeg, jpg, or gif.
$allowed = array ('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg');
if (in_array($_FILES['upload']['type'], $allowed)) {

// Move the file over.
if (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/{$_FILES['upload']['name']}")) {

echo '<p>The file has been uploaded!</p>';

} else { // Couldn't move the file over.

echo '<p><font color="red">The file could not be uploaded because: </b>';

// 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;
default:
print 'A system error occurred.';
break;
} // End of switch.

print '</b></font></p>';

} // End of move... IF.

} else { // Invalid type.
echo '<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
unlink ($_FILES['upload']['tmp_name']); // Delete the file.
}

} else { // No file uploaded.
echo '<p><font color="red">Please upload a JPEG or GIF image smaller than 512KB.</font></p>';
}

} // 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 GIF image 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>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>

我在本地测试,问题主要集中在红色那句话上。
比如,我不选择任何文件,直接点击提交,按我的理解,应该是$_FILES['upload']为空,那么页面应该显示Please upload a JPEG or GIF image smaller than 512KB.但实际测试下来却是显示Please upload a JPEG or GIF image.并且提示说unlink()出错,这说明$_FILES['upload']不为空。到底该怎么理解?
另外,我看到网上说表单中设置MAX_FILE_SIZE能限制上传文件的大小,但我故意用了个大文件来试,却没发现任何作用,这又该怎么理解?
...全文
38 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
fdasf 2009-06-07
  • 打赏
  • 举报
回复
第一个问题我已经明白了。现在就问问MAX_FILE_SIZE的问题。我试下来好像有没有它没什么区别呀

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧