Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

File listed in a 'variable' cannot be found.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

File listed in a 'variable' cannot be found.

Postby pewe » Wed Apr 03, 2013 2:28 pm

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:

Code: Select all
$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: Select all
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: Select all
[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
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Wed Apr 03, 2013 2:39 pm

unable to open is not the same as 'does not exist' have you checked is_readable($image)? It may just be a permissions issue.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby pewe » Wed Apr 03, 2013 2:54 pm

Thanks for the quick response seandisanti.

Sorry, I was going on the echo error - forgetting that it is printing what I put in the script :oops:

The directory permission is 755 and the file is 664 - so should not be a problem.
ALSO - if I hardcode the value of the variable (eg $Image = 'original/raleighbike.JPG';) the script works as it should, extracting and storing the Exif data in a database as per the script.

This is what totally baffles me
- hardcoded = works, variable = not working.

(Sorry - I should have mentioned that in the OP).
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Wed Apr 03, 2013 3:37 pm

What version of php are you using? just trying to recreate on my side now
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Wed Apr 03, 2013 3:46 pm

This is working for me....
Code: Select all

$blah 
= 'tests/test1.jpg';
$exif = exif_read_data($blah, 0, true);
echo "test1.jpg:<br />\n";
foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
        echo "$key.$name$val<br />\n";
    }
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby pewe » Wed Apr 03, 2013 4:20 pm

It is PHP Version 5.3.22.

Thanks for the script - however this also works for me - it is what I meant by hard coding - ie

Code: Select all
$Image = 'original/raleighbike.jpg';

echo $Image;

echo file_exists($Image) ? 'ok' : '  file not found';
   
$exif = exif_read_data($Image, 0, true);


This will work as it should.

The problem seems to occur when I load the variable $Image from a variable created in the previous php page
- even though when I check the content of the variable $Image once created it has the correct info - ie 'original/raleighbike.jpg' (without the ')
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Wed Apr 03, 2013 4:44 pm

It shouldn't be causing this particular issue, which I'm going to research some more, but when you're able you should upgrade to 5.4. There were a lot of changes behind the scenes that make 5.4 a better option when available
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby pewe » Wed Apr 03, 2013 5:12 pm

Thanks seandisanti.
Unfortunately I have no control over the php version as it is up to my hosting service.

I will also keep looking, but anything you can uncover would be appreciated.
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Wed Apr 03, 2013 5:18 pm

how are you passing the variable from page to page? I've tested with post, get, and session and all seem to be working. I am beginning to think that maybe yours is a variable scope issue.
here's working test with post...
Code: Select all
<?php
if ($_POST){
    
$blah $_POST['image'];
    echo 
$blah;
$exif exif_read_data($blah0true);
echo 
"test1.jpg:<br />\n";
foreach (
$exif as $key => $section) {
    foreach (
$section as $name => $val) {
        echo 
"$key.$name$val<br />\n";
    }
}
} else {
    echo 
'<html>
        <form action="' 
$_SERVER['PHP_SELF'] . '" method="post">
            <input type="text" name="image" placeholder="Enter path to image" />
            <input type="submit" value="exif_read_data" />
        </form>'
;
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby pewe » Wed Apr 03, 2013 6:21 pm

I have attached the complete file set to this reply.
The method for passing the variable is at the bottom of the file maxImageUpload.class.php, and is picked up at the top of the file 'displayexif.php.

- upload.php is the file which is accessed to start the process.
- maxImageUpload.class.php uploads the image, stores the original and creates a smaller version and a thumbnail, storing them in their relevant directories
- calc.php takes the lat and longitude from the exif and converts it to decimal
- displayexif.php sources the exif from the uploaded image, lists it on a page with the image, and stores all the data in a sql database.

The sql table details are also included.

Any insight you can offer will be greatly appreciated.

It's now 3.00am here so I'm off to get some sleep, and will pick this up again tomorrow.

Sent you a PM

Thanks.
Attachments
upload.zip
(7.36 KiB) Downloaded 35 times
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Wed Apr 03, 2013 8:29 pm

Ok, I am in the process of rewriting some big sections of these files, so it may take me a little bit to post revisions. A few practices that I'm seeing in here to try to avoid are shortcut tags (<? instead of <?php), and going in and out of php and html. it's better to stick with php and echo out your markup as necessary. it helps avoid confusion when reading through the code, code folding for ide's etc. also, mysql_ functions are deprecated, they're slow and less secure than PDO objects.

I will post completed code once i'm done rewriting it, and can explain the changes as necessary. For now if you want more info about any of the practices mentioned above, I've ranted at length on the last one in this forum, and the others are more about trying to stick to best practices.

Please keep in mind I'm not pointing those things out to attack you or your predecessor on the project, but only so that you're aware of them to avoid in the future for your own benefit. The downside to the longevity enjoyed by the php language is that many of the available resources have not been maintained over time, and a lot of them still encourage deprecated practices and functions like the mysql_ functions. The majority of books at my local library on the subject are circa 2009 or earlier with only 3 in the whole district published later than 2011.

I do not pretend to be an authority on the language, but have had the benefit of learning first hand from some pretty good authorities, and a LOT of trial and error with old resources. </rant>
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Wed Apr 03, 2013 10:51 pm

ok, there are headaches, but i messed with it enough to identify and fix a lot of them. I changed the way that the database is interacted with. modify constants.php to have your credentials and db name.

some things you're still having issues with: it's trying to proceed before file is finished uploading. That is trivial to fix so i'm leaving it to you to research how to evaluate the original file size, and stored file size and wait until they're the same to proceed. This can be demonstrated by refreshing the result page and the errors go away once the file is uploaded.

also, your code assumed that not only was exif_read_data returning data, but that it was returning full data for every file, which is not typically going to be the case for a variety of reasons. i modified the code so that it only looks for sub keys when the main index is set, and modified every echo to include a ternary operation so it only echoed variables if they were already set.

For the record, that is the absolute worst way to echo fields. The better, faster, and easier way to do that, is to call your exif_read_data, and if it returns an array, to loop through the array echoing each key and value. That is trivial so i am leaving it for you to implement. Make sure you fix the wait time before you try to tackle this one, so that you are only trying to read data from finished files.

Other changes were made, but it is now nearing my bedtime so I will leave it to you to ask if something doesn't work still. have a great night.
Attachments
upload.zip
(7.18 KiB) Downloaded 36 times
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby pewe » Thu Apr 04, 2013 5:09 am

Thanks so much for the input.
My son was not an expert coder and was developing a web site for the family and friends purely as a favour and to rekindle previous experience of php and mysql that he had started to learn with a previous employer a few years ago - so I'm not surprised some of it is outdated.

I have even less knowledge than he did, although I can follow logic and have managed to muddle through most things so far. I will have a look at your recommendations/suggestions for further changes when I have some more time.

As far as the current issue is concerned the problem unfortunately it is still there.

I modified the start of the displayexif file then ran displayexif.php directly to see what happens if I hard code the location and then pass it through variables more than once - like this:

Code: Select all
$picture = 'original/raleighbike.jpg';

$photo1 = $picture;

$Image = $photo1;

echo "$picture<br />";

echo "$photo1<br />" ;

echo $Image. '<br />';

echo $thumb. '<br />';

echo file_exists($photo1) ? 'ok<br />' : '  photo file not found<br />';

echo file_exists($picture) ? 'ok<br />' : ' picture file not found<br />';

echo file_exists($Image) ? 'ok<br />' : ' image file not found<br />';

echo file_exists($thumb) ? 'ok<br />' : '  thumb file not found<br />';
   
$exif = exif_read_data($Image, 0, true);


This worked perfectly as seen here:

displayexif-th.JPG
displayexif-th.JPG (169.32 KiB) Viewed 980 times


However as soon as I altered displayexif to pickup the variable $photo (passed from upload.php when I run it) like this;

Code: Select all
$picture = $photo;

$photo1 = $picture;

$Image = $photo1;

echo "$picture<br />";

echo "$photo1<br />" ;

echo $Image. '<br />';

echo $thumb. '<br />';

echo file_exists($photo1) ? 'ok<br />' : '  photo file not found<br />';

echo file_exists($picture) ? 'ok<br />' : ' picture file not found<br />';

echo file_exists($Image) ? 'ok<br />' : ' image file not found<br />';

echo file_exists($thumb) ? 'ok<br />' : '  thumb file not found<br />';
   
$exif = exif_read_data($Image, 0, true);


the files were listed in the variables correctly as can be seen below but could no longer be found when called by either function (file_exists or exif_display_data). In fact the variable $Image still contains the correct info as it is used later in the script to display the image and add the location to the database - which it does correctly ;

upload-th.JPG
upload-th.JPG (180.81 KiB) Viewed 980 times


So if the variable $Image ultimately contains the same information (ie original/raleighbike.jpg) which ever way it is generated, I just do not understand why there should be this issue with the result depending on how the variable was initially created.

Did you have time to test it on your server, and if so what were the results.
If not (and I understand if you don't have the time) have you any ideas as to what in PHP could cause this behaviour - could it be a server side issue??

Thanks once again for your assistance thus far.
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Thu Apr 04, 2013 7:22 am

the revisions that i posted work, with the exceptions being those that i listed.

1) execution continues prior to file upload completing which generates errors

2) EXIF data is not always set, so you cannot assume all variables are going to have values.

I coded around #2 by only echoing variables that were set using ternary operations, but suggested rewriting that portion to make use of a foreach() loop on the arrays created by your exif_read_data calls. that way you will only ever be echo'ing or querying keys that actually exist.

#1 is what is causing the behavior that you're explaining. you can observe this by uploading a small photo, then leave the error up on screen for about 15 seconds and refresh the page. the errors will probably be gone at that point.

again, these statements are true for the files that I uploaded, not necessarily the original files that you'd uploaded.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby pewe » Thu Apr 04, 2013 9:59 am

seandisanti - thanks once agai for your help.

I missed the point about refreshing in your previous reply - a bit brain dead with all the visits to the hospital and late nights so forgive me.

I did a quick refresh of a previous upload and as expected you were perfectly right.

I will research how to evaluate the original file size, and stored file size etc as soon as I can, but as my son has taken a turn for the worse I am off to take his mother to the hospital.

Thanks once again and will post results in due course.
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Thu Apr 04, 2013 10:36 am

you can look at filesize, or you could just research sleep() and is_writable(). I think you can piece it together pretty quickly once you understand those 2 functions
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: File listed in a 'variable' cannot be found.

Postby pewe » Sat Apr 06, 2013 4:00 am

Sean.
Using time between other commitments I did some research and was experimenting with some of you suggestions.
Although I did not get very far I came across a different upload script which used similar code, but a slightly different approach - and it worked without causing the 'delay' problem.

The only thing I could see that was a major difference was the way the new script stores the upload as a 'tmp' file transferring it to the final destination on completion whereas the original script (I think) is uploading direct to the final directory.

I could be misreading the original script (given my lack of coding experience) but could 'logically' see how the problem could occur if that is the case.

Anyway, I have other things to get on with to finish this project 'in time' so at least I don't appear to have to spend any more time on this issue - but who knows what's round the corner.

Thanks for your help without which I would still be trying to figure out why the file was always missing (or so it seemed).
pewe
New php-forum User
New php-forum User
 
Posts: 16
Joined: Fri May 25, 2012 6:05 am

Re: File listed in a 'variable' cannot be found.

Postby seandisanti » Mon Apr 08, 2013 11:03 am

No problem, and good luck man. Definitely continue posting if you run into more issues, always happy to help.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron