2016-10-14 78 views
0

我使用下面的代碼上傳我的文件:PHP temp_name和move_uploaded_file錯誤

<?php include("./phpincludes/header.inc.php"); 

    $status = ""; 

    if(@$_POST['submit']){ 

     $pic = @$_FILES['pic']['name']; 
     $pic_temp = @$_FILES['pic']['tmp_name']; 
     $pic_type = @$_FILES['pic']['type']; 
     $pic_size = @$_FILES['pic']['size']; 

     $doc1 = @$_FILES['doc1']['name']; 
     $doc1_temp = @$_FILES['doc1']['tmp_name']; 
     $doc1_type = @$_FILES['doc1']['type']; 
     $doc1_size = @$_FILES['doc1']['size']; 

     $doc2 = @$_FILES['doc2']['name']; 
     $doc2_temp = @$_FILES['doc2']['tmp_name']; 
     $doc2_type = @$_FILES['doc2']['type']; 
     $doc2_size = @$_FILES['doc2']['size']; 

     $name = stripcslashes(htmlspecialchars(urldecode($_POST['name']))); 
     $phone1 = stripcslashes(htmlspecialchars(urldecode($_POST['phone1']))); 
     $phone2 = stripcslashes(htmlspecialchars(urldecode($_POST['phone2']))); 
     $address = stripcslashes(htmlspecialchars(urldecode($_POST['address']))); 
     $sec = stripcslashes(htmlspecialchars(urldecode($_POST['sec_amount']))); 

     if($name != ""){ 


      if($pic_size <= 1048576){ 
       if (!$pic_temp) { // if file not chosen 
        echo "ERROR: Please browse for a file before clicking the upload button. ".$pic_temp; 
       } 
       $chars = "abcdefghijklmnopqrstuvrstwxyzABCDEFGHIJKLMNOPQRSTUVWXWZ"; 
       $rand_dir_name = $name." ".substr(str_shuffle($chars), 0, 6); 

       mkdir("uploads/vendors/$rand_dir_name"); 
       move_uploaded_file($doc1_temp, "uploads/vendors/$rand_dir_name/$doc1"); 
       move_uploaded_file($doc2_temp, "uploads/vendors/$rand_dir_name/$doc2"); 
       if(move_uploaded_file($pic_temp, "uploads/vendors/$rand_dir_name/$pic")){ 
        echo "uploads/vendors/$rand_dir_name/$pic"; 
       } else { 
        echo "move_uploaded_file function failed"; 
       } 

      }else{ 
       echo "Image size should be less than 1 MB!"; 
      } 

     }else{ 
      echo "Name is neccassary!"; 
     } 


    } 

?> 

但問題是,它給我的錯誤,而上傳文件「錯誤:請瀏覽文件點擊上傳之前按鈕「。和'move_uploaded_file函數失敗'。我認爲這個問題是在文件前面的@,但是當我刪除它給我錯誤的未定義索引。

我的HTML表單: -

<form method='POST' name='form'> 
       <td><h4>Photo:</h4><input type='file' name='pic' accept="image/gif, image/jpeg, image/png"><h4>Full Name:</h4><input class='tableinput' type='text' placeholder='Name' name='name'></td> 
       <td><h4>Phone 1:</h4><input class='tableinput' type='text' placeholder='Phone' name='phone1'> 
       <h4>Phone 2:</h4><input class='tableinput' type='text' placeholder='Phone' name='phone2'></td> 
       <td> 
        <h4>Document 1:</h4> 
        <input type='file' name='doc1'> 

        <h4>Document 2:</h4> 
        <input type='file' name='doc2'> 
       </td> 
       <td><h4>Service:</h4><input class='tableinput' type='text' placeholder='Service' name='services'> 
       <h4>Address:</h4><input class='tableinput' type='text' placeholder='Address' name='address'></td> 
       <td> <input class='tableinput' type='text' placeholder='Sercurity Amount' name='sec_amount'></td> 
       <td > 
        <input type='submit' id='submit' name='submit' style='display:none;'/> 
        <i class='glyphicon glyphicon-ok-sign tableglyp blue' aria-hidden='true' onclick='submit()'> 
        </i> 
       </td> 
       </form> 

請幫幫忙!

+1

@抑制視圖中的錯誤/警告。似乎在某些情況下可能會取消$ _FILES值。所以請檢查您是否獲得'$ _FILES'值。通過使用'var_dump'或'print_r' – Thamaraiselvam

+0

讓我們來看看你的表單HTML – Rasclatt

+0

@Rasclatt在我的文章中看到我的html表單 – Zicsus

回答

0

爲你的錯誤,主要的原因是:因爲表單標籤是缺少屬性

2)你的檢查事物的狀態邏輯是有缺陷的

1)你的表單上的HTML將無法通過文件。

3)你很手動做你的處理

4)幾乎從來沒有使用@符號謹慎(AS),你要修復錯誤

這是一個非常基本的上傳腳本看一眼。首先,我想首先組織$_FILES數組,這使它對我更具可讀性。最後,請所有的業務邏輯爲功能,這樣你就可以得到他們的邏輯在頁面(你將包括他們,當你想使用它們):

/functions/getFiles.php

function getFiles() 
    { 
     $files = array(); 
     foreach($_FILES as $keyname => $files) { 
      foreach($_FILES[$keyname]['name'] as $key => $value) { 
       if($_FILES[$keyname]['error'][$key] != 0) 
        continue; 

       $file[$keyname][$key]['name']  = $_FILES[$keyname]['name'][$key]; 
       $file[$keyname][$key]['type']  = $_FILES[$keyname]['type'][$key]; 
       $file[$keyname][$key]['tmp_name'] = $_FILES[$keyname]['tmp_name'][$key]; 
       $file[$keyname][$key]['error']  = $_FILES[$keyname]['error'][$key]; 
       $file[$keyname][$key]['size']  = $_FILES[$keyname]['size'][$key]; 
      } 
     } 

     return $file; 
    } 

此功能將開啓$_FILES陣列從:

Array 
(
    [pic] => Array 
     (
      [name] => Array 
       (
        [0] => IMG_7506.JPG.jpeg 
       ) 

      [type] => Array 
       (
        [0] => image/jpeg 
       ) 

      [tmp_name] => Array 
       (
        [0] => /datatmp/phpTNMUP2 
       ) 

      [error] => Array 
       (
        [0] => 0 
       ) 

      [size] => Array 
       (
        [0] => 1003150 
       ) 

     ) 

    [doc] => Array 
     (
      [name] => Array 
       (
        [0] => gI_0_DiscoveryLogovertical1.jpg 
        [1] => gI_0_DiscoveryLogovertical2.jpg 
       ) 

      [type] => Array 
       (
        [0] => image/jpeg 
        [1] => image/jpeg 
       ) 

      [tmp_name] => Array 
       (
        [0] => /datatmp/phpjY1IYG 
        [1] => /datatmp/phpVsX37k 
       ) 

      [error] => Array 
       (
        [0] => 0 
        [1] => 0 
       ) 

      [size] => Array 
       (
        [0] => 43318 
        [1] => 43318 
       ) 

     ) 

) 

到:

Array 
(
    [pic] => Array 
     (
      [0] => Array 
       (
        [name] => IMG_7506.JPG.jpeg 
        [type] => image/jpeg 
        [tmp_name] => /datatmp/phpTNMUP2 
        [error] => 0 
        [size] => 1003150 
       ) 

     ) 

    [doc] => Array 
     (
      [0] => Array 
       (
        [name] => gI_0_DiscoveryLogovertical1.jpg 
        [type] => image/jpeg 
        [tmp_name] => /datatmp/phpjY1IYG 
        [error] => 0 
        [size] => 43318 
       ) 

      [1] => Array 
       (
        [name] => gI_0_DiscoveryLogovertical2.jpg 
        [type] => image/jpeg 
        [tmp_name] => /datatmp/phpVsX37k 
        [error] => 0 
        [size] => 43318 
       ) 

     ) 

) 

現在您創建一個上傳的函數。下面是一個簡單的例子:

/functions/uploadFiles.php

function uploadFiles($files,$dir,&$errors,$max = 1048576) 
    { 
     if(!is_dir($dir)) { 
      if(!mkdir($dir,0755,true)) 
       throw new Exception('Upload folder could not be created.'); 
     } 

     foreach($files as $key => $file) { 
      $name = preg_replace('/[^a-zA-Z0-9\.\-\_]/','',$file['name']); 
      if(empty($name)) { 
       $error[$key][] = 'Name can not be empty.'; 
       continue; 
      } 

      if($file['size'] > $max) { 
       $errors[$key][] = htmlspecialchars($file['name']).' is too large ('.number_format($max/1024,0).' KB max)'; 
       continue; 
      } 

      if(!move_uploaded_file($file['tmp_name'],$dir.$file['name'])) 
       $errors[$key][] = 'File could not be uploaded'; 
     } 
    } 

它更容易有這樣的作爲可重複使用的功能:

/functions/createRandName.php

function createRandName($name) 
    { 
     $chars = "abcdefghijklmnopqrstuvrstwxyzABCDEFGHIJKLMNOPQRSTUVWXWZ"; 
     return $name." ".substr(str_shuffle($chars), 0, 6); 
    } 

使用方法:

# Include the functions here 
require_once(__DIR__.'/functions/createRandName.php'); 
# ...etc. 

# Set your error array 
$errors = array(); 

#check that a post is set 
if(!empty($_POST['submit'])) { 
    # Create the directory 
    $dir = __DIR__.'/'.createRandName(preg_replace('/[^0-9a-zA-Z\s\_\-]/','',$_POST['name'])).'/'; 
    # Because the upload hinges on a folder being created first that doesn't 
    # already exist, we want to stop the process as a whole if the directory 
    # fails to be created 
    try { 
     # Organize the files and loop through them 
     foreach(getFiles() as $input => $files) { 
      # Upload the files 
      uploadFiles($files,$dir,$errors); 
     } 
    } 
    catch (Exception $e) { 
     $errors[] = $e->getMessage(); 
    } 
} 
# Report errors 
if(!empty($errors)) 
    print_r($errors); 
?> 

您的表格標籤需要enctype='multipart/form-data'來上傳文件。即使只有一個文件,您也應該排列所有文件名,如pic[]doc[]。這樣你的文件都可以使用上面的相同上傳機制。

<table> 
    <form method='POST' name='form' enctype='multipart/form-data'> 
     <td> 
      <h4>Photo:</h4> 
      <input type='file' name='pic[]' accept="image/gif, image/jpeg, image/png"> 
      <h4>Full Name:</h4> 
      <input class='tableinput' type='text' placeholder='Name' name='name'> 
     </td> 
     <td> 
      <h4>Phone 1:</h4> 
      <input class='tableinput' type='text' placeholder='Phone' name='phone[1]'> 
      <h4>Phone 2:</h4> 
      <input class='tableinput' type='text' placeholder='Phone' name='phone[2]'></td> 
     <td> 
      <h4>Document 1:</h4> 
      <input type='file' name='doc[]'> 

      <h4>Document 2:</h4> 
      <input type='file' name='doc[]'> 
     </td> 
     <td> 
      <h4>Service:</h4> 
      <input class='tableinput' type='text' placeholder='Service' name='services'> 
      <h4>Address:</h4> 
      <input class='tableinput' type='text' placeholder='Address' name='address'> 
     </td> 
     <td> 
      <input class='tableinput' type='text' placeholder='Sercurity Amount' name='sec_amount'> 
     </td> 
     <td> 
      <input type='submit' id='submit' name='submit' /> 
      <i class='glyphicon glyphicon-ok-sign tableglyp blue' aria-hidden='true' onclick='submit()'></i> 
     </td> 
    </form> 
</table> 

使用這種形式的佈局,你的表格後陣列看起來像這樣:

Array 
(
    [name] => My Name 
    [phone] => Array 
     (
      [1] => 123-123-1233 
      [2] => 321-698-7894 
     ) 

    [services] => Something 
    [address] => 123 My Street 
    [sec_amount] => 123 
    [submit] => Submit 
)