if (file_exists()
を使用してwp-uploadsディレクトリに保存されているWP投稿の画像を取得しようとしていますが、ファイルのパスが認識されません。file_exists()がfalseを返すのはなぜですか?
各投稿ごとに、最大8つの画像があります。各画像は、ファイル名の末尾にa〜gの文字を持ちます(または何もありません)。また、ファイル名の特定の文字を置き換えるためのstr_replaceがあります。
各画像がある場合はそれを表示し、存在しない場合は何も戻さないようにする必要があります。したがって、ポストが最後のb、d、fの画像に関連付けられている場合、その3つだけを表示します。
私は(file_exists())
を使わずにテストし、それぞれの画像を単純なエコーで拾うことができましたが、$img
のパスが認識されないようです。
イムPHPのnoobののビットので、任意の助けをいただければ幸いです...
$uid = get_post_meta (get_the_ID(), 'Unique number', true);
$root ="/wp-content/uploads/2016/Collection/";
$path = str_replace(" ","_",$uid);
$path = str_replace(".","_",$path);
$path = str_replace(":","",$path);
$img = $root.$path.".jpg";
$imga = $root.$path."a.jpg";
$imgb = $root.$path."b.jpg";
$imgc = $root.$path."c.jpg";
$imgd = $root.$path."d.jpg";
$imge = $root.$path."e.jpg";
$imgf = $root.$path."f.jpg";
$imgg = $root.$path."g.jpg";
if (file_exists($img)) { echo "<img src='".$root.$path.".jpg' />"; } else { echo ""; }
if (file_exists($imga)) { echo "<img src='".$root.$path.".jpg' />"; } else { echo ""; }
if (file_exists($imgb)) { echo "<img src='".$root.$path."b.jpg' />"; } else { echo ""; }
if (file_exists($imgc)) { echo "<img src='".$root.$path."c.jpg' />"; } else { echo ""; }
if (file_exists($imgd)) { echo "<img src='".$root.$path."d.jpg' />"; } else { echo ""; }
if (file_exists($imge)) { echo "<img src='".$root.$path."e.jpg' />"; } else { echo ""; }
if (file_exists($imgf)) { echo "<img src='".$root.$path."f.jpg' />"; } else { echo ""; }
if (file_exists($imgg)) { echo "<img src='".$root.$path."g.jpg' />"; } else { echo ""; }`
はい、フルまたは絶対ファイルパスを使用する必要があります。これとうまくいかない場合は、[allow_url_fopen](http://php.net/filesystem.configuration#ini.allow-url-fopen) )があなたのPHP設定で有効になります。 –
'$ _SERVER ['DOCUMENT_ROOT']'私の問題を解決しました - ありがとうございます。 – Aitch