【问题标题】:Directory structure when unzipping file on the server with PHP使用 PHP 在服务器上解压缩文件时的目录结构
【发布时间】:2018-12-02 15:21:25
【问题描述】:

当用户注册时,我正在创建一个站点我解压缩了一个文件,该文件包含用户个人资料的当前文件夹结构。

文件夹结构以 zip /u/folders.zip 的形式保存在 users 目录中,我以为我正在创建以下结构:

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

它正在创造的是这样的:

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

处理文件:

<?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;


?>

我需要解压用户文件夹根目录下 zip 中的文件夹。我很确定如果 sn-p 是 copy() 的问题。

感谢您的帮助!

【问题讨论】:

  • 根据您的 PDO 查询,我将假设您也错误地散列了您的密码。 PDO 查询应该使用准备好的语句,并参数化。这与问题无关,因此脚本的前 15 行也可以删除,最后 11 行也是如此。
  • folders.zip文件中文件夹的布局是什么?
  • User3783243 - 感谢您的详细帮助。我实际上不再需要密码了。
  • Hasta - 这是一个 index.php 3 个文件夹,其中包含 index.php 文件和一个文件夹,其中包含 3 个空文件夹。

标签: php ziparchive


【解决方案1】:

尝试改变这个:

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

用这个:

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

请阅读extractTo() php 文档页面上的user contributed note 以获得更好的解释。

【讨论】:

  • 我想通了。更改 zip 后,我已将原始文件夹结构压缩到名为“文件夹”的根目录中,一切正常。因此,不要压缩一个包含 4 个文件夹和一个 php 文件的文件夹。我压缩了 4 个文件夹和 php 文件。它总是最简单的事情。感谢您的帮助!
  • 我同意。有时我也是,错过了最简单的解决方案;)
猜你喜欢
  • 2011-07-10
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 2023-03-16
  • 2021-10-05
  • 2015-12-26
相关资源
最近更新 更多