file_exists()不起作用,但是当在浏览器中给出图像的url时,会显示图像

时间:2013-09-04 10:27:19

标签: php image url file-exists imagecreatefrompng

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;
        }
    }

}

3 个答案:

答案 0 :(得分:0)

文件名应为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)) {

}

答案 1 :(得分:0)

在file_exisits函数中而不是HOME使用物理路径。物理路径是这样的“/ var / www / public_html /”

使用phpinfo()函数来了解物理路径

OR

使用

  dirname(__FILE__) . DIRECTORY_SEPARATOR 

正确地动态获取物理路径。

答案 2 :(得分:0)

您拥有以下代码:

//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地址经常被共享。