Php 使用文件_exists()检查wordpress上载文件夹
我在Wordpress uploads文件夹中创建了一个目录,供最终用户通过ftp批量上传照片。图片编号为1.jpg,2.jpg。。。等等。我已经成功地生成了图像URL,但现在我想测试空URL——也就是说,如果“8.jpg”不存在,则显示主题图像文件夹中的占位符图像 我试图使用文件_exists(),但每次都返回false并始终显示占位符图像。如有任何建议,将不胜感激Php 使用文件_exists()检查wordpress上载文件夹,php,wordpress,file-exists,Php,Wordpress,File Exists,我在Wordpress uploads文件夹中创建了一个目录,供最终用户通过ftp批量上传照片。图片编号为1.jpg,2.jpg。。。等等。我已经成功地生成了图像URL,但现在我想测试空URL——也就是说,如果“8.jpg”不存在,则显示主题图像文件夹中的占位符图像 我试图使用文件_exists(),但每次都返回false并始终显示占位符图像。如有任何建议,将不胜感激 <?php while ( have_posts() ) : the_post(); // create url
<?php while ( have_posts() ) : the_post();
// create url to image in wordpress 'uploads/catalogue_images/$sale' folder
$upload_dir = wp_upload_dir();
$sub_dir = $wp_query->queried_object;
$image = get_field('file_number');
$image_url = $upload_dir['baseurl'] . "/catalogue_images/" . $sub_dir->name . "/" . $image . ".JPG"; ?>
<?php if(file_exists($image_url)){
echo '<img src="' . $image_url . '" alt="" />';
} else {
//placeholder
echo '<img src="' . get_bloginfo("template_url") . '/images/photo_unavailable.jpg" alt="" />';
} ?>
<?php endwhile; ?>
必须使用内部路径检查文件是否存在。 因此,请使用
$upload\u dir['path']
代替$upload\u dir['baseurl']
[路径]-要上载的基本目录和子目录或完整路径
目录
PHP
文件\u主要存在
功能。这一点通过以下方式变得显而易见
幸运的是,我们看到这给了我们几个有用的值:
- “路径”-基本目录和子目录或上载目录的完整路径
- “url”-上传目录的基本url和子目录或绝对url
- “subdir”-启用“上载使用年/月文件夹”选项时的子目录
- “basedir”-没有子目录的路径。
- “baseurl”-不带子目录的URL路径。
- “错误”-设置为false
$image_relative_path = "/catalogue_images/" . $sub_dir->name . "/" . $image . ".JPG";
$image_path = $upload_dir['basedir'] . $image_relative_path;
$image_url = $upload_dir['baseurl'] . $image_relative_path;
然后使用file\u exists($image\u path)
而不是file\u exists($image\u url)
注
与PHP>=5.0.0上的PHP注释一样,您需要的是
http://
协议(这是file\u exists
使用的)。file\u exists
需要一个内部文件路径,而不是http路径。file\u exists
需要服务器上图像的路径,而不是相对于文档根的URL。$image\u url
的可变内容看起来如何?工作正常-非常感谢您清楚地解释它。