简体   繁体   中英

Get all img and pdf with php

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.

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));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM