Solved

Permission Issue when running a PHP file through Cron

Posted on 2013-06-13
11
412 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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multi line FPDF footer: 3 24
restriction of entering a a page 5 31
Help to debug powershell script 5 38
Displaying a google org chart from php/mysql code 27 17
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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 …

762 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