delete image files with php unlink()?
I am writing an account delete function that will query the db for an email, determine the user_id, use the user_id to locate image filenames and THEN use php to delete the files by filename in 5 different directories (each a different size). (oh yes, and delete records in 14 tables in the db - but not the prob here)
I was unable to delete the files using php unlink() - I checked the permissions directory and file chmod set to 777.
I uploaded a test image to same directory as delete_account.php file and could not delete it either.
Test uploaded image file originally had chmod 660 but I set it to 777.
Here is code:
$file1 = "Kevin.jpg";
if (unlink($file1))
{
echo ("Deleted $file1<br />");
}
else
{
echo ("Error deleting $file1<br />");
}
I also tried putting full url http://dev.ratemybutt.com/Kevin.jpg but does not delete.
Surely I am missing something obvious like a php.ini setting or something.
Butts need deleting!
greg posted this at 23:23 — 26th June 2008.
He has: 1,581 posts
Joined: Nov 2005
If the file's set to 777 then unlink should work.
Does PHP return any errors?
Like - blah...No such file or directory...blah
The only thing I can think of is an incorrect path. Perhaps something to do with your server config and/or the fact it's in a sub-domain.
I say that because if the PHP script is in the same dir as the image, you have the correct filename and it is 777 then there should be no reason for it not to delete the file, regardless of the file type.
You could try
<?php
$file1 = $_SERVER['DOCUMENT_ROOT']."/Kevin.jpg";
?>
But I am truly throwing guesses at you here, and you should be looking at the error log (if there is no on-screen reporting)
pr0gr4mm3r posted this at 23:59 — 26th June 2008.
He has: 1,502 posts
Joined: Sep 2006
When managing files in PHP, you should use the filesystem path, not the URL. So, it would be something like "/home/user/public_html/image/directory/Kevin.jpg".
If you aren't seeing any warnings, make sure you have error reporting enabled.
decibel.places posted this at 03:04 — 27th June 2008.
He has: 1,494 posts
Joined: Jun 2008
well, it seems I am neither stupid nor crazy (we shall see...)
I thought using filesystem path may be correct, but still get the error generated by the if...else statement in the code:
Error deleting /var/www/vhosts/dev.ratemybutt.com/httpdocs/Kevin.jpg
using
$file1 = $_SERVER['DOCUMENT_ROOT']."/Kevin.jpg";
generates the identical error:
Error deleting /var/www/vhosts/dev.ratemybutt.com/httpdocs/Kevin.jpg
Yes, it is a subdomain, created on server by server admin, client has Plesk cp, not so great for creating subdomains - I requested single ftp account access to the 4 related subdomains (there are also dev.ratemybooty.com dev.ratemychest.com dev.howdrunk.com that share the same image dirs and db on dev.ratemybutt.com ) It took some time to get everything working, server admin had to add conf files for dev.ratemybooty.com dev.ratemychest.com to appear...
I am using the following .htaccess file, it is necessary for members' profiles to display:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ http://dev.ratemybutt.com/index.php?option=profile&username=$1&task=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ http://dev.ratemybutt.com/index.php?option=profile&username=$1&task=$2 [L]
I ran phpinfo() in the same dir as the delete_account.php file and image file (attached)
here's the last few entries in error.log:
[client 67.83.143.95] PHP Warning: unlink(/var/www/vhosts/dev.ratemybutt.com/httpdocs/Kevin.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 24
[client 67.83.143.95] PHP Warning: unlink(/var/www/vhosts/dev.ratemybutt.com/httpdocs/Kevin.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 26
[client 67.83.143.95] PHP Warning: unlink(/var/www/vhosts/dev.ratemybutt.com/httpdocs/Kevin.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 26
[client 67.83.143.95] PHP Warning: unlink(/var/www/vhosts/dev.ratemybutt.com/httpdocs/Kevin.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 28
earlier log with filename (in same dir) no path:
[client 67.83.143.95] PHP Warning: unlink(Kevin.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 24
[client 67.83.143.95] PHP Warning: unlink(Kevin.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 24
[client 67.83.143.95] PHP Warning: unlink(Kevin.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 24
and here is even earlier log of trying to delete actual images in 5 directories:
[client 67.83.143.95] PHP Warning: unlink(new_images/thumbnails/091207065141.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 41
[client 67.83.143.95] PHP Warning: unlink(new_images/smallsize/091207065141.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 50
[client 67.83.143.95] PHP Warning: unlink(new_images/midsize/091207065141.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 59
[client 67.83.143.95] PHP Warning: unlink(new_images/original/091207065141.jpg): Permission denied in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 68
[client 67.83.143.95] PHP Warning: unlink(new_images/091207065141.jpg): No such file or directory in /var/www/vhosts/dev.ratemybutt.com/httpdocs/delete_account.php on line 77
decibel.places posted this at 03:28 — 27th June 2008.
He has: 1,494 posts
Joined: Jun 2008
Response from RackSpace about not being able to delete images on live sites - not even with ftp client which method does work on dev subdomains.
2 questions:
1) Where do I find the ACLs in Plesk?
2) Does it seem that changing permissions by the command line code below will enable a generic user/member to delete their own files by loading a php page in the browser? (this is intended to be a self delete tool - with ample warnings)
oh yes fyi I am logging in with ratebutt for ratemybutt.com -- and devadmin for dev.ratemybutt.com that username was set up by server admin - so for the dev sites there is either another user "dev" or support just made a mistake here.
and this is my ticket opener:
greg posted this at 10:14 — 27th June 2008.
He has: 1,581 posts
Joined: Nov 2005
As the error is permission denied... how do you upload the image "Kevin.jpg" in the first place?
If you use a HTML and PHP file upload form then it will be PHP that has permissions on the file and thus PHP will also have permission through your on-site files to delete it.
If you just FTP the "Kevin.jpg" and then try to delete it via on-site scripts with PHP then it is likely that PHP will not have permissions.
I would guess the file is owned by root, and therefore PHP wont have delete permissions .
So if you have been FTPing it, try using a file upload form on-site then try the exact same delete script you have been using.
decibel.places posted this at 14:30 — 27th June 2008.
He has: 1,494 posts
Joined: Jun 2008
there are a few contiguous situations going on here
1) images on live site are uploaded with php by users but directory is owned by root (is there a way to change that?)
2) dev site images are copied by ftp and not owned by root but also not created with php, uploaded in bulk with ftp
3) test image Kevin.jpg was uploaded by ftp
what about setting permissions with
for i in `find /var/www/vhosts/[DOMAIN] -type d \( -name 'http*docs' -o -wholename '*/http*docs/*' \) -print`; do setfacl -R -m (user|group):[USER/GROUPNAME]:rwx $i; setfacl -d -m (user|group):[USER/GROUPNAME]:rwx $i; done
decibel.places posted this at 15:24 — 27th June 2008.
He has: 1,494 posts
Joined: Jun 2008
my delete script works on the live sites where images were created with php
it would be nice to work on dev sites but not essential...
greg posted this at 16:22 — 27th June 2008.
He has: 1,581 posts
Joined: Nov 2005
SO I guess the correct path for this image on dev would be something like
/var/www/vhosts/dev/httpdocs/Kevin.jpg
Have you tried a test text file, rather than image, and see if you can fopen and write to it and delete it?
FTP a file like "test.txt" and try it, rather than use fopen to create it.
decibel.places posted this at 20:23 — 27th June 2008.
He has: 1,494 posts
Joined: Jun 2008
yeah, I thought about that kind of test
point is, got the self-delete tool up and working on the live sites, don't need to fuss much here on dev
as long as I don't have other problems with permissions
Warning: there are naked butts on these sites, some chests, too. not quite pornographic- the large user base indicates they seem to fill a valuable societal function... from an anthropologist's point of view - from a programmer's point of view they serve a valid financial function
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.