问题
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