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.