Solved

Permission Issue when running a PHP file through Cron

Posted on 2013-06-13
11
408 Views
Last Modified: 2013-06-13
Hi guys,

I've had a few questions answered around the topic of call a PHP File through Cron, and seem to hit a problem every step of the way!

The latest problem.....

So, i'm using Cron to call a PHP file, which is working fine and the script is being called. The script is designed to delete some files from the root of my site.

When I run this outside of Cron, it works fine (run as in, upload the PHP and navigate to it), and the appropriate files are being deleted.

However, when the script is called through Cron, I get a permissions error.

PHP Warning:  unlink(): Unable to access ../Uploaded_Files/MS_Keys.xlsx in /var/www/vhosts/"Domain"/httpdocs/Code/clean_up.php on line 16 102

Where MS_Keys.xlsx is the file that needs to be deleted.

I'm using Plesk 11 through my webhosts to configure the site, although I do not have access to the shell to run any scripts directly on it.

Now this seems like a permissions issue. Does anyone know where I change the permissions on the folder 'Uploaded_Files', and what I need to change it to so that my script can delete any files contained within it?

Many humble thanks in advance,
0
Comment
Question by:gesmuk
  • 5
  • 3
  • 3
11 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39245138
Your directory paths will be different when you run CRON vs when you run a web page.  Consider using phpinfo() in the CRON script.  You use ob_start() to capture the output.  You can email the captured output to yourself and compare it to the web script output.
0
 

Author Comment

by:gesmuk
ID: 39245278
Cool, I've tested a few things and it definitely seems like the path is the issue.

Can you give me an example of what the script would look like to email myself the output of phpinfo()?
0
 

Author Comment

by:gesmuk
ID: 39245305
Actually, no worries. I've got that emailed to me now.

If I post up the result, could you point me towards what path I need to enter in?
0
 
LVL 6

Accepted Solution

by:
Robert Saylor earned 334 total points
ID: 39245527
try this:

unlink('/var/www/vhosts/yourdomainname.com/httpdocs/Uploaded_Files/MS_Keys.xlsx');
0
 

Author Comment

by:gesmuk
ID: 39245781
Thanks for the suggestion, I'm afraid I get the following message though:

PHP Warning:  unlink(/var/www/vhosts/mydomain.co.uk/httpdocs/Uploaded_Files/MS_Keys.xlsx): No such file or directory in /var/www/vhosts/mydomain.co.uk/httpdocs/Code/testmail.php
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:gesmuk
ID: 39245792
Ok, so I've just tried this again with the following command:

unlink('../Uploaded_Files/MS_Keys.xlsx);

But i'm getting a different message from the one above. This time it looks as though it's found the file, but doesn't have permission to delete it, or I

PHP Warning:  unlink(): Unable to access ../Uploaded_Files/MS_Keys.xlsx.png in /var/www/vhosts/shareafile.co.uk/httpdocs/Code/testmail.php on line 4
0
 
LVL 6

Assisted Solution

by:Robert Saylor
Robert Saylor earned 334 total points
ID: 39245797
You need to plug in the absolute path.

I think $_SERVER['docroot'] will get that

Create info.php with

phpinfo();

It will show you
0
 

Author Comment

by:gesmuk
ID: 39245889
Issue resolved!

Bizarrely, the file IS being deleted, but i'm still getting the same 'No File or Directory Found' error message email to me (I have the alerts turned on in Plesk).


I can only assumed that the file is being deleted, and then the error generated to say that it can't be found

So the correct folder path does seem to be:

/var/www/vhosts/mydomain.co.uk/httpdocs/Uploaded_Files/

Thanks for all your help, been battling this one for a few weeks!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39245953
... the file IS being deleted, but i'm still getting the same 'No File or Directory Found' error
And that would make sense, every time, after the first time the script was run!

;-)

~Ray
0
 
LVL 6

Expert Comment

by:Robert Saylor
ID: 39245971
Put @ in front of unlink to silence the error
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39245981
And whether or not you have the @ to suppress the error, the function will still return TRUE or FALSE, so your script can know the outcome.
http://php.net/manual/en/language.operators.errorcontrol.php
http://php.net/manual/en/function.unlink.php
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now