我有一个带有以下结构的zip文件,可以用Php。
进行提取-/
|- db_data.ini
|- upload/
|- aa/
|- Many files here
|- bb/
|- Many files here
|- cc/
|- Many files here
|- ETC.
在upload
文件夹中,还有许多其他文件夹包含大量文件。
我需要的是将upload
目录解压缩到我的网站upload
目录。
我正在使用的代码如下:
$zip = new ZipArchive();
if ($zip->open($input_filename) !== TRUE) throw new Exception('Could not open the zip file !');
$zip->extractTo('/upload/', '/upload/');
$zip->close();
我已将目录chmod(ed)到0777,但它一直在Permission denied
行上给我extractTo
。
我做错了什么或者我错过了extractTo
的用法吗?
感谢您的帮助
答案 0 :(得分:1)
请检查您的phpinfo()输出是否已安装扩展程序。
您可以这样做:
将Zip文件解压缩到临时位置。
扫描并将所有文件移动(复制)到组合文件夹。
删除临时文件夹及其所有内容(在步骤1中创建)。
代码:
//Step 01
$zip = new ZipArchive();
$zip->open($_FILES['zip']['tmp_name']);
$zip->extractTo('temp/user');
$zip->close();
//Define directories
$userdir = "user/portfolio"; // Destination
$dir = "temp/user"; //Source
//Step 02
// Get array of all files in the temp folder, recursivly
$files = dirtoarray($dir);
// Cycle through all source files to copy them in Destination
foreach ($files as $file) {
copy($dir.$file, $userdir.$file);
}
//Step 03
//Empty the dir
recursive_directory_delete($dir);
// Functions Code follows..
//to get all the recursive paths in a array
function dirtoarray($dir, $recursive) {
$array_items = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($dir. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, dirtoarray($dir. "/" . $file, $recursive));
}
} else {
$file = $dir . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
// Empty the dir
function recursive_directory_delete($dir)
{
// if the path has a slash at the end we remove it here
if(substr($directory,-1) == '/')
{
$directory = substr($directory,0,-1);
}
// if the path is not valid or is not a directory ...
if(!file_exists($directory) || !is_dir($directory))
{
// ... we return false and exit the function
return FALSE;
// ... if the path is not readable
}elseif(!is_readable($directory))
{
// ... we return false and exit the function
return FALSE;
// ... else if the path is readable
}else{
// we open the directory
$handle = opendir($directory);
// and scan through the items inside
while (FALSE !== ($item = readdir($handle)))
{
// if the filepointer is not the current directory
// or the parent directory
if($item != '.' && $item != '..')
{
// we build the new path to delete
$path = $directory.'/'.$item;
// if the new path is a directory
if(is_dir($path))
{
// we call this function with the new path
recursive_directory_delete($path);
// if the new path is a file
}else{
// we remove the file
unlink($path);
}
}
}
// close the directory
closedir($handle);
// return success
return TRUE;
}
}