|
Whatever
|
 |
« on: June 07, 2010, 10:15:06 AM » |
|
If I use this code to check for a file I get the correct result: #!/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. #!/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
|
|
|
|
|
|
|
Whatever
|
 |
« 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
|
|
|
|
|
sarahA
|
 |
« 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 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
|
|
|
|
|
|
|
Whatever
|
 |
« 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. #!/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
|
|
|
|
Rosco
|
 |
« Reply #6 on: June 07, 2010, 03:26:14 PM » |
|
Try <?php if(file_exists('eos.csv')) { require('eos_update.php'); } else { echo "File not found"; } ?>
|
|
|
|
|
Logged
|
|
|
|
|
Whatever
|
 |
« 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
|
|
|
|
|
Whatever
|
 |
« 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
|
|
|
|
|
sarahA
|
 |
« 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
|
|
|
|
|
|
|
Whatever
|
 |
« 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
|
|
|
|
|
sarahA
|
 |
« 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
|
 |
« 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
|
|
|
|
|
Whatever
|
 |
« 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: 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
|
|
|
|
|
|
|
Whatever
|
 |
« 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
|
|
|
|
|
sarahA
|
 |
« 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
|
 |
« 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: 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
|
|
|
|
|
Whatever
|
 |
« Reply #19 on: June 09, 2010, 07:37:53 AM » |
|
Thanks for the info Sarah, will try it now.
|
|
|
|
|
Logged
|
|
|
|
|
sarahA
|
 |
« 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
|
 |
« 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: <?php
$current_dir = getCwd();
echo "Current directory is $current_dir";
$array = scandir(".", 1);
print_r($array); ?>
|
|
|
|
|
Logged
|
|
|
|
|
Whatever
|
 |
« 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
|
|
|
|
|
|
|
Whatever
|
 |
« 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
|
|
|
|
|