我有一个表单,允许用户上传多张图片,每张图片的上限为 2MB。提交表单后,图像将存储在服务器上(例如我的电脑)。
问题 上传多个文件时,上传的第一张图片总是无法上传到服务器,而其他(例如第二张,第三张等)可以成功保存到我的电脑。当只用一张图片,图片也上传失败。
我得出的结论是,通过表单提交的第一张图像将始终无法上传,尽管我无法理解为什么会这样,因为我能够成功地回显图像文件的 tmp_name。
我的代码(摘录):
if(isset($_FILES['upload']['tmp_name']))
{
$numfile=count($_FILES['upload']['tmp_name']);
for($i=0;$i<$numfile;$i++)
{
if(!empty($_FILES['upload']['tmp_name'][$i]))
{
if(is_uploaded_file($_FILES['upload']['tmp_name'][$i]))
{
//Conditionals for uploaded file
$foldername=$_SESSION['UserId'];
$cat=$_POST['category'];
$sub=$_POST['subcat'];
$itemname=$_POST['itemname'];
$allowed_filetypes=array('.jpg','.gif','.bmp','.png','.JPG');
$max_filesize = 2097152; // Maximum filesize in BYTES (currently 2.0MB).
$upload_path = 'C:\Users\Kence\Desktop\UniServer\www\images\\'.$foldername.'\\'.$cat.'\\'.$sub.'\\'.$itemname.'\\'; // The place the files will be uploaded to.
//Checks if Folder for User exists
//If not, A folder for the user is created to store the user's images
if(!is_dir($upload_path))
{
$upload_path=mkdir($upload_path,0644,true);
}
$filename = $_FILES['upload']['name'][$i]; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(in_array($ext,$allowed_filetypes))
{
if(filesize($_FILES['upload']['tmp_name'][$i])<$max_filesize)
{
if(is_writable($upload_path))
{echo"$upload_path <br>";
print_r($_FILES);
if(!move_uploaded_file($_FILES['upload']['tmp_name'][$i],"$upload_path" . $filename))
{
$errormsg="Upload Failed.Error in moving file";
}
}
else
{
$errormsg="Image Upload Failed.";
}
}
else
{
$errormsg="Upload failed.ONly images below 2MB are allowed";
}
}
else
{
$errormsg="Error. Only JPEG,PNG and BMP files are allowed";
}
}
else
{
$errormsg="Error.File not uploaded";
}
}
}
}
else
{
$errormsg="Upload failed";
}
错误消息 我收到错误消息
错误。文件未上传
表格代码:
<body onload="getcategory()">
<form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" name="additem" enctype="multipart/form-data" method="POST">
<table>
<tr>
<td>Select Category: </td><td>
<select name="category" id="category" onchange="getsubcategory(this)">
<option disabled="disabled" value="">Choose a category</option>
</select>
</td>
</tr>
<tr>
<td>Select SubCategory</td>
<td>
<select id="subcat" name="subcat">
<option value=""></option>
</select>
</td>
</tr>
<tr>
<td>Item Name</td>
<td><input type="text" name="itemname" size="30" maxlength="50" required="required"></td>
</tr>
<tr>
<td>Item Price</td>
<td><input type="number" name="itemprice" size="30" min="1" required="required"></td>
</tr>
<tr>
<td>Item Info</td>
<td><textarea name="iteminfo" col="40" rows="10" maxlength="300" required="required"></textarea>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td colspan="2"><input type="SUBMIT" name="Button" value="Submit"></td>
</tr>
<tr>
<td colspan="2"><?PHP if(isset($errormsg)){echo"$errormsg";}?></td>
</tr>
<tr>
<td colspan="3"><font color="#FF0000"></font></td>
</tr>
即使我能够print_r ($_FILES)
并且tmp_name
我尝试上传的所有图像都将成功显示。
EDIT Step Taken/Tried 添加了一个 if(!empty) 检查,以便只处理带有 tmp_name 的上传。因此忽略空上传,从而消除错误(感谢 Sven 的帮助)
Error.File not uploaded
但是,即使这样,提交给表单的第一张图片也不会被保存(即,如果我只上传一张图片,如果会失败,没有任何错误,如果我上传多张图片,只有第一张图片不会被保存上传,而其余的将成功保存。)但是,如果我要刷新页面(从而重新提交相同的表单),则将保存第一张图像。
这里的问题是文件路径不可写,导致第一个图像无法保存。但是让我困惑的是,如果第一个图像的文件路径不可写,为什么后续图像会有一个有效的文件路径,当它们在相同的代码上运行时?另外,为什么刷新页面(并因此重新提交表单)神奇地让第一张图像被成功保存(这意味着文件路径现在是可写的?)
我的 print_r($_FILES); 的结果
Array (
[upload] => Array (
[name] => Array (
[0] => download (1).jpg
[1] => download.jpg
[2] =>
[3] =>
[4] =>
)
[type] => Array (
[0] => image/jpeg
[1] => image/jpeg
[2] =>
[3] =>
[4] =>
)
[tmp_name] => Array (
[0] => C:\Users\Kence\Desktop\UniServer\tmp\php474.tmp
[1] => C:\Users\Kence\Desktop\UniServer\tmp\php475.tmp
[2] =>
[3] =>
[4] =>
)
[error] => Array (
[0] => 0
[1] => 0
[2] => 4
[3] => 4
[4] => 4
)
[size] => Array (
[0] => 6229
[1] => 6984
[2] => 0
[3] => 0
[4] => 0
)
)
)
有人能指出我在哪里做错了吗?过去 3 个小时我一直在尝试,但我仍然不知道我做错了什么,这让我发疯了!
编辑:已解决
想通了我的错误。
我最初写的
if(!is_dir($upload_path))
{
$upload_path=mkdir($upload_path,0644,true);
}
我把它改成了
if(!is_dir($upload_path))
{
mkdir($upload_path,0644,true);
}
这解决了我遇到的错误。我真的犯了愚蠢的错误