Problem with php and using variables for file selection.

Associate
Joined
3 Apr 2013
Posts
11
I have a problem using a variable to extract Exif data from a photo file as follows:

1 - I use a script that uploads and stores jpg's and as it does so it creates variable '$photo' which contains the name of the jpg and the directory it is stored in.
I also set a second variable called '$thumb' which contains the name and location of a thumbnail of the jpg which is created and stored by the upload script.

2 - On completion of the upload an exif extraction script is launched which starts by using the '$photo' variable to create a variable called '$Image' which is used at various points in the script after exif extraction - for example to fetch and display the image as well store the image details in a sql database.

When I run the script it creates a PHP error saying that the 'exif_read_data' function cannot find the file.

So in the script I set some 'echo' commands to verify the contents of the variables, as well as too check that the file held by the variable exists.

This is the script code:

PHP:
$Image = $photo;

    echo "$photo";

    echo $Image;

    echo file_exists($Image) ? 'ok' : ' image file not found';

    echo file_exists($photo) ? 'ok' : '  photo file not found';

    echo $thumb;

    echo file_exists($thumb) ? 'ok' : '  thumb file not found';

    $exif = exif_read_data($photo, 0, true);

The printed result of this is

Code:
original/raleighbike.jpg - content of variable $photo (correct)
original/raleighbike.jpg - content of variable $Image (correct)
image file not found - when checking for the file in $Image (incorrect, file does exist)
photo file not found - when checking for the file in $photo (incorrect, file does exist)
thumb file not found - when checking for the file in $thumb (incorrect, file does exist)
thumbnail/thumb_raleighbike.jpg

So the content of the variables are showing correctly and confirming they are holding the right location/filenames, and this is verified later in the script when the variable content is used to dispaly the jpg and also store its content in a sql database.
BUT the 'exif_read_data' function cannot find the files returning the error

Code:
[02-Apr-2013 18:45:29 America/Chicago] PHP Warning:  exif_read_data() [<a href='function.exif-read-data'>function.exif-read-data</a>]: Unable to open file in /home2/webi8726/public_html/domain.com/sqlsidebar/displayexif.php on line 23

Line 23 is the '$exif =' line above.

I have tried searching for any clues as to why this happens, and also asked various php 'experts' and not yet come up with an answer to the problem.

Can anyone here suggest a solution?? - here's hoping;)
 
Last edited:

fez

fez

Caporegime
Joined
22 Aug 2008
Posts
25,806
Location
Tunbridge Wells
At a glance I would say that you are not getting the full path to the picture. Whenever you are using file access methods you must reference the full path to the resource. You can use things like dirname(__FILE__) to get the path to the current file and then go from there or just specify the full path.

When you upload a file to the server it puts it in a tmp directory. You then move/copy that file to somewhere useful like an upload folder.

On a mac, that folder path might be /Users/username/Sites/mysite/uploads/image.jpg

To access that via your site to display the image, you may be able to specify the path as uploads/image.jpg but if you wanted to do something like getting the exif data, you would need the full filesystem path.
 
Last edited:
Associate
OP
Joined
3 Apr 2013
Posts
11
Thanks fez - but this does not appear to be the case.

If I hardcode the location of an already uploaded image the script works.

$Image = 'original/raleighbike.jpg';

echo $Image;

echo file_exists($Image) ? 'ok' : ' file not found';

$exif = exif_read_data($Image, 0, true);

This will work and the exif is extracted.

It is when I use a previously set variable to create $Image that the error occurs, even though the content of the variable $Image is 'original/raleighbike.jpg' once it is created.
 

fez

fez

Caporegime
Joined
22 Aug 2008
Posts
25,806
Location
Tunbridge Wells
I am somewhat confused when you say

If I hardcode the location of an already uploaded image the script works.

I assume that the images that you are referencing via variables are also already uploaded.

Its a very small snippet of code that shouldn't be hard debug but without knowing what the rest of it does, it could be tricky to help.
 
Associate
OP
Joined
3 Apr 2013
Posts
11
I am somewhat confused when you say



I assume that the images that you are referencing via variables are also already uploaded.

Its a very small snippet of code that shouldn't be hard debug but without knowing what the rest of it does, it could be tricky to help.

Fez - thanks for your comment.

I have discovered the problem and now need to research a solution.

To use a hardcoded method to load the variable the image had to be on the server already (as you pointed out).

When the variable was generated from the upload of an image, the part of the script which reads the variable returns positively showing the image 'location/filename' BUT the part of the script which reads the exif from the file (and the part which checks the existence of the file in the directory) are launching BEFORE the the file has finished uploading, so report that the file does not exist.

So now I have to find a way of delaying the execution of the read_exif-data function until the upload has finished - taking in to account the fact that different file sizes will take longer to upload - bandwidth will also affect the time.

So time for some more research :(
 

fez

fez

Caporegime
Joined
22 Aug 2008
Posts
25,806
Location
Tunbridge Wells
Hmmm, that shouldn't be happening as far as I know. When you upload an image via php it uploads the file then the form submit is complete and your script it posts to is executed. The file is put in a temp directory then a reference to that temporary location is added to the $_POST array under the key of the input['file'] element.

Is the image upload via a standard php form and its not done via ajax or anything like that (shouldn't be an issue even if it is tbh).
 
Associate
OP
Joined
3 Apr 2013
Posts
11
Hmmm, that shouldn't be happening as far as I know. When you upload an image via php it uploads the file then the form submit is complete and your script it posts to is executed. The file is put in a temp directory then a reference to that temporary location is added to the $_POST array under the key of the input['file'] element.

Is the image upload via a standard php form and its not done via ajax or anything like that (shouldn't be an issue even if it is tbh).

I am not very php experienced so I am not too familiar with the way it all works, but I was told that the problem could be to do with the upload timing. Sure enough when I waited a while (30 secs or so) after the page returned the 'could not open file' message and refreshed the page, the file was found and the info displayed.

Unfortunately I cannot attach files or I could send you the upload script which I'm sure would answer your question, but the script does use a form for identifying the file locally and uploading it and the script is all self contained in a single php file.

I have put a copy here if you have time to look at it
Code:
http://www.ukf.com/Upload.class.zip

I'd be interested in your comments.
 
Associate
OP
Joined
3 Apr 2013
Posts
11
All is OK now.
Not sure why, but using a rewritten script seems to have resolved the issue.
Similar code but the unable to open file issue is gone.

Thanks for the contributions
 
Back
Top Bottom