2017-04-25 52 views
0

這是我的代碼中的一小部分,我試圖上傳各種文檔(文字+圖像)。但似乎只有圖像上傳得很好。其餘的word文件,excel文件等沒有得到顯示。什麼樣的變化需要,使他們正常可見。(請看看我的截圖)screenshot of my output.注:在截圖中,第三個文件是不可見的無法顯示文檔文件

代碼:

  <meta charset="utf-8"> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
     <link rel="stylesheet" href="<?php echo base_url("css/bootstrap.css"); ?>"> 
     </head> 
     <body> 
     <div class="container"> 

      <p><?php echo $this->session->flashdata('statusMsg'); ?></p> 
       <form enctype="multipart/form-data" action="" method="post"> 
        <div class="form-group"> 
         <label>Choose Files</label> 

        <input type="file" class="form-control" name="userFiles[]" multiple/> 
        <button type="submit" class="btn-success form-control" name="fileSubmit" value="UPLOAD" width="100px">UPLOAD</button> 
        </div> 
        </form> 



      <div class="row"> 


        <div class="thumbnail"> 
      <?php $thumbnails = array_chunk($files, 3); 
      ?> 
        <?php if(!empty($files)) { 
         foreach($thumbnails as $files) { ?> 
         <div class="row"> 
         <?php foreach($files as $file) { ?> 
          <div class="col-md-4"> 
           <div class="thumbnail"> 
            <img src="<?php echo base_url('uploads/files/'.$file['file_name']); ?>" alt="" > 
            <p>Uploaded On <?php echo date("j M Y",strtotime($file['created'])); ?></p> 
           </div> 
          </div> 
         <?php } ?> 
        </div> 
       <?php } ?> 
      <?php } else { ?> 
       <p>Image(s) not found.....</p> 
      <?php } ?> 

          </div> 
       </div></div></div> 
</body> 
</html> 
+1

你說的是那並不是第三文件檢查沒有照片? Excel和Word沒有默認圖像 – Forbs

+0

@Forbs:是的,先生,這是不可見的文字文件。如果它沒有默認圖像,那麼現在我怎樣才能顯示文件上傳! –

+0

Ron Axm查看我的回答更新 –

回答

3

你要添加圖片並將其指定爲一個$ var的文件擴展名

例如

<?php 
$notfound_image = "notfound.png"; //your not file found image 
$excel_image = "excel.png"; //your excel image icon 
$excel_file = "ok.xls"; //your excel file name 

//get file extension 
$parts=pathinfo($excel_file); 
//echo $parts['extension']; //Returns "xls" 
$excel_extension = $parts['extension']; 

//check with if 
//compare file extension 

if ($excel_extension == "xls"){ 
echo '<img src="'.$excel_image.'" />'; 
} else { 
echo '<img src="'.$notfound_image.'" />'; 
} 

?> 

大於本規則的例子,我創造了你可以做同樣的與其他文件擴展名,並在條件

讓我們來看看這對你的作品

<?php $thumbnails = array_chunk($files, 3); 
      ?> 
        <?php if(!empty($files)) { 
         foreach($thumbnails as $files) { ?> 
         <div class="row"> 
         <?php foreach($files as $file) { 
$filename = $file['file_name']; ?> 
          <div class="col-md-4"> 
           <div class="thumbnail"> 
           <?php 

     $parts=pathinfo($filename); 
     $extension = $parts['extension'];  

           switch ($extension) { 
    case 'xls': 
    echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'"> 
    <img src="path_to_excel_icon.png" alt="" > 
    <p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>'; 
    break; 

    case 'docx': 
    echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'"> 
    <img src="path_to_word_icon.png" alt="" > 
    <p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>'; 
    break; 

    case 'jpg': 
    echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'"> 
    <img src="<?php echo base_url('uploads/files/'.$file['file_name']); > 
    <p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>'; 
    break; 
    case 'png': 
    echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'"> 
    <img src="<?php echo base_url('uploads/files/'.$file['file_name']); > 
    <p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>'; 
    break; 

    default: 
    echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'"> 
    <img src="path_to_not_found_icon.png" alt="" > 
    <p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>'; 

} 

?> 

           </div> 
          </div> 
         <?php } ?> 
        </div> 
       <?php } ?> 
      <?php } else { ?> 
       <p>Image(s) not found.....</p> 
      <?php } ?> 
+0

'if($ parts ['extension'] =「xls」)'你在這裏做一個任務,而不是比較。 –

+0

'$ excel_extension = $ parts ['extension']; //檢查是否 //比較文件擴展名 if($ excel_extension =「xls」)' –

+0

Umm .... huh?我不明白你在這裏告訴我什麼。你的答案會失敗。 –