[英]Trouble getting PHP file_exists to work
我正在嘗試獲取縮略圖以鏈接到具有相同名稱的PDF(如果PDF存在),但是不鏈接到任何不存在的PDF(如果不存在)。 這是我的代碼:
<?php
if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) {
$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full') ;
$pdf = substr_replace($full_image_url , 'pdf', strrpos($full_image_url[0] , '.') +1);
$filename = $pdf[0];
if (file_exists($filename)) {
echo '<a href="' . $pdf[0] . '" title="' . the_title_attribute('echo=0') . '" . target="_blank" >';
the_post_thumbnail('Full Size');
echo '</a>';
}
else {
echo "The file $filename exists";
}
}
?>
當前,else語句只是為了證明它是否正在查找文件。 看起來像它看起來一樣The file http://localhost/AWAD/wp-content/uploads/2012/03/+D.pdf exists
。 而且,如果我擺脫了條件,那么將顯示帖子縮略圖,並帶有指向PDF的鏈接。 我就是沒有條件工作。
誰能發現它為什么不起作用?
您應該在FS上將路徑傳遞到file_exists
,現在正在傳遞URL
我很確定file_exists
想要完整的文件路徑,而不是URL。 因此,您可能需要使用WordPress的wp_uploads_dir
函數獲取上載目錄的基本路徑,然后將其余路徑附加到該目錄的末尾,然后將該字符串傳遞給file_exists
。 希望這是有道理的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.