Solved

Permission Issue when running a PHP file through Cron

Posted on 2013-06-13
11
414 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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