problem with file_exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

problem with file_exists

Post by barb woolums »

I have some code for uploading images from a pc to a web server. Before uploading I check the file exists on the PC using the file_exists function (the image name is retrieved from a database). This works when using my local server which is running PHP 4, but it doesn't work on my hosting server(linux) which is running PHP 5.2.9, here the file is not found even though it does exist on the PC.

I asked my hosting provider for help and they turned on allow_url_encode. This didn't help, and I'm not sure it's relevant in this case anyway.

Any ideas? Here's the code:

$path is something like C:\Users\Barb\My Pictures\Recipes

Code: Select all

 
if ($_POST['image_folder']) {
    $path=$_POST['image_folder'];
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
     //check the file's extension
    $ext = strtolower(strrchr($image,'.'));
    if (in_array($ext,$limitedext)) { //only upload image if it is a valid image file
         $targetdir = "imagetmp/$prefix-".$image;
         $sourcedir = $path."\\".$image;    
         if (file_exists($sourcedir)) {
             list($width,$height) = getimagesize($sourcedir);
             if ($width > 250) {$width = 250;}
             if ($height > 250) {$height = 250;}
             require_once('thumbnail_create.php');
             $a = new Thumbnail($sourcedir,$width,$height,$targetdir,80,'"bevel()"');
             $newsourcedir = "imagetmp/$prefix-".$image;
             $newtargetdir = "images/recipe/$prefix-".$image;
             rename($newsourcedir, $newtargetdir);
    
         }
      }
  }
 
Any help would be much appreciated.
Last edited by barb woolums on Sat Jul 18, 2009 12:37 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: problem with file_exists

Post by requinix »

Nope, not relevant.

Code: Select all

$sourcedir = $path."\\".$image;
Linux cares about whether you use the right directory separator.

The "correct" solution is

Code: Select all

$sourcedir = $path.DIRECTORY_SEPARATOR.$image;
however Windows has no problem if you want to use "/" instead of "\".
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

Thanks for your quick response. However, that didn't work either. I also tried changing all the backslashes in the $path variable to slashes. I used fopen instead of file_exists in this instance as it actually produces an error

Here it is

Warning: fopen(C:/Users/Barb/My Pictures/Recipes/hands_27.gif) [function.fopen]: failed to open stream: No such file or directory

This file definitely exists on my PC at this path.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: problem with file_exists

Post by califdon »

I would echo the $sourcedir variable to see exactly what it is.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: problem with file_exists

Post by Eric! »

barb woolums wrote: Warning: fopen(C:/Users/Barb/My Pictures/Recipes/hands_27.gif) [function.fopen]: failed to open stream: No such file or directory

This file definitely exists on my PC at this path.
Are you sure? My XP box doesn't like the forward slash for paths and generates an error. However this would probably work on your host linux server with the proper path names...
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

Here's the result when I echoed $sourcedir (with quotes around it to ensure there weren't any spaces I didn't know about)

"C:/Users/Barb/My Pictures/Recipes/hands_27.gif"

The screenshot of the folder on my Pc with the image in it is below. I'm running Vista. The space in "My Pictures" wouldn't be a problem would it?
ScreenHunter_01 Jul. 19 07.31.gif
ScreenHunter_01 Jul. 19 07.31.gif (93.6 KiB) Viewed 1355 times
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

I checked just in case, but the space is not the issue.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: problem with file_exists

Post by califdon »

You had to go and make me hungry, didn't you?!! :twisted:

I don't run Vista, so I don't know, but it's possible it's the forward slashes that it doesn't like. At least try it with back slashes.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

Sorry about the pictures.

If you look at my original post, I was originally using backslashes. THis works on my local server but not the hosting server.

Very frustrating!!
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: problem with file_exists

Post by redmonkey »

If I'm reading it correctly, you are trying to check if the file exsts prior to uploading? It won't work when running on your remote server as it has no direct access to your local file system to be able to check if the file exists or not.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

.. and yet it works from my remote server using this code

Code: Select all

 
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
if (($_FILES['file']['name'] and $_FILES['file']['error']==0) | !$_FILES['file']['name']) {
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
    //check the file's extension
    $ext = strtolower(strrchr($_FILES['file']['name'],'.'));
    //uh-oh! the file extension is not allowed!
    if (!in_array($ext,$limitedext)) {
        $msgtxt = "Image selected is not a valid image";
        print("<div id='message_box' class='ok'><img id='close_message' style='float:right;cursor:pointer'  src='images/12-em-cross.png' >$msgtext</div>");
        exit();
    }
    $prefix=mt_rand(100000,999999);
    $target = "imagetmp/$prefix-".basename($_FILES['file']['name']);
    list($width,$height) = getimagesize($_FILES['file']['tmp_name']);
    if ($width > 250) {$width = 250;}
    if ($height > 250) {$height = 250;}
    require_once('thumbnail_create.php');
    $a = new Thumbnail($_FILES['file']['tmp_name'],$width,$height,$target,80,'"bevel()"');
    
    if(!$a) {$msgtxt = 'Error uploading image';}
} else {
    if ($_FILES['file']['error'] > 0) {
        $msgtxt = "<p style='color:#365B65;font-size:15px;text-align:center;' >The image failed to upload!!!</p>";
        switch ($_FILES['file']['error']) {
            case 1:  $msgtxt .= '- Image exceeds the maximum size for uploads';  break;
            case 2:  $msgtxt .= '- Image exceeds the maximum size of 500KB';  break;
            case 3:  $msgtxt .= '- Image only partially uploaded';  break;
            case 4:  $msgtxt .= '- No image uploaded';  break;
        }
    print("<div id='message_box' class='ok'><img id='close_message' style='float:right;cursor:pointer'  src='images/12-em-cross.png' >$msgtext</div>");
    }
}
echo $prefix,'-',basename($_FILES['file']['name']);
?>
 
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: problem with file_exists

Post by redmonkey »

Your second code sample is completely different and is not attempting to check the existence of the file on your local machine.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

Guess I'll have to use the same approach in the other scenario.

Thanks to all for your help
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

I've thought about this for a while, and I'm stumped. :banghead:

This is what I'm trying to achieve.

I have written code to import multiple recipes into a database on my remote server from a csv file on my PC which is all working apart from dealing with images of the recipes. In the csv file there may be the name of an image file. I have entered the path where this image should be found.

So while reading through the csv file I want to upload any images defined in the csv for any recipe and found in the spefified folder up to a folder on the remote server.

Any suggestions as to how I should go about this?
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: problem with file_exists

Post by barb woolums »

Further to this, I already have a php file which handles the upload of an image. This is used in a different scenario wher a single recipe is entered via a form. In that scenario I use ajax to trigger the upload php when an image file is browsed to. This works great.

I just don't how I can trigger this process in the other scenario described above.
Post Reply