2011-05-01 58 views
0

我在我的上傳文件中有這個錯誤。語法是正確的,問題在哪裏?is_uploaded_file array給出錯誤

錯誤:

is_uploaded_file() expects parameter 1 to be string, array given in C:\Program Files\EasyPHP-5.3.5.0\www\bena_website\admin\variables\upload_img.php on line 34

<h3>Please Choose a File and click Submit</h3> 

     <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
      <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> 
      <input name="userfile[]" type="file" /> 
      <input type="submit" value="Submit" /> 
     </form> 

<?php 
     // check if a file was submitted 
     if(!isset($_FILES['userfile'])) { 
      echo '<p>Please select a file</p>'; 
     } 
     else 
      { 
      print_r($_FILES); 
      try { 
       upload(); 
       // give praise and thanks to the php gods 
       echo '<p>Thank you for submitting</p>'; 
      } 
      catch(Exception $e) { 
       echo $e->getMessage(); 
       echo 'Sorry, could not upload file'; 
      } 
     } 
// the upload function 
function upload(){ 

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { 

    // check the file is less than the maximum file size 
    if($_FILES['userfile']['size'] < $maxsize) 
     { 
    // prepare the image for insertion 
    $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name'])); 
    // $imgData = addslashes($_FILES['userfile']); 

    // get the image info.. 
     $size = getimagesize($_FILES['userfile']['tmp_name']); 

    // put the image in the db... 
     // database connection 
     mysql_connect("localhost", "root", "") OR DIE (mysql_error()); 

     // select the db 
     mysql_select_db ("bena") OR DIE ("Unable to select db".mysql_error()); 

    // our sql query 
    $sql = "INSERT INTO testblob 
      (image_id , image_type ,image, image_size, image_name) 
      VALUES 
      ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')"; 

    // insert the image 
    if(!mysql_query($sql)) { 
     echo 'Unable to upload file'; 
     } 
    } 
} 
else { 
    // if the file is not less than the maximum allowed, print an error 
    echo 
     '<div>File exceeds the Maximum File limit</div> 
     <div>Maximum File limit is '.$maxsize.'</div> 
     <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div> 
     <hr />'; 
    } 
} 
?> 
+2

你能顯示你的HTML表單嗎? – 2011-05-01 15:39:50

+1

我想你有多個文件輸入那裏或1與名稱用戶文件** [] ** – 2011-05-01 15:41:15

+3

我有一種感覺佩卡認爲是一樣的,但我認爲你張貼多個文件在你的窗體?在這種情況下,'$ _FILES ['userfile'] ['tmp_name']'將保存一組路徑。再想一想,你的變量名稱('userfile')看起來像只上傳一個文件。在這種情況下,請'print_r()'$ _FILES'數組並將其添加到您的問題:-) – Bojangles 2011-05-01 15:41:38

回答

7

發射波長爲上傳的start()函數的$ _FILES數組學習的結構,什麼是結構內:

print_r($_FILES); 

由於寫在評論之一,如果你有一個表格如

<form ... method="post" enctype="multipart/form-data"> 
    <input type="file" name="userfile[0]" /> 
    <input type="file" name="userfile[1]" /> 
    ... 
</form> 

<form ... method="post" enctype="multipart/form-data">> 
    <input type="file" name="userfile[]" /> 
    <input type="file" name="userfile[]" /> 
    ... 
</form> 

你需要訪問這個

$_FILES[ 'userfile' ][ 'tmp_name' ] 

像數組:

$_FILES['userfile'][ 'tmp_name' ][ 0 ] 
$_FILES['userfile'][ 'tmp_name' ][ 1 ] 
+0

我希望你得到了正確的觀點...... – KoolKabin 2011-05-01 16:26:46

+0

@KoolKabin:我的回答有問題嗎? – SteAp 2011-05-01 16:30:11

+0

不,根本沒有,我的意思是你得到了正確的觀點... – KoolKabin 2011-05-01 16:37:43