2012-08-02 51 views
0

可能重複:
Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error警告:move_uploaded_file

我回來了再還學習了多個上傳。好吧,我在這裏有一個情況。和我得到2警告:

警告:move_uploaded_file()以期望參數1是串,陣列中的F中給出:\瓦帕\ WWW \ DKI \ 1個\ entry_fotos.php在線路32

警告: move_uploaded_file()以期望參數1是串,陣列中的F中給出:\瓦帕\ WWW \ DKI \ 1個\ entry_fotos.php第45行上

和這是代碼

$ne_photo_images = $_FILES['ne_photo_image']['name']; 
    $fe_photo_images = $_FILES['fe_photo_image']['name']; 
    $tmp_file = $_FILES['ne_photo_image']['tmp_name']; 
    $tmp_file1 = $_FILES['fe_photo_image']['tmp_name']; 

     if(!is_array($ne_photo_images)) { 
     $ne_photo_images = array(); 

    } 

    if(!is_array($tmp_file)) { 
     $tmp_file = array(); 
    } 
     if(! move_uploaded_file($tmp_file, 'image/' . $ne_photo_images)) 

if(!is_array($fe_photo_images)) { 
     $fe_photo_images = array(); 
    } 
if(!is_array($tmp_file1)) { 
     $tmp_file1 = array(); 
    } 
    if(! move_uploaded_file($tmp_file1, 'image/' . $fe_photo_images)) 

,我得到從我的問題反饋此查詢代碼之前

$sql = "INSERT INTO photo(`photo_id`, `ne_photo_image`, `fe_photo_image`, `hop_id`, `title`) VALUES"; 
    for($i = 0, $l = sizeof($titles) ; $i < $l ; $i++) 
    { 
     //adding row datas 
     $sql .= " (null, 
       '".$ne_photo_images [$i]."', 
       '".$fe_photo_images [$i]."', 
       '".$hopid."', 
       '".$titles[$i]."')"; 
     if($i < $l - 1) 
     $sql .= ","; 
    } 

    if(mysql_query($sql)) 
    {   
    } 

,這是我的表單:

<form method="post" enctype="multipart/form-data"> 
    <table border="0"cellpadding="0" cellspacing="0" width= "100%"> 
     <tr> 
      <td>Hop Name :<?echo "$data[hop_name]"?> 
       <input type='hidden' name='photo_hop_id' value='<?echo"$data[hop_id]"?>'> 
      </td> 
     </tr> 
       <table border="0"cellpadding="0" cellspacing="0" width= "100%"> 
    <tr> 
       <td cellpadding="0" cellspacing="0" width= "50%"> 
        Near End Site Name : <?echo "$data[ne_site_name]"?> 
         </br> 
        Near End Site Id : <?echo "$data[ne_site_code]"?> 
       </td> 
       <td cellpadding="0" cellspacing="0" width= "50%"> 
        Far End Site Name : <?echo "$data[fe_site_name]"?> 
         </br> 
        Far End Site Id : <?echo "$data[fe_site_code]"?> 
       </td> 
    </tr> 
    <tr> 
       <td cellpadding="0" cellspacing="0" width= "50%"> 
        <? $pm1= mysql_query("SELECT photo_name FROM photo_name WHERE photo_name_id = 1"); 
         $dpm1 = mysql_fetch_array ($pm1);echo"$dpm1[0]"?> 
        <input type='hidden' name='photo_name_id[]' value='<?echo"$dpm1[0]"?>'> : 
        <input type="file" name="ne_photo_image[]"> 
       </td> 
       <td cellpadding="0" cellspacing="0" width= "50%"> 
        <?echo "$dpm1[0]"?> : <input type="file" name="fe_photo_image[]"> 
       </td> 
    </tr> 

</table> 

    </table> 
    <input type="submit" value="tambah" /> 
</form> 

,我希望這是更容易被發現。非常感謝您的幫助

+0

呼應的var_dump($ ne_photo_images); var_dump($ fe_photo_images);結果在執行之前:array(size = 0) empty array(size = 0) empty – 2012-08-02 08:33:34

+0

echoing var_dump($ ne_photo_images);後續代碼var_dump($ fe_photo_images);結果是:array(size = 1) 0 => string'Lighthouse.jpg'(length = 14) – 2012-08-02 08:36:07

+0

現在我可以完成它,也許這可以爲新手 – 2012-08-02 18:03:25

回答

0

這是造成你的問題......

if(!is_array($ne_photo_images)) { $ne_photo_images = array(); } 
if(!is_array($tmp_file)) { $tmp_file = array(); } 

如果你的$ ne_photo_images不是將其更改爲空數組,但你的陣列中,同樣適用於$ tmp_file。然後你給$ tmp_file作爲參數move_uploaded_file和$ ne_photo_images,它只移動一個文件。不是一個文件

enter code here move_uploaded_file陣列($ tmp_file, '圖像/'。$ ne_photo_images))

你應該做這樣的事情

$ne_photo_images[] = $_FILES['ne_photo_image']['name']; 
$ne_photo_images[] = $_FILES['fe_photo_image']['name']; 
$tmp_file[] = $_FILES['ne_photo_image']['tmp_name']; 
$tmp_file[] = $_FILES['fe_photo_image']['tmp_name']; 

,然後像做

$allGood = true; 
for($i = 0; $i < count($ne_photo_images); $i++) 
{ 
    if (!move_uploaded_file($tmp_file[$i], 'image/' . $ne_photo_images[$i])) 
    { 
    $allGood = false; 
    } 
} 

if (!$allGood) { die("Not all files where moved."); } 

更一般的解決方案

foreach ($_FILES as $file) 
{ 
    if (move_uploaded_files($file['tmp_name'], 'image/' . $file['name'])) 
    { 
    // also insert the file into the database here 

    } 
} 
+0

做一個很好的導師,以及如果做到兩個移動uploaded_file,我應該做兩次? – 2012-08-02 08:45:41

+0

你不必做兩次。循環已經處理了你放入數組的所有文件。你看在所有已上傳的文件並將其移動所有 – bkwint 2012-08-02 08:55:53

+0

我得到這個:不是哪裏感動了所有文件。我真的搞不清 – 2012-08-02 09:00:28

0

此最終代碼

$hopid = $_POST['photo_hop_id']; 
$titles = $_POST['photo_name_id']; 

$ne_photo_images = $_FILES[ne_photo_image][name]; 
$fe_photo_images = $_FILES[fe_photo_image][name]; 

while(list($key,$value) = each($_FILES['ne_photo_image']['name'])) 
    { 
     if(!empty($value)) 
     { 
      $filename = $value; 
       $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line 

       $add = "image/$filename"; 
        //echo $_FILES['images']['type'][$key]; 
      // echo "<br>"; 
       copy($_FILES['ne_photo_image']['tmp_name'][$key], $add); 
       chmod("$add",0777); 


     } 
    } 
     while(list($key,$value) = each($_FILES['fe_photo_image']['name'])) 
    { 
     if(!empty($value)) 
     { 
      $filename = $value; 
       $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line 

       $add = "image/$filename"; 
        //echo $_FILES['images']['type'][$key]; 
      // echo "<br>"; 
       copy($_FILES['fe_photo_image']['tmp_name'][$key], $add); 
       chmod("$add",0777); 


     } 
    } 

       $sql = "INSERT INTO photo(`photo_id`, `ne_photo_image`, `fe_photo_image`, `hop_id`, `title`) VALUES"; 
    for($i = 0, $l = sizeof($titles) ; $i < $l ; $i++) 
    { 
     //adding row datas 
     $sql .= " (null, 
       '".$ne_photo_images [$i]."', 
       '".$fe_photo_images [$i]."', 
       '".$hopid."', 
       '".$titles[$i]."')"; 
     if($i < $l - 1) 
     $sql .= ","; 
    } 

    if(mysql_query($sql)) 
    {   
    } 

    else