开发者

Can't unzip with PHP?

I'm having trouble using PHP to unzip an uploaded file. I can upload fine, the .zip gets copied to where its supposed to, but it does not unzip. The directory and zip are being properly chmod'd to 777. The apache error logs just show "Error: Can't create [whateverfile]" for each file in the .zip. Any idea why this happens? Appreciate it! Please tell me if this belongs over at stackoverflow, but it seems to be an apache/os/somethingelse problem. Code follows:

<?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开发者_StackOverflow中文版'], $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");


?>


Try with this code. I think this code is help you.... Note : ZipArchive class support PHP 5 >= 5.2.0 version. Please check your php version. Make one extracthere folder in your root folder.

    $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';
    }


I found the solution using the terminal SH command to execute 2 script commands in one line

 exec( "sh -c \"cd " . $path . " && unzip " . $path  . $filename . " -d " . $folder . "\"" );

which generates something along the line of :

sh -c "cd /var/www/project-X/wp-content/uploads/raw/ && unzip /var/www/project-x/wp-content/uploads/raw/filename.zip -d folderName"

This works perfectly for me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜