我一直在 – 我認為權限問題 – 解壓縮檔案(這部分沒問題)并将内容移動到寫檔案夾.
我正在運作簡單的代碼:
$zip = new ZipArchive( );
$x = $zip->open( $file );
if ( $x === true ) {
$zip->extractTo( $target );
$zip->close( );
unlink( $file );
rmove( __DIR__ . '/' . $target . '/dist', __DIR__ );
} else {
die( "There was a problem. Please try again!" );
}
其中rmove()是一個簡單的遞歸函數,它疊代内容并将rename()應用于每個檔案.
問題是解壓縮順利,檔案被複制,但沒有被移動 – 從臨時檔案夾中删除.到目前為止,我讀到的可能是由于在重命名時沒有對解壓縮檔案的寫入權限.
如何在解壓縮時控制這些權限?
更新:rmove()的内容:
function rmove( $src, $dest ) {
// If source is not a directory stop processing
if ( ! is_dir( $src ) ) return false;
// If the destination directory does not exist create it
if ( ! is_dir( $dest ) ) {
if ( ! mkdir( $dest ) ) {
// If the destination directory could not be created stop processing
return false;
}
}
// Open the source directory to read in files
$i = new DirectoryIterator( $src );
foreach( $i as $f ) {
if ( $f->isFile( ) ) {
echo $f->getRealPath( ) . '
';
rename( $f->getRealPath( ), "$dest/" . $f->getFilename( ) );
} else if ( ! $f->isDot( ) && $f->isDir( ) ) {
rmove( $f->getRealPath( ), "$dest/$f" );
unlink( $f->getRealPath( ) );
}
}
unlink( $src );
}
解決方法:
據我所知ZipArchive :: extractTo沒有設定任何特殊的寫/删除權限,是以您應該擁有對提取檔案的完全通路權限.
你的代碼的問題是rmove函數.您正在嘗試使用取消連結删除目錄,但取消連結會删除檔案.您應該使用rmdir删除目錄.
如果我們解決了這個問題,你的rmove函數可以正常工作.
function rmove($src, $dest) {
// If source is not a directory stop processing
if (!is_dir($src)) {
return false;
}
// If the destination directory does not exist create it
if (!is_dir($dest) && !mkdir($dest)) {
return false;
}
// Open the source directory to read in files
$contents = new DirectoryIterator($src);
foreach ($contents as $f) {
if ($f->isFile()) {
echo $f->getRealPath() . '
';
rename($f->getRealPath(), "$dest/" . $f->getFilename());
} else if (!$f->isDot() && $f->isDir()) {
rmove($f->getRealPath(), "$dest/$f");
}
}
rmdir($src);
}
您不必删除循環中的每個子檔案夾,最後的rmdir将删除所有檔案夾,因為這是一個遞歸函數.
如果仍然無法删除該檔案夾的内容,則可能沒有足夠的權限.我認為這不太可能,但在這種情況下你可以試試chmod.
标簽:php,permissions,ziparchive
來源: https://codeday.me/bug/20190710/1425028.html