【发布时间】:2015-11-09 06:11:06
【问题描述】:
由于问题出在 file_exists() 上,并且该函数似乎应该转到根目录,因此当您确定 file_exists 函数上的文件位置时,您使用基本 url 声明它,因为它只测试文件来自服务器而不是来自 url 的位置:
错误代码
$dir = base_url()."assets/produits/";
foreach ($rows as $row) {
$nom = $row->nShape;
$type = array(".jpeg", ".jpg");
foreach ($type as $ext) {
echo "<br>I'm in the foreach loop <br>";
$file_name = $dir . $nom . $ext;
if (file_exists($file_name)) {
echo "I'm in the file_exists function<br>";
$img_src = $file_name;
} else {
echo "I'm in the else statement <br>";
echo $file_name."\n";
$img_src = $dir . "none.png";
}
}
}
问题是全名在那里,但它总是认为它不存在,我做了一些回声来检查代码是否到达,这是一个屏幕截图:
知道http://localhost/dedax_new/assets/produits/2052.jpeg存在于服务器中。
解决办法:
// set the default to the no find image
$img_src = base_url() . "assets/produits/none.png";
foreach ($rows as $row) {
$nom = $row->nShape;
$type = array(".jpeg", ".jpg");
foreach ($type as $ext) {
$file_name = $nom . $ext;
if (file_exists("./assets/produits/".$file_name)) {
$img_src = base_url() . 'assets/produits/'.$file_name;;
// we found a file that exists 'get out of dodge'
break;
}
}
提前感谢所有贡献者。
【问题讨论】:
-
看起来您正在查找扩展名为 .jpeg 的文件,但没有找到扩展名为 .jpg 的文件。问题是您正在退出 foreach 并将您的路径设置为最后一个测试。
-
@BigScar 我该如何解决这个问题,因为我需要给所有的扩展发短信
-
如果你想测试所有的扩展,也许最好将结果存储在不同的变量或数组中,否则你只会得到现在的最后一个结果。
标签: php