。表單提交後,圖像將存儲在服務器上(例如,在我的計算機上)。多個圖片上傳未能我有一個表格,允許用戶上傳多個圖像,與封端的2MB每個圖像僅上傳第一圖像
問題 當上傳多個文件,也就是第一張上傳的圖片總是會失敗在上傳到服務器上,而其他人(例如,第二,第三等)都能夠被成功地保存到我的電腦如果只用ONE圖片測試該表格,則圖片也無法上傳。
我已經得出結論,即通過表單提交的第一個圖像總是會失敗上傳雖然我不明白爲什麼是這樣的話,因爲我能夠成功地呼應了圖像文件的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";
}
錯誤消息 我得到錯誤信息
Error.File沒有上傳
表單代碼:
<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
我試着上傳將成功地顯示。
編輯 步驟所採取/試過 增加了一個,如果(!空)檢查,只有擁有tmp_name的值上傳因此processed.Empty上傳被忽略,從而去除誤差(積分斯文他的熱心幫助)
Error.File not uploaded
然而,即便如此,也就是提交表單的第一個圖像將不被保存(即,如果我上傳一個圖片,如果會失敗,沒有任何錯誤,並且如果我上傳多張圖片,只有第一張圖片不會上傳w )然而,如果我刷新頁面(從而重新提交相同的表單),則第一個圖像將被保存。
這裏的問題是,文件路徑是不是可寫,導致第一個圖像不被保存。但是,令我困惑的是,如果第一個圖像的文件路徑不可寫,爲什麼後來的圖像有一個有效的文件路徑,當他們在相同的代碼上運行?另外,爲什麼要刷新頁面(從而重新提交表單),讓第一個圖像成功保存(這意味着文件路徑現在可寫?)
我的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);
}
哪些解決我漸漸的錯誤。我真的很愚蠢的錯誤
我有一種感覺,這可能是因爲你的'file_exists()'調用。您是否嘗試過首先手動創建目錄,然後發送上傳?此外,[這個問題](http://stackoverflow.com/questions/5202306/why-does-image-upload-fail-phps-is-uploaded-file-check)可能會有所幫助 – Bojangles
它會幫助很多如果你在開始和結束清理括號的情況。適當縮進。 –
@Bojangles我試着查看提供的鏈接並將file_exists更改爲實時路徑,但它不起作用。提交表單後,只會上傳第二張圖片。只有當我刷新表單(從而使其上傳兩次),兩個圖像纔會被保存。 –