How to delete a zip file using php

荒凉一梦 提交于 2020-01-11 14:37:53

问题


This is my php code to delete a zip file:

$name = "zip_file_name";
chmod('./modules/',0777);
unlink('./modules/'.$name.'.zip');

Here modules is the folder of zip file contained. When I wrote this code I got an error:

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">



<h4>A PHP Error was encountered</h4>



<p>Severity: Warning</p>

<p>Message:  unlink(./modules/texts.zip) [<a href='function.unlink'>function.unlink</a>]: Permission denied</p>

<p>Filename: controllers/super_admin.php</p>

<p>Line Number: 590</p>



</div>

If there is any mistake in my code?

I also check the chmod like this:

chmod('./modules/'.$name.'.zip',0777);

But I got the same error define above.


回答1:


Additionally you would have to change permissions to the zip, to make it writable first

chmod('./modules/'.$name.'.zip',0666);

666 = read / write for all Make sure that will return true. But since you cannot delete the file, most likely you will not be able to change it's permission also as Apache is not the owner.

777 for the parent directory is required only to write new files to that directory, do not affect operations that you can do with files that are already there.




回答2:


$name = "zip_file_name";
$sPath = "./modules/" . $name . ".zip";
$aFilePath = explode("/", $sPath);
$i = 0;
$sLastFolder = "";
foreach ($aFilePath as $sFolder) {
    $i++;
    if (file_exists($sLastFolder . $sFolder) || is_dir($sLastFolder . $sFolder)) {
        $this->make_writeable($sLastFolder . $sFolder);
        $iOldumask = umask(0); // important part #1
        chmod($sLastFolder . $sFolder, 0777);
        umask($iOldumask); // important part #2
        $sLastFolder .= $sFolder . "/";
    }
}
unlink($sPath);


来源:https://stackoverflow.com/questions/8968324/how-to-delete-a-zip-file-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!