Jump to content

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

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?

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.