file_exists() not working ,but when url of image is given in browser, image is displayed

485 views Asked by At

file_exists is not working!! But when the url ($img in code) is given in the browser, image is displayed. I know file_exists() takes only harddrive path but i could understand, help please ..

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

There are 3 answers

0
som On

file name should be Path to the file or directory not IP Address

'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)) {

}
0
Developer-Sid On

in file_exisits function instead of HOME use physical path. physical path is something like this "/var/www/public_html/"

use phpinfo() function to know the physical path

OR

use

  dirname(__FILE__) . DIRECTORY_SEPARATOR 

PROPERLY to get the physical path dynamically.

2
James On

This code you have:

//HOME is "http://ip address/"

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

if(file_exists($img))
{

Will not work. The function file_exists() is expecting a local directory path. You can use fopen() for a remote path.

$handle = fopen("http://www.example.com/", "r");

if (!$handle)
  {
    //no file
  }
else
  {
    // file exists
  }

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

I believe it works with IP addresses, but be careful as IP addresses are quite often shared.