douqiaotong8682 2018-06-14 10:35
浏览 44
已采纳

用php获取所有img和pdf

I'm trying to get all images from folder with php and link them with another folder where I have pdf files. The names of both are the same.

My code looks like this:

    $dir_jpg = "../wp-content/uploads/newspaper/jpg/";
    $dir_pdf = "../wp-content/uploads/newspaper/pdf/";

        $images = glob($dir_jpg."*.*");
        $pdfs = glob($dir_pdf."*.*");

        $array = array_combine($images, $pdfs);

        foreach($array as $image => $pdf){
          echo '<ul>';
            echo '<li>';
              echo '<a href="'.$pdf.'" target="_blank"><img src="'.$image.'" height="100" width="100" /></a>';
            echo '</li>';
          echo '</ul>';
        }

I did var_dump to check the array, but it was empty. What could be the reason my code is not working?

Update I store my files in a wordpress folder system, but I didn't upload them through wordpress media, so there is no records about them in database. I want to avoid it, and upload those files through ftp account and list them with a php code.

Update 2 On chat with @Gabor Klement we have got to this

  $wp_root = wp_upload_dir()['baseurl'];
      $dir_jpg = $wp_root."/newspaper/jpg/";
      $dir_pdf = $wp_root."/newspaper/pdf/";

      $images = glob($dir_jpg."*.*");
      $pdfs = array();

      $imgNum = count($images);

      $list = '<ul>';
        for ($i = 0; $i < $imgNum; $i++) {
            $filename = substr(basename($images[$i]), 0, -4);
            $pdfs[$i] = $dir_pdf.$filename.".pdf";

            if (file_exists($dir_pdf.$filename.".pdf")) {
                $list .= '<li><a href="'.$pdfs[$i].'" target="_blank"><img src="'.$images[$i].'" height="100" width="100" /></a></li>';
            }

        }

        $list .= '</ul>';

        echo $list;

but for some reason wordpress is not showing those files. Funy part is that <img src="<?php echo $dir_jpg ?>/july2012.jpg" /> placed before the $list works

Update 3 the only thing that is passing paths to $images is wp_upload_dir()['basedir']; but then the wordpress creates a path like this domain.com/home/user/domains/domain/public_html/wp-content/uploads/newspaper/jpg/december2012.jpg and the image is not found.

  • 写回答

1条回答 默认 最新

  • douxieqiu0651 2018-06-17 15:40
    关注

    I found a solution for my problem. I had to use wp_upload_dir()['basedir'] to pass the path to glob, and then wp_upload_dir()['baseurl']to pass the link to src.

    Maybe it not the best solution, but it works.

      $base_dir = trailingslashit(wp_upload_dir()['basedir']);
      $base_url = wp_upload_dir()['baseurl'];
      $dir_jpg  = '/newspaper/jpg/';
      $dir_pdf  = '/newspaper/pdf/';
    
      $images = glob($base_dir.$dir_jpg.'*.*');
    
      foreach($images as $image) {
       $url = $base_url.$dir_jpg.basename($image);
    
       $filename = substr(basename($image), 0, -4);
    
       $pdfs = $base_url.$dir_pdf.$filename.".pdf";
    
       printf('<a href="'.$pdfs.'" target="_blank" class="newspaper-link col-xl-3"><img src="%s" alt="'.$filename.'.pdf"><div class="newspaper-hover"></div></a>', esc_url($url));
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100