简体   繁体   English

file_exists() 不工作 codeigniter

[英]file_exists() not working codeigniter

I am working with codeigniter.我正在使用 codeigniter。 I want to display images but if some image is not exist it should show image-not-found-medium.jpg which is dummy image..我想显示图像但如果某些图像不存在它应该显示image-not-found-medium.jpg这是虚拟图像..

below is my code下面是我的代码

<?php
    $image_path_medium = site_url('assets/images-products/medium');
    $image_not_found_medium =  $image_path_medium . "/" . "image-not-found-medium.jpg";
    $image_name_with_path = $image_path_medium . "/" . $home_technology[$key]->product_sku . "-" . "a" . "-medium.jpg";

    if (file_exists($image_name_with_path)) {
        echo $image_name_with_path;
    } else {
        echo $image_not_found_medium;
    }
    ?>

but it always shows $image_not_found_medium i think there is problem with my if condition .但它总是显示$image_not_found_medium我认为我的if condition有问题。 Please help.请帮忙。

<?php
    $image_path_medium = site_url('assets/images-products/medium');
    $image_not_found_medium =  $image_path_medium . "/" . "image-not-found-medium.jpg";
    $image_name_with_path = $image_path_medium . "/" . $home_technology[$key]->product_sku . "-" . "a" . "-medium.jpg";//this is your image url
    $image_file_path=FCPATH.'assets/images-products/medium'. $home_technology[$key]->product_sku . "-" . "a" . "-medium.jpg";//this is your file path

   if (file_exists($image_file_path)) //file_exists of a url returns false.It should be real file path
   {
       echo $image_name_with_path;
   } 
   else 
   {
      echo $image_not_found_medium;
   }
?>

You are using absolute path for file existence which is wrong.您正在使用文件存在的绝对路径,这是错误的。 You have to use real path because the file_exists() function checks whether or not a file or directory exists on the current server.您必须使用真实路径,因为 file_exists() 函数检查当前服务器上是否存在文件或目录。

If your assets folder is placed in root then just use getcwd() - Gets the current working directory as如果您的资产文件夹位于根目录中,则只需使用 getcwd() - 获取当前工作目录为

$image_path_medium = getcwd().'assets/images-products/medium';

Otherwise give the proper path to the assets folder like否则给资产文件夹的正确路径,如

$image_path_medium = getcwd().'application/views/assets/images-products/medium';

Instead of file_exists() prefer is_file() when checking files, as file_exists() returns true on directories.在检查文件时,不是 file_exists() 更喜欢 is_file(),因为 file_exists() 在目录上返回 true。 In addition, you might want to see if getimagesize() returns FALSE to make sure you have an image.此外,您可能想查看 getimagesize() 是否返回 FALSE 以确保您拥有图像。

Use like this.像这样使用。

$g = base_url().'upload/image.jpg';
if(file_exists($g) !== null){//your code..}

This is works for me in CI.这在 CI 中对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM