DotDragnet
February 08, 2012, 08:01:48 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: PHP file_exists() issue  (Read 1944 times)
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« on: June 07, 2010, 10:15:06 AM »

If I use this code to check for a file I get the correct result:

Code:
#!/usr/local/bin/php

<?php
$filename 
'eos.csv';

if (
file_exists($filename)) {
    echo 
"The file $filename exists";
} else {
    echo 
"The file $filename does not exist";
}
?>

Now if I use this, I get the wrong result, ie. File not found.

Code:
#!/usr/local/bin/php

<?php

$filename 
'eos.csv';

  if(
file_exists('$filename')) {
      
exec('php eos_update.php');
    } else {
      echo 
"File not found";
    }
?>

I have tried changing the path to eos.csv but nothing seems to change the result. Is there something obvious I'm missing or doing wrong?

Thanks.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Rosco
Global Moderator
Hero Member
*****
Posts: 907



View Profile Awards
« Reply #1 on: June 07, 2010, 10:38:24 AM »

dunno about exec but couldn't you use require('eos_update.php') or header('location: eos_update.php') ?

I don't know why I'm even trying to advise on php big grin
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #2 on: June 07, 2010, 12:02:47 PM »

Whoosh, confused even more now!

Not sure to be honest Rosco. The script has worked before but seems very flaky which confuses me even more. When I had it run by cron on a minute basis is worked a couple of times and not the next...
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
sarahA
DDN Contribs
Hero Member
*****
Posts: 2123



View Profile WWW Awards
« Reply #3 on: June 07, 2010, 01:20:18 PM »

Daft question maybe, but is exec allowed? You can check in phpinfo.

Second point would be that you have

Code:
  if(file_exists('$filename')) {

in your second script. Remove the single quotes, I don't believe the variable gets 'translated' as a variable within single quotes, only double quotes.
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #4 on: June 07, 2010, 01:24:13 PM »

Don't quote $filename in the second code. It is checking for a file literally named $filename.

(Generally, don't ever quote variables unless to type cast to a string, and in that case you would need double quotes).

header() is certainly wrong but I don't see why you used exec and not require.

Too slow at typing today.
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #5 on: June 07, 2010, 03:17:10 PM »

OK, so I have this now, slightly simplified, but still not working. All files are in the same folder.

Code:
#!/usr/local/bin/php

<?php
  
if(file_exists('eos.csv')) {
      require(
'php eos_update.php');
    } else {
      echo 
"File not found";
    }
?>
« Last Edit: June 07, 2010, 03:23:17 PM by Whatever » Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Rosco
Global Moderator
Hero Member
*****
Posts: 907



View Profile Awards
« Reply #6 on: June 07, 2010, 03:26:14 PM »

Try

Code:
<?php
  
if(file_exists('eos.csv')) {
      require(
'eos_update.php');
    } else {
      echo 
"File not found";
    }
?>

Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #7 on: June 07, 2010, 04:25:49 PM »

Nope. Added the first line back in and tried a few variations and getting other errors. Will look at it later.

Thanks Ross.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #8 on: June 08, 2010, 09:50:23 AM »

Ok, so I believe the first line is required to make the php file run?

Just so I'm straight, if I look at my phpinfo page (myofficestationery.com/phpinfo.php), which value am I looking for?
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
sarahA
DDN Contribs
Hero Member
*****
Posts: 2123



View Profile WWW Awards
« Reply #9 on: June 08, 2010, 11:08:45 AM »

What exactly are you trying to do in the file? It's very hard with just a simple if statement. If you're simply checking to see if a CSV exists in the same directory as the script you're running, and if it is then run a PHP file called eos_update.php, also to be found in the same directory (so all 3 files in the same directory), then the code Rosco gave should be fine.

What errors are you getting?

If the point of the file / if statement is different to that then it may help to give a bit of background info on it first.
Logged

Rosco
Global Moderator
Hero Member
*****
Posts: 907



View Profile Awards
« Reply #10 on: June 08, 2010, 11:24:33 AM »

I've also tested that code on my own webspace, using require(), and it works fine

The problem might be in the full version of your code or in eos_update.php
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #11 on: June 08, 2010, 12:14:09 PM »

I'm supplied weekly with a file called eos.csv. I upload this and then run the file eos_update.php. This then reads the file and updates our sell prices on the website. This all works fine but I want to take the manual processes out of the equation.

Our supplier can ftp the eos.csv file to our server. I then was looking to run a cron job every 4 hours to run eos_file_checker.php which in turn would check if the eos.csv file had been uploaded yet. If it had it would run the eos_update.php file and that sends me an email once complete to let me know it's all successfully worked.

All files are in the same folder.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
sarahA
DDN Contribs
Hero Member
*****
Posts: 2123



View Profile WWW Awards
« Reply #12 on: June 08, 2010, 12:51:21 PM »

Then the code Rosco gave should be fine. Are you testing this via a browser or via a cron at the moment?

What errors are you getting and what's the full code for the main file?
Logged

Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #13 on: June 08, 2010, 01:36:59 PM »

So running that through the browser now it works and runs the file. The only issue is a permissions issue when renaming the log file but I can sort that later.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #14 on: June 08, 2010, 05:48:38 PM »

Thanks so far guys. Now my next issue is running this from a cron job.

I set up a cron to run public_html/imports/eos/eos_file_checker.php at set intervals and this is what I get via email notification:

Quote
public_html/imports/eos/eos_file_checker.php: line 1: ?php: No such file or directory
public_html/imports/eos/eos_file_checker.php: line 2: syntax error near unexpected token `'eos.csv''
public_html/imports/eos/eos_file_checker.php: line 2: `  if(file_exists('eos.csv')) {'

I believed the way to clear this was to specify the path to php?
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #15 on: June 08, 2010, 07:06:05 PM »

You need the #! path like you had in the first post.
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #16 on: June 08, 2010, 09:18:32 PM »

Ok, so now it works but can't see the file.

I think I read about false negatives so will keep Googling. If anyone has any suggestions then I'd gladly welcome them!

Cheers all.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
sarahA
DDN Contribs
Hero Member
*****
Posts: 2123



View Profile WWW Awards
« Reply #17 on: June 09, 2010, 07:36:48 AM »

What's the command for the cron? I don't usually put the php path in the php file, I have my cron set to

/usr/bin/php /home/username/public_html/file.php

So going by your original post

/usr/local/bin/php /home/username/public_html/imports/eos/eos_file_checker.php

But you'll need to change username to your site's username and check the home directory is correct.

That's via a cPanel CRON by the way, not sure if it's different for others
Logged

Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #18 on: June 09, 2010, 07:37:34 AM »

Looking at this closely, it works fine unless run by cron so now my thinking is file ownership/permissions.

I have changed permissions of the files temporarily to 777 and the folder it's in but this doesn't change anything.

Here's what i get when I list the contents of the folder via SSH:

Quote
drwxrwxrwx 3 myoffice myoffice 4096 Jun  8 23:35 ./
drwxrwxrwx 6 myoffice nobody   4096 May 31 17:44 ../
-rwxrwxrwx 1 myoffice myoffice  180 Jun  8 22:50 eos_check.php*
-rwxrwxrwx 1 myoffice myoffice  162 Jun  8 22:45 eos_file_checker.php*
-rwxrwxrwx 1 myoffice nobody   1721 Jun  8 14:25 eos_update.php*
drwxrwxrwx 2 myoffice myoffice 4096 Jun  8 23:31 log/

Should I be looking to CHOWN the files in question to nobody or should this not make a difference?
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #19 on: June 09, 2010, 07:37:53 AM »

Thanks for the info Sarah, will try it now.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
sarahA
DDN Contribs
Hero Member
*****
Posts: 2123



View Profile WWW Awards
« Reply #20 on: June 09, 2010, 07:41:31 AM »

If it runs through the browser then the server/cron should run it without a problem providing it's set up correctly. I used to have a zip file of photos and csv file put on a client's server every night and had a cron run a script to import the csv and open the zip up and resize the photos in it. I didn't have writeable permissions on the import files as these just need reading.
Logged

Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #21 on: June 09, 2010, 08:07:18 AM »

It still couldn't find the file Sarah.

This might be leading me up the wrong path but I created a file which gets the current working directory and lists the contents. If I run it via a browser it works fine and shows me it can indeed see eos.csv listed in the imports/eos folder. Now if I run this directory listing via the cron job, it lists the contents of the root directory rather than the imports/eos folder.

My file which lists the files is in the eos folder too so why would it show the contents of the root directory when run by cron?? This is the code:

Code:
<?php

$current_dir 
getCwd();

echo 
"Current directory is $current_dir";

$array scandir("."1);

print_r($array);
?>

Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #22 on: June 09, 2010, 08:23:43 AM »

The file works when run in root but causes a few php errors.

I'm going to have to leave this for now as a customers whole network is down so i have to go out to them.

Thanks sarah
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #23 on: June 09, 2010, 08:37:25 AM »

Cron doesn't set a working directory.

chdir(dirname(__FILE__));

before your file_exists line.
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #24 on: June 09, 2010, 09:18:57 AM »

Success! That sorted it.

Many thanks Jason, Sarah and Rosco for all your help and putting up with me!
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!