2012-11-11 106 views
0

所以我有一個腳本,它允許文件得到一次上傳:麻煩與上傳多個上傳的文件

<input style = "width: 90px;" name="book-image" type="file" id="image" value = "Upload"> 
<input style = "width: 90px;" name="book-preview" type="file" id="book" value = "Upload"> 

這裏是接收腳本:如果圖像存在,它測試如果預覽(PDF)然後運行2個函數來上傳每張圖片和pdf。

if(file_exists($_FILES['book-image']['tmp_name']) || is_uploaded_file($_FILES['book-image']['tmp_name'])) { 
     //If there is a preview or non-free book 
     if((file_exists($_FILES['book-preview']['tmp_name']) || is_uploaded_file($_FILES['book-preview']['tmp_name']))) { 
      //valid - upload image, preview and add book 
      if($image = uploadBookImage() && $pdf = uploadPreviewPdf()) { 
       $newValues['imagefilename'] = $image; 
       $newValues['previewfile'] = $pdf; 
       if($qc->insertBook($newValues)) { 
        $message = "Added Book!"; 
       } else{ 
        $error .= "Couldn't add book"; 
       } 
      } 
     } 
} 

$ qc-> insertBook($ values);將值添加到數據庫。

這裏是影片的上傳功能:

function uploadBookImage() { 
$allowedExts = array("jpg", "jpeg", "gif", "png"); 
$gay = explode(".", $_FILES['book-image']['name']); 
$extension = end($gay); 
$target_path = "../images/books/"; 
$filename = $_FILES["book-image"]["name"]; 
$target_path = $target_path . basename($_FILES['book-image']['name']); 
if ((($_FILES["book-image"]["type"] == "image/gif") 
|| ($_FILES["book-image"]["type"] == "image/jpeg") 
|| ($_FILES["book-image"]["type"] == "image/png") 
|| ($_FILES["book-image"]["type"] == "image/pjpeg")) 
&& ($_FILES["book-image"]["size"] < 2000000) 
&& in_array($extension, $allowedExts)){ 
    if (file_exists($target_path)) { 
     return false; 
    } else { 
     if(move_uploaded_file($_FILES['book-image']['tmp_name'], $target_path)) { 
      echo $filename; 
      return $filename; 
     } 
    } 
} else { 
    return false; 
} 
}//function 

function uploadPreviewPdf() { 
    $allowedExts = array("pdf"); 
    $gay = explode(".", $_FILES['book-preview']['name']); 
    $extension = end($gay); 
    $target_path = '../previews/'; 
    $filename = $_FILES['book-preview']['name']; 
    $target_path = $target_path . basename($_FILES['book-preview']['name']); 
    if((($_FILES['book-preview']['type'] == 'application/pdf')) 
     && ($_FILES['book-preview']['size'] < 6000000) 
     && in_array($extension, $allowedExts)) { 
     if(file_exists($target_path)) { 
      return false; 
     } else { 
     if(move_uploaded_file($_FILES['book-preview']['tmp_name'], $target_path)) { 
      return $filename; 
     } 
    } 
} else { 
    return false; 
} 
} 

的問題:當我同時上傳,它增加了對圖像名稱值1,併爲PDF文件名 - 到數據庫。但是,如果我只上載圖片將它添加圖像名稱到數據庫

上傳圖像時,無功轉儲的輸出:上傳圖像和PDF時,輸出圖像文件名和VAR轉儲 輸出:輸出圖像文件名

我不知道爲什麼。請幫忙。

+0

在這兩種情況下'var_dump($ _ FILES)'是什麼? – hakre

+0

立即檢查。敬請關注。絕望的大聲笑。 – Nick

+0

是的調試可以吸。只需短暫休息一下,用新鮮的眼睛和一步一步的解決問題的方法就可以快速完成。 – hakre

回答

0

我想通了:

它是:

if($image = uploadBookImage() && $pdf = uploadPreviewPdf()) { 
    $newValues['imagefilename'] = $image; 
    $newValues['previewfile'] = $pdf; 
} 

它需要的是

if($image = uploadBookImage()) { 
    $newValues['imagefilename'] = $image; 
    if($pdf = uploadPreviewPdf()){   
     $newValues['previewfile'] = $pdf; 
    } 
} 

不知道爲什麼,雖然。誰能解釋爲什麼?我唯一的猜測是,它沒有時間這麼快完成兩項檢查?