shadiadiph Posted June 9, 2010 Share Posted June 9, 2010 Probably a silly question but is there a function that can remove a zip directory from the server? I have tried rmdir and unlink neither work!! Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/ Share on other sites More sharing options...
JonnoTheDev Posted June 9, 2010 Share Posted June 9, 2010 Because a directory needs to be empty before it can be removed. Look at the examples for recursive deletion within the manual. http://uk3.php.net/rmdir Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1069890 Share on other sites More sharing options...
shadiadiph Posted June 9, 2010 Author Share Posted June 9, 2010 mm i have tried opening it removing the content tried opendir unlink closedir rmdir also been trung to do it with zip_open zip_close can't seem to get it to work Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1069894 Share on other sites More sharing options...
JonnoTheDev Posted June 9, 2010 Share Posted June 9, 2010 Does the user the script is running under have the correct permissions? Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1069901 Share on other sites More sharing options...
shadiadiph Posted June 9, 2010 Author Share Posted June 9, 2010 I did think of that too i changed chmod to 755 and it still wouldn't work. Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070065 Share on other sites More sharing options...
JonnoTheDev Posted June 9, 2010 Share Posted June 9, 2010 post your code Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070099 Share on other sites More sharing options...
shadiadiph Posted June 10, 2010 Author Share Posted June 10, 2010 Tried both of the codes below neither seem to work $openzip =opendir('metro.zip'); if ($openzip==false) { echo "Could not open metro.zip<br />"; exit; } if ($openzip==true) { unlink('metro.csv'); closedir('metro.zip'); rmdir('metro.zip'); } $zip = new ZipArchive; if ($zip->open('metro.zip') === TRUE) { $zip->deleteName('metro.csv'); $zip->close(); rmdir('metro.zip'); echo "Ok"; } else { echo 'failed'; } Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070228 Share on other sites More sharing options...
JonnoTheDev Posted June 10, 2010 Share Posted June 10, 2010 Bad code. You are using opendir to create a handle for a directory and not using it in any way. You are not supplying a server path to your directory or file to delete. Your folder name metro.zip, are you sure this is a folder, looks like a filename to me. <?php $openzip =opendir('metro.zip'); if ($openzip==false) { echo "Could not open metro.zip<br />"; exit; } if ($openzip==true) { unlink('metro.csv'); closedir('metro.zip'); rmdir('metro.zip'); } ?> To open a directory and recursively delete files & the folder something like the following will work <?php $dir = "/path/to/my/folder/from/server/root/"; // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if($filename != ".." || $filename != ".") { unlink($dir.$filename); } } closedir($dh); rmdir($dir); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070276 Share on other sites More sharing options...
shadiadiph Posted June 11, 2010 Author Share Posted June 11, 2010 php doesn't seem to recognise it as a directory but as a file when i try to unlink it it keeps telling me Warning: unlink(metro.zip) [function.unlink]: Permission denied in I need to have a rethink maybe i need to use ftp_connect and ftp_delete? Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070653 Share on other sites More sharing options...
JonnoTheDev Posted June 11, 2010 Share Posted June 11, 2010 err no. it simply means the owner of that directory is not that of the correct user, therefore you do not have permission to delete it. The error states clearly, 'Permission Denied'. Why would you use ftp functions? FTP functions connect to a remote FTP server, not the same server! Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070709 Share on other sites More sharing options...
shadiadiph Posted June 11, 2010 Author Share Posted June 11, 2010 So why would it still say access denied when it is set to 777? Somethings not right Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070717 Share on other sites More sharing options...
JonnoTheDev Posted June 11, 2010 Share Posted June 11, 2010 Who is the owner of the directory? Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070719 Share on other sites More sharing options...
shadiadiph Posted June 11, 2010 Author Share Posted June 11, 2010 its my website so i guess its me it's set to 777 so anyone should be able to anything with it Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070750 Share on other sites More sharing options...
JonnoTheDev Posted June 11, 2010 Share Posted June 11, 2010 Check! Run ls -l at a CLI prompt. If the directory is owned by the root user then there is no way it can be deleted. Why not run a simple test? Simply create an empty directory on your server (with the correct ownership). Then use rmdir() to remove it. <?php rmdir('/path/to/directory'); ?> Does it work? If so, again it is a permissions error on your other directory, the ownership of the directory probably is that of a user that the web server isn't a member of the same group. You cannot expect a php script to remove files/directories on a server that it did not create. If so you could end up deleting system files and folders, and attacks would be rife! Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070752 Share on other sites More sharing options...
shadiadiph Posted June 11, 2010 Author Share Posted June 11, 2010 on another site i built it works fine with a normal directory that ctreates a temp directory and sucessfully removes them just seems to be with zip files Quote Link to comment https://forums.phpfreaks.com/topic/204275-how-to-remove-a-zip-directory/#findComment-1070767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.