Jump to content

How to remove a zip directory?


shadiadiph

Recommended Posts

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';
}

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);
    }
}
?>

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!

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.