遇到file_exists和is_file的麻烦

时间:2012-05-08 21:29:19

标签: php file-exists

我准备了这段代码,正如你所看到的,我已经注释了我的问题区域

//prepare the image file path that includes the isbn in the file name
    $imageFilePath = '"./data/images/' . $book[0] . '.jpg"';

            // Debugging
    // $var = "==>" . file_exists($imageFilePath) . "<==is it true?";
    // $str .= $var;
    // //tests if file is NOT there, if it isn't the imagepath reflects the default image
    // if (file_exists($imageFilePath) == false){

        // $imageFilePath = '"./data/images/default.jpg"';
    // }

我的图片路径变量就像一个魅力,现在我想使用默认图像,如果该文件不存在。 取消注释if语句后,我的所有图像都将成为默认图像

我尝试过is_file和file_exists,但无济于事。您还可以看到我尝试使用$ var变量捕获结果,==&gt;之间没有空格。和&lt; ==。

我刚刚开始学习编程,一般来说我通常可以在这里或那里使用回声或使用互联网进行调试。我找不到解决方案。

非常感谢所有帮助,并提前感谢

2 个答案:

答案 0 :(得分:1)

你可以做那样的事情

$imageFilePath = "somethingToCheck";
$imageFilePathDefault = "somethingDefault";
$imageFilePath = file_exists($imageFilePath) ? $imageFilePath : $imageFilePathDefault;

对于接受file_exists解释和论证,请查看here

对于第三行代码,我们将其解释为:

if(file_exists($imageFilePath))
 $imageFilePath = $imageFilePath; //confirm that is valid
else
 $imageFilePath = $imageFilePathDefault; //change to default
编辑后

编辑,我建议您从路径中删除双重配额

答案 1 :(得分:1)

删除双引号:

$imageFilePath = './data/images/' . $book[0] . '.jpg';