2016-03-26 22 views
0

Kullanıcıların durumunu göstermek istiyorum yüklendi ve başarısız oldu dosyaları. İşte birden fazla dosya için benim kodSayfayı göster Hata (array) php ile sayfaya geri dön


<form method="post" enctype="multipart/form-data" > 
     <?php 
     //codes to echo error and uploaded file status 
     ?> 
     <input type="file" name="files[]" multiple> 
     <input type="submit" value="Upload Images" name="submit_image"> 
</form> 

karşıya bulunuyor

yüklenen görüntüleri doğrulamak Benim kodudur:

<?php 
if (isset($_POST["submit_image"])) { 
    if (!empty($_FILES["files"]["name"][0])) { 
     $files = $_FILES["files"]; 
     //arrarys to include files uploaded successfully and failed 
     $uploaded = array(); 
     $failed = array(); 
     //allowed extensions of the files 
     $allowed = array("jpg","gif","jpeg","png"); 
     //access tmp_name arrary 
     foreach ($files['name'] as $position => $file_name) { 
      $file_tmp = $files["tmp_name"][$position]; 
      $file_size = $files["size"][$position]; 
      $file_error = $files["error"][$position]; 
      $file_ext = explode(".", $file_name); 
      $file_ext = strtolower(end($file_ext)); 
      if (in_array($file_ext, $allowed)) { 
       if ($file_error === 0) { 
        if ($file_size <= 204800) { 
         //move the valid file to the upload folder 
         $file_name_new = uniqid("", true) . "." . $file_ext; 
         $file_destination = "uploads/" . $file_name_new; 
         if (move_uploaded_file($file_tmp, $file_destination)) { 
          $uploaded[$position] = $file_destination; 

         } else { 
          $failed[$position] = "[{$file_name}] failed to upload."; 
         } 
        } else { 
         $failed [$position] = "[{$file_name}] is larger than 200kb."; 

        } 
       } else { 
        $failed[$position] = "[{$file_name}] failed to upload."; 

       } 
      } else { 
       $failed[$position] = "[{$file_name}] file extension {$file_ext} is not allowed. Only jpg, jpep, png and gif are accetable."; 
      } 
     } 
}?> 

ben yazdırmak istemiyorum şey içeren dizi veya kullanıcıların anlamadığı şeyler. Bunu yapmanın en iyi yolu nedir? Teşekkürler!

+0

[** PHP Elkitabından dosya yükleme işlemleri ** (http://php.net/manual/en/features.file-upload.php) – RiggsFolly

+1

Bir 'print_r ($ _ FILES); kodunuzun üst kısmında, birden fazla dosya yüklenirken '$ _FILES 'dizisinin neye benzediğini anlayabilirsiniz. – RiggsFolly

cevap

1
<?php if($uploaded || $failed){ ?> 
      <ul> 
       <?php foreach($uploaded as $msg){ 
        echo "<li>".$msg."</li>"; 
       }?> 
      </ul> 
      <ul> 
       <?php foreach($failed as $msg){ 
        echo "<li>".$msg."</li>"; 
       }?> 
      </ul> 
<?php } ?> 

Bu, hata iletisini ve başarıyla yüklenen mesajı bir listede gösterecektir. Ayrıca, bu yararlı olabilir. emb HTML in php

İlgili konular