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

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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失