【问题标题】:file_exists() not working ,but when url of image is given in browser, image is displayedfile_exists() 不起作用,但是当在浏览器中给出图像的 url 时,会显示图像
【发布时间】:2013-09-07 19:30:27
【问题描述】:

file_exists 不工作!!但是当浏览器中给出 url(代码中的 $img)时,会显示图像。我知道file_exists() 只需要硬盘路径,但我能理解,请帮忙..

include_once("../inc/inc_constants.php");

include_once("database/db.php");

include_once("includes/global.php");

ini_set('max_execution_time',300);

         $sql="select plan_image_name from mp_new_project_images
                  where project_code in
                  (select project_code from mp_new_project
                    where project_status='Active' ) ";

           $sql_result=mysql_query($sql) or die(mysql_error());



        while($sqlrow=mysql_fetch_array($sql_result))
        {
          //HOME is "http://ip address/"

    $img  = HOME."images/properties/thumbs_400/".$sqlrow['plan_image_name']." ";

    if(file_exists($img))
    {

    $dest =HOME."images/properties/thumbs_400/compress_50/".$sqlrow['plan_image_name']." ";
    $dest1=HOME."images/properties/thumbs_400/compress_20/".$sqlrow['plan_image_name']." ";
    $dest2=HOME."images/properties/thumbs_400/compress_10/".$sqlrow['plan_image_name']." ";

    $size = getimagesize($img);

    switch($size['mime']) {
        case 'image/jpeg':
            $im=imagecreatefromjpeg($img);
            imagejpeg($im,$dest,50);
            imagejpeg($im,$dest1,20);
            imagejpeg($im,$dest2,10);
        break;
        case 'image/png':
             $im = imagecreatefrompng($img);
             imagepng($im,$dest,50);
             imagepng($im,$dest1,20);
             imagepng($im,$dest2,10);
       break;
        case 'image/gif':
            $im = imagecreatefromgif($img);
            imagegif($im,$dest,50);
            imagegif($im,$dest1,20);
            imagegif($im,$dest2,10);
        break;
        default:
            return false;
        break;
        }
    }

}

【问题讨论】:

  • HOME 的价值是什么?
  • 家是ip地址/
  • 如果 HOME 是一个 url,那么它将不起作用.... file_exists() 与服务器文件系统一起使用
  • 如果你给了 hhtp://文件路径名它不会工作..只要给文件系统路径
  • 路径末尾的额外空间也是古玩。

标签: php image url file-exists imagecreatefrompng


【解决方案1】:

文件名应该是Path to the file or directory而不是IP地址

'HOME' constant should be /var/www/html not ('http://url') for example 

$img  = HOME."images/properties/thumbs_400/".$sqlrow['plan_image_name']." ";

if(file_exists($img)) {

}

【讨论】:

    【解决方案2】:

    你有这个代码:

    //HOME is "http://ip address/"
    
    $img  = HOME."images/properties/thumbs_400/".$sqlrow['plan_image_name']." ";
    
    if(file_exists($img))
    {
    

    不会工作。函数 file_exists() 需要一个本地目录路径。您可以将 fopen() 用于远程路径。

    $handle = fopen("http://www.example.com/", "r");
    
    if (!$handle)
      {
        //no file
      }
    else
      {
        // file exists
      }
    

    http://php.net/manual/en/function.fopen.php

    我相信它适用于 IP 地址,但要小心,因为 IP 地址经常被共享。

    【讨论】:

    • 我使用了你的 fopen 方法它的工作谢谢你,但是文件没有在目标文件夹中被压缩。代码中没有错误我认为路径有问题 ($dest,$dest1 ,$dest2).. 帮助表示赞赏!
    • 你能详细解释一下文件没有被压缩的问题吗?你期待什么?
    【解决方案3】:

    在 file_exisits 函数而不是 HOME 中使用物理路径。物理路径类似于“/var/www/public_html/”

    使用phpinfo()函数知道物理路径

    使用

      dirname(__FILE__) . DIRECTORY_SEPARATOR 
    

    适合动态获取物理路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 2021-06-13
      • 2013-01-28
      • 2013-07-28
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多