2013-07-28 58 views
0

。表單提交後,圖像將存儲在服務器上(例如,在我的計算機上)。多個圖片上傳未能我有一個表格,允許用戶上傳多個圖像,與封端的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); 
} 

哪些解決我漸漸的錯誤。我真的很愚蠢的錯誤

+0

我有一種感覺,這可能是因爲你的'file_exists()'調用。您是否嘗試過首先手動創建目錄,然後發送上傳?此外,[這個問題](http://stackoverflow.com/questions/5202306/why-does-image-upload-fail-phps-is-uploaded-file-check)可能會有所幫助 – Bojangles

+0

它會幫助很多如果你在開始和結束清理括號的情況。適當縮進。 –

+0

@Bojangles我試着查看提供的鏈接並將file_exists更改爲實時路徑,但它不起作用。提交表單後,只會上傳第二張圖片。只有當我刷新表單(從而使其上傳兩次),兩個圖像纔會被保存。 –

回答

1

看看你的轉儲,並與你的代碼進行比較。

你的代碼訪問的所有上載領域,即使在3場,4點5。所以,你在所有這些文件中的代碼進行迭代上傳任何文件,例如和一個空字符串使用realpath()

這將觸發錯誤消息「Error.File not uploaded」。這是正確的,因爲文件編號3,4和5沒有上傳,他們留空。

您的代碼必須處理此問題。

而作爲安全功能,請勿使用realpath(),請使用is_uploaded_file()。 PHP有一個在上傳過程中創建的文件名列表,只有這些文件應該被允許在上傳腳本中進行處理。即使攻擊者欺騙PHP來操縱「tmp_name」,您也不想觸摸任何其他文件。

+0

嗨史文謝謝你指出了我,我已經upvoted你的答案。我已經通過添加if(!empty($ _ FILES ['upload'] ['tmp_name'] [$ i]))來糾正它。然而,即使如此,我原來的問題,這是我的形式拒絕上傳/保存通過它傳遞的第一個圖像。 (也就是說,如果我只上傳一張圖片,如果失敗,沒有任何錯誤,並且如果我上傳了多張圖片,則只有第一張圖片不會被上傳,而其他圖片會被成功保存。)但是,如果我要刷新頁面(從而重新提交相同的表格),然後保存第一個圖像。 –

0

這裏「上傳」$_FILES['upload']引用表單中文件字段的名稱。
如果從命名上載文件領域的文件已被上傳

你也許名稱,格式爲不同的第一場上傳你只ckecking?

+0

您好stratton,我已經添加了表單的代​​碼,表單中的上傳字段名爲corectly即「上傳」。 –

0

找出我的錯誤。

我原來寫

if(!is_dir($upload_path)) 
{ 
    $upload_path=mkdir($upload_path,0644,true); 
} 

和我改成了

if(!is_dir($upload_path)) 
{ 
    mkdir($upload_path,0644,true); 
} 

哪些解決我漸漸的錯誤。 我真的很愚蠢的錯誤。