Solved

Permission Issue when running a PHP file through Cron

Posted on 2013-06-13
11
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 110

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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 7

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
 

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 7

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 110

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 7

Expert Comment

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

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 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