dongxia19772008 2018-12-02 15:21
浏览 182

使用PHP解压缩服务器上的文件时的目录结构

I have a site I am creating when a user signs up I unzip a file that holds the current folder structure for the users profile.

The folder structure is housed in the users directory as a zip /u/folders.zip what I thought I was doing is creating the following structure:

-u
--folders.zip
--newamazingusername
---index.php
---folder1
---folder2
----subfolder1
----subfolder2
---folder3

What it is creating instead is this:

-u
--folders.zip
--newamazingusername
---folders
----index.php
----folder1
----folder2
-----subfolder1
-----subfolder2
----folder3

Processing file:

<?php
require_once( "../../assets/inc/connection.php" );

if ( !empty($_GET['email']) && !empty($_GET['username']) ):
    $get_username = $_GET['username'];
    $get_email = $_GET['email'];

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $hash = **[REDACTED CODE] Assume I am Hashing my password properly**
        $sql = "UPDATE users SET username='" . $get_username . "' WHERE email='" . $get_email . "'";
        // use exec() because no results are returned
        $conn->exec($sql);

        $zip = new ZipArchive;
        $fileLocation = $_SERVER['DOCUMENT_ROOT'] . '/u/folders.zip';
        $newfile = $_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username . '.zip';

        if (!copy($fileLocation, $newfile)) {
            echo "failed to copy";
            header('Location: http://' . $_SERVER['HTTP_HOST'] . '?error=foldercopyfail');
            exit();
        }
        $res = $zip->open($newfile);
        if ($res === TRUE) {
            $zip->extractTo($_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username);
            $zip->close();

            header('Location: http://' . $_SERVER['HTTP_HOST'] . '/u/' . $get_username );
        } else {
            echo 'doh!';
        }
        unlink($newfile);

        exit();
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }

else :
    header('Location: http://' . $_SERVER['HTTP_HOST'] . '?error=noaccess');
endif;


?>

I need to unzip the folders in the zip at the root of the user folder. I am pretty sure its an issue with the copy() if snippet.

Thank you for the help!

  • 写回答

1条回答 默认 最新

  • duanliusong6395 2018-12-02 16:22
    关注

    Try change this :

    $res = $zip->open($newfile);
    if ($res === TRUE) {
        $zip->extractTo($_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username);
        $zip->close();
    
        header('Location: http://' . $_SERVER['HTTP_HOST'] . '/u/' . $get_username );
    } else {
        echo 'doh!';
    }
    

    with this :

    $res = $zip->open($newfile);
    if ($res === TRUE) {
        for($i = 0; $i < $zip->numFiles; $i++) {
            $filename = $zip->getNameIndex($i);
            $fileinfo = pathinfo($filename);
            copy("zip://".$newfile."#".$filename, $_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username . $fileinfo['basename']);
        }
        $zip->close();
    
        header('Location: http://' . $_SERVER['HTTP_HOST'] . '/u/' . $get_username );
    } else {
        echo 'doh!';
    }
    

    Please read the user contributed note on the php docs page of extractTo() for the better explanation.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么