我在使用PHP解压缩上传的文件时遇到问题。我可以上传很好,.zip被复制到它应该的位置,但它不会解压缩。目录和zip正在正确地chmod到777. apache错误日志只显示“错误:无法为.zip中的每个文件创建[whateverfile]”。知道为什么会这样吗?欣赏它!请告诉我这是否属于stackoverflow,但它似乎是一个apache / os / somethingelse问题。代码如下:
<?php
$target_path = "/home/someuser/hostgames/";
mkdir($target_path . basename($_FILES['uploadedfile']['name'],".zip") . "/",0777);
chmod($target_path . basename($_FILES['uploadedfile']['name'],".zip") . "/",0777);
$target_path =$target_path . basename($_FILES['uploadedfile']['name'],".zip") . "/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
echo "<br/>Path is: $target_path<br/>";
echo "<br/>Command is: unzip $target_path<br/>";
shell_exec("chmod 777 $target_path");
shell_exec("unzip $target_path");
?>
答案 0 :(得分:1)
尝试使用此代码。我认为这段代码对你有帮助.... 注意:ZipArchive类支持PHP 5&gt; = 5.2.0版本。请检查您的PHP版本。 在根文件夹中创建一个extracthere文件夹。
$path = 'New folder.zip';
$zip = new ZipArchive;
$f = 0;
if ($zip->open($path) === true)
{
for($i = 0; $i < $zip->numFiles; $i++)
{
if($zip->extractTo('extracthere/', array($zip->getNameIndex($i))))
{
$f = 1;
}
else
{
$f = 0;
}
}
$zip->close();
}
if($f==0)
{
echo 'Prolem';
}
else
{
echo 'Done';
}
答案 1 :(得分:0)
我找到了使用终端SH命令在一行中执行2个脚本命令的解决方案
exec( "sh -c \"cd " . $path . " && unzip " . $path . $filename . " -d " . $folder . "\"" );
生成以下内容:
sh -c "cd /var/www/project-X/wp-content/uploads/raw/ && unzip /var/www/project-x/wp-content/uploads/raw/filename.zip -d folderName"
这对我来说很完美