Solved

Permission Issue when running a PHP file through Cron

Posted on 2013-06-13
11
417 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 
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

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.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

635 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