SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with file_exists

    I am experiencing a problem using file_exists with an image path taken from my database.

    When I upload images via my home made CMS, I store them as follows


    Code:
    define ("IMAGES_DIRECTORY","../photos/");
    ...
    $newname =IMAGES_DIRECTORY.$image_name;
    ...
    
    $query = "INSERT INTO photos	(accommodationId,										imageFileName)
    VALUES ('$accommodationId',															
    '$newname')";
    This results is an image being stores something like

    Code:
    ../photos/photo_2053974276.jpg
    The "../" seems to cause problems when I try to check if the file exists on a another page

    i.e

    Code:
    if (file_exists($imageName) {
    However if I go into the database and remove the "../" from the image field value, it works.

    I would like to know why it doesn't work with the "../"

    I was thinking I could make the following change but this wouldn't work due to some pages being in folders that need to reference the images

    Code:
    define ("IMAGES_DIRECTORY","photos/");
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  2. #2
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also tried

    Code:
    define ("IMAGES_DIRECTORY","/photos/");
    which results in the image being stored as

    Code:
    /photos/th_photo_82707602.jpg
    but this doesn't work either

    Therefore I am thinking, is it better to just store the image name in the database and not the path - then when I need to reference an image from a page, add the path?

    Or I could store the full URL path in the database but am thinking that would cause problems if I ever wanted to change the domain name of my site.
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Therefore I am thinking, is it better to just store the image name in the database and not the path
    Spot on I'd say, after all, you're already relying on an application defined path anyway.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool - thanks Anthony! It just means I need to amend the existing photo paths in my database but fortunately there are only about 20 of them.
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Darn that didn't fix the problem! I have changed all my code so it now just stores the actual image name in the database but the file_exists code is still not working.

    It could be due to the structure of the code but as I paid someone to write this particular bit of code, I haven't a clue

    Here is the code in case anyone can spot the problem. Basically this page relates to displaying markers on a google map. I have commented out the previous bit of code relating to the file_exists check.

    Code:
    <body>
    
    <div id="map"></div>
    
    <script type="text/javascript">
    
     var init_lat = <?php echo $results[0]['latitude'] ?>;
     var init_lng = <?php echo $results[0]['longitude'] ?>;
     var init_miles = <?php echo $_GET['radius'] ?>;
    
     var init_zoom = 14;
    
     var prop_ids = new Array();
     var prop_lats = new Array();
     var prop_lngs = new Array();
     var prop_picpaths = new Array();
     var prop_names = new Array();
     var prop_descs = new Array();
     var prop_markers = new Array();
     var prop_points = new Array();
    
    <?php
    
     define ("PHOTOS_DIRECTORY","/photos/");
    
     $counter = 0;
    
      if ($limit_found > 0) {
    
    	foreach ($results as $map_data) {
    
    		$photoFileName = PHOTOS_DIRECTORY.$results[".$counter."]['pic_path'];
    
    		if (file_exists($photoFileName)) {
    
    			$picpath = $photoFileName;
    		} else {
    
    			$picpath = "photos/no-img.gif";
    		}
    
        //if (file_exists(PHOTOS_DIRECTORY.$map_data['pic_path'])) {
        	//$picpath = PHOTOS_DIRECTORY.$map_data['pic_path'];
        //} else {
        	//$picpath = "photos/no-img.gif";
        //}
    
    	if (strlen($map_data['description']) > 100) {
    		$map_data['description'] = trim(substr($map_data['description'],0,100))."...";
    	} 
    
        echo " prop_ids[".$counter."] = ".$map_data['id'].";\n";
        echo " prop_lats[".$counter."] = ".$map_data['latitude'].";\n";
        echo " prop_lngs[".$counter."] = ".$map_data['longitude'].";\n";
        echo " prop_names[".$counter."] = \"".addslashes($map_data['businessName'])."\";\n";
        echo " prop_descs[".$counter."] = \"".addslashes($map_data['description'])."\";\n";
        echo " prop_picpaths[".$counter."] = \"".$picpath."\";\n";
        echo " prop_points[".$counter."] = new GLatLng(".$map_data['latitude'].",".$map_data['longitude'].");\n";
        echo " prop_markers[".$counter."] = \"".$map_data['category']."\";\n\n";
    
    
        $counter++;
    
       }
      }
    
    ?>
    </script>
    
    </body>
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    echo out the filepath which you construct to see if it resembles the expected value.
    PHP Code:
    $photoFileName PHOTOS_DIRECTORY.$results[".$counter."]['pic_path'];
    echo 
    $photoFileName
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anthony

    I did try to echo out the value but it doesn't seem to display anything on screen - I think this may be due to the code structure. Not really sure how this php inside javascript is working.

    Code:
    <?php
    
     define ("PHOTOS_DIRECTORY","/photos/");
    
     $counter = 0;
    
      if ($limit_found > 0) {
    
    	foreach ($results as $map_data) {
    
    		$photoFileName = PHOTOS_DIRECTORY.$results[".$counter."]['pic_path'];
    
    		echo "photoFileName = " . $photoFileName;
    
    		if (file_exists($photoFileName)) {
    
    			$picpath = $photoFileName;
    		} else {
    
    			$picpath = "photos/crap.gif";
    		}
    
        //if (file_exists(PHOTOS_DIRECTORY.$map_data['pic_path'])) {
        	//$picpath = PHOTOS_DIRECTORY.$map_data['pic_path'];
        //} else {
        	//$picpath = "photos/no-img.gif";
        //}
    
    	if (strlen($map_data['description']) > 100) {
    		$map_data['description'] = trim(substr($map_data['description'],0,100))."...";
    	} 
    
        echo " prop_ids[".$counter."] = ".$map_data['id'].";\n";
        echo " prop_lats[".$counter."] = ".$map_data['latitude'].";\n";
        echo " prop_lngs[".$counter."] = ".$map_data['longitude'].";\n";
        echo " prop_names[".$counter."] = \"".addslashes($map_data['businessName'])."\";\n";
        echo " prop_descs[".$counter."] = \"".addslashes($map_data['description'])."\";\n";
        echo " prop_picpaths[".$counter."] = \"".$picpath."\";\n";
        echo " prop_points[".$counter."] = new GLatLng(".$map_data['latitude'].",".$map_data['longitude'].");\n";
        echo " prop_markers[".$counter."] = \"".$map_data['category']."\";\n\n";
    
    
        $counter++;
    
       }
      }
    
    ?>
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  8. #8
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I managed to see the value of $photoFileName by setting the marker popup description to its value and its value is

    Code:
    /photos/th_photo_82707602.jpg
    which is a valid photo. I am therefore not sure why the file_exists is not recognising this as existing?

    I even made a test php file as follows and it also returns a result to say the photo does not exist but the photo definitely does exist and is in the photos directory

    Code:
    <?php
    
    	if (file_exists("/photos/th_photo_82707602.jpg")) {
    
    		echo "file exists";
    
    	} else {
    
    		echo "file  does not exists";
    
    	}
    
    
    ?>


    If I take the first slash off then it recognises it as existing

    Code:
    <?php
    
    	if (file_exists("photos/th_photo_82707602.jpg")) {
    
    		echo "file exists";
    
    	} else {
    
    		echo "file  does not exists";
    
    	}
    
    
    ?>

    Could this be something to do with the root and the fact in my FTP proggy the photo directory appears as /public_html/photos/
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  9. #9
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working!!

    Code:
    if (file_exists($_SERVER['DOCUMENT_ROOT']."/photos/".$map_data['pic_path'])) {
    
    	$picpath = $photoFileName;
    } else {
    
    	$picpath = "photos/no-img.gif";
    }
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •