普通网友 2019-04-23 18:33
浏览 56
已采纳

如何为特定文件扩展名类型显示特定图像?

I'm trying to implement a php, mySQL Admin panel/dashboard which shows a small preview image of the file that has been uploaded, if the file is not a jpg, jpeg, png or gif then I would like to display a placeholder image.

So for example if the uploaded file is actually a jpg, the preview column in the table would show the image associated to the file name stored in the db (this I have working) but if it's a pdf then it should show a stock/filler/predefined image of my choosing.

I'm hoping to do this in php but have very limited knowledge in this area with this project being my first large php project.

I have tried to implement this functionality using a js/jquery switch but have had no such luck, Code for the last attempt included below:


 $(document).ready(function() {

 var fileName, fileExtension;

 fileName = event.target.innerHTML;

 fileExtension = fileName.replace(/^.*\./, '');

 switch (fileExtension) {

  case 'png': case 'jpeg': case 'jpg':
      $('#tableImg').attr("src","<?php echo $row["fileName"]; ?>");
      break;

  case 'zip':
      $('#tableImg').attr("src","images/pdf.png");
      break;

  case 'pdf':
      $('#tableImg').attr("src","images/pdf.png");
      break;
    }
});

I am no longer using this method, it was just my last attempt to get it working.

The preview column is currently called in the php file like so;

<img id="tableImg"  class="admin-thumb" src="<?php echo $row["fileName"]; ?>" alt="">

I know that my current methodology will only allow the files with jpg, png and other image related extensions to show but I can't figure out how to swap out the echo $row["filename"]

What I think I'm after, and please correct me if I'm wrong is either an if, else if, else or a php switch. I just have no idea how to make it work the way mentioned earlier.

To recap: I need an assist making a preview column of a table show either the image associated to the file name/URL or if the file type is a zip, pdf or non image file it shows a placeholder image selected by me. I did have a screenshot to share but I don't have 10 rep to do so, yet.

Any help is appreciated and my thanks in advance.

Limur

  • 写回答

1条回答 默认 最新

  • du94414 2019-04-24 15:51
    关注

    SplFileInfo should provide the extention and then just use a switch statement to set the image you want to show.

    $myfile=$row["fileName"];
    $info = new SplFileInfo($myfile);
    $ext = $info->getExtension();
    
    switch ($ext) {
     case 'zip':
        $image='zip.png';
        break;
     case 'pdf':
        $image='pdf.png';
        break;
     default:
        $image=$myFile;
    }
    
    
    <img id="tableImg"  class="admin-thumb" src="<?= $image ?>" alt="">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿