douanye8442 2015-09-21 15:13
浏览 147
已采纳

Linux PHP ExtractTo返回整个路径而不是文件结构

I am pulling my hair out over here. I have spent the last week trying to figure out why the ZipArchive extractTo method behaves differently on linux than on our test server (WAMP).

Below is the most basic example of the problem. I simply need to extract a zip that has the following structure:

my-zip-file.zip
-username01
    --filename01.txt
    -images.zip
        --image01.png
    -songs.zip
        --song01.wav
-username02
    --filename01.txt
    -images.zip
       --image01.png
    -songs.zip
       --song01.wav

The following code will extract the root zip file and keep the structure on my WAMP server. I do not need to worry about extracting the subfolders yet.

<?php
if(isset($_FILES["zip_file"]["name"])) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$errors = array();  

$name = explode(".", $filename);

$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
    $errors[] = "The file you are trying to upload is not a .zip file. Please try again.";
}

$zip = new ZipArchive();

if($zip->open($source) === FALSE)
{
    $errors[]= "Failed to open zip file.";
}

if(empty($errors))
{
    $zip->extractTo("./uploads");
    $zip->close();
    $errors[] = "Zip file successfully extracted! <br />";
}

}
?>

The output from the script above on WAMP extracts it correctly (keeping the file structure).

When I run this on our live server the output looks like this:

--username01\filename01.txt
--username01\images.zip
--username01\songs.zip
--username02\filename01.txt
--username02\images.zip
--username02\songs.zip

I cannot figure out why it behaves differently on the live server. Any help will be GREATLY appreciated!

  • 写回答

1条回答 默认 最新

  • dongzhanjuan5141 2015-09-21 16:44
    关注

    To fix the file paths you can iterate over all extracted files and move them.

    Supposing inside your loop over all extracted files you have a variable $source containing the file path (e.g. username01\filename01.txt) you can do the following:

    // Get a string with the correct file path
    $target = str_replace('\\', '/', $source);
    
    // Create the directory structure to hold the new file
    $dir = dirname($target);
    if (!is_dir($dir)) {
        mkdir($dir, 0777, true);
    }
    
    // Move the file to the correct path.
    rename($source, $target);
    

    Edit

    You should check for a backslash in the file name before executing the logic above. With the iterator, your code should look something like this:

    // Assuming the same directory in your code sample.
    $dir = new DirectoryIterator('./uploads');
    foreach ($dir as $fileinfo) {
        if (
            $fileinfo->isFile() 
            && strpos($fileinfo->getFilename(), '\\') !== false // Checking for a backslash
        ) {
            $source = $fileinfo->getPathname();
    
            // Do the magic, A.K.A. paste the code above
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码