2013-07-23 130 views
1

我確定有一個簡單的解決方案,我無法看到。PHP成功上傳文件

我上傳的東西的一種形式。

當腳本完成時,在腳本的其餘部分運行之前,它使用Header('Location: admin.php?success')if($_GET['success']) { echo WOOHOO SUCCESS }類型的消息。

的問題,這是你要上傳2個文件一次,你不能因爲腳本的第一部分是執行,沒有別的。然後我考慮使用布爾值來設置true或false,並顯示一條消息,但也失敗了。

我希望能夠上傳多個文件相繼並獲得成功的消息,爲每一個。

非常感謝。

相關PHP:

if(isset($_GET['success']) && empty($_GET['success'])){ 
     echo '<h2>File Upload Successful! Whoop!</h2>'; 

    } else{ 

    if(empty($_POST) === false){ 

     //check that a file has been uploaded 
     if(isset($_FILES['myTrainingFile']) && !empty($_FILES['myTrainingFile']['tmp_name'])){ 

      file stuff... 

      if(in_array($fileExt, $blacklist) === true){ 
       $errors[] = "File type not allowed"; 
      } 
     } 

     if(empty($errors) === true){ 
      //run update 
      move file stuff... 
       } 

      } 

      $comments = htmlentities(trim($_POST['comments'])); 
      $category = htmlentities(trim($_POST['category'])); 

      $training->uploadDocument($fileName, $category, $comments); 
      header('Location: admin.php?success'); 
      exit(); 

     } else if (empty($errors) === false) { 
      //header('Location: messageUser.php?msg=' .implode($errors)); 
      echo '<p>' . implode('</p><p>', $errors) . '</p>'; 
     }} 
    } 
    ?> 
+0

表單在同一頁上?什麼是工作流程?選擇文件,點擊按鈕,然後會發生什麼...你能更加明確嗎? – Floris

+0

在您的「成功」消息中,print_r($ _ FILES)並確保它們全部上傳。如果是,請嘗試類似foreach($ _ FILES AS $ k => $ v){echo'File'。$ k。'上傳!
'; } – Dylan

+0

@floris - 是表單在同一頁上。如果不是生命可能來自用戶更容易:) 工作流 - 選擇相關的字段,請單擊 - 流量從「現場」(?) - 檢查網址是成功的,如果是打印的消息,如果沒有chekc表單,流程上傳] – null

回答

0

您通過$_FILES超全局數組需要循環,然後上傳每個文件。

這裏的工作的例子給你一個更好的主意。

<?php 

    $upload_dir= './uploads'; 
    $num_uploads = 2; 
    $max_file_size = 51200; 
    $ini_max = str_replace('M', '', ini_get('upload_max_filesize')); 
    $upload_max = $ini_max * 1024; 
    $msg = 'Please select files for uploading'; 
    $messages = array(); 

    if(isset($_FILES['userfile']['tmp_name'])) 
    { 
     for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++) 
     { 
      if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i])) 
      { 
       $messages[] = 'No file uploaded'; 
      } 
      elseif($_FILES['userfile']['size'][$i] > $upload_max) 
      { 
       $messages[] = "File size exceeds $upload_max php.ini limit"; 
      } 
      elseif($_FILES['userfile']['size'][$i] > $max_file_size) 
      { 
       $messages[] = "File size exceeds $max_file_size limit"; 
      } 
      else 
      { 
       if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i])) 
       { 
        $messages[] = $_FILES['userfile']['name'][$i].' uploaded'; 
       } 
       else 
       { 
        $messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed'; 
       } 
      } 
     } 
    } 
?> 

<html> 
<head> 
<title>Multiple File Upload</title> 
</head> 

<body> 

<h3><?php echo $msg; ?></h3> 
<p> 
<?php 
    if(sizeof($messages) != 0) 
    { 
     foreach($messages as $err) 
     { 
      echo $err.'<br />'; 
     } 
    } 
?> 
</p> 
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post"> 
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" /> 
<?php 
    $num = 0; 
    while($num < $num_uploads) 
    { 
     echo '<div><input name="userfile[]" type="file" /></div>'; 
     $num++; 
    } 
?> 

<input type="submit" value="Upload" /> 
</form> 

</body> 
</html> 

希望這會有所幫助!

+0

的上傳部分這是一次徹底的檢修,看起來好多了!我明天不能測試,但我一定會讓你知道它是如何發展的。謝謝! – null

+0

當然。別客氣! –

+0

我似乎無法得到此與我以前的代碼一起工作。這些文件總是無法上傳。 我也收到和錯誤:警告:move_uploaded_file(/anythingslider.jquery.json)function.move上傳文件]:未能打開流:權限在 – null