2013-07-23 2 views
1

나는 단지 볼 수없는 간단한 해결책이있을 것이라고 확신합니다.파일 업로드시 PHP 성공

물건을 업로드 할 수있는 양식이 있습니다.

스크립트가 완료되면 나머지 스크립트가 실행되기 전에 Header('Location: admin.php?success')if($_GET['success']) { echo WOOHOO SUCCESS } 유형의 메시지가 사용됩니다.

이 문제는 스크립트의 첫 번째 부분이 실행 되었기 때문에 한 번에 두 개의 파일을 업로드하고 싶지 않다는 것입니다. 그런 다음 부울 값을 사용하여 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 - 예. 양식이 같은 페이지에 있습니다. 그렇지 않은 경우 삶이 더 쉬울 수도 있습니다 :) 사용자의 워크 플로 - 관련 필드를 선택하고 'site'에서 (?) - URL이 성공했는지 확인한 다음 성공한 경우 인쇄 메시지를 표시하고 그렇지 않으면 인쇄 양식 업로드 ] – 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-uploaded-file] : 스트림을 열지 못했습니다 : 사용 권한이 거부되었습니다. – null

관련 문제