简体   繁体   English

通过 PHP 上传 ZIP 文件和解压缩 ftp 文件夹

[英]Upload a ZIP file and UNZIP ftp folder via PHP

I want to make a form where you can fill FTP login server and get option to upload ZIP file.我想制作一个表格,您可以在其中填写 FTP 登录服务器并获取上传 ZIP 文件的选项。 The script works apart from the last part (UNZIP the file) I want to perform UNZIP uploaded file.该脚本与最后一部分(解压缩文件)分开工作,我想执行解压缩上传的文件。 Does anyone know what is the problem?有谁知道是什么问题? TIA TIA

<?php 
if (isset($_POST['Submit'])) {
$ftp_server = $ftp = $_POST['ftp'];
$ftp_user_name = $username = $_POST['username'];
$ftp_user_pass = $password = $_POST['password'];   


 if (!empty($_FILES['upload']['name'])) {
$ch = curl_init();

$file1 = $localfile = $_FILES['upload']['name'];
$fp = fopen($file1, 'r');

$file = '/htdocs/file.zip';
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
    echo "Successfully uploaded ftp://".$username.":".$password."@".$ftp.$file."\n";

    $zip = new ZipArchive;
    $zip->open($file); 
    $zip->extractTo('ftp://'.$username.':'.$password.'@'.$ftp.'/htdocs'); 
    $zip->close();

} else {
    echo "There was a problem while uploading $file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
 }  
}
?>

    <?php if(isset($error)){ echo $error; } ?>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <div>
                <label for="upload">Select file</label>
                <input name="upload" type="file" />
                <br> Ftp Server:
                <br>
                <input type="text" name="ftp" value="<?php if(isset($ftp)){ echo $ftp; } ?>">
                <br> Username:
                <br>
                <input type="text" name="username" value="<?php if(isset($username)){ echo $username; } ?>">
                <br> Password:
                <br>
                <input type="text" name="password" value="<?php if(isset($password)){ echo $password; }else{ echo '123456';} ?>">

                <input type="submit" name="Submit" value="Upload" />
            </div>
        </form>

THE ERROR错误

Successfully uploaded上传成功

ftp:// : @ftp.***.com/htdocs/file.zip Warning: ZipArchive::extractTo(): Invalid or uninitialized Zip object in C:\xampp\htdocs\upload.php on line 29 ftp:// : @ftp.***.com/htdocs/file.zip 警告:ZipArchive::extractTo(): 第 29 行 C:\xampp\htdocs\upload.php 中的 Zip 对象无效或未初始化

Warning: ZipArchive::close(): Invalid or uninitialized Zip object in C:\xampp\htdocs\upload.php on line 30警告:ZipArchive::close(): 第 30 行 C:\xampp\htdocs\upload.php 中的 Zip 对象无效或未初始化

The ZipArchive does not support URL wrappers. ZipArchive不支持 URL 包装器。

And your code does not make much sense anyway:无论如何,您的代码没有多大意义:

  • You first upload $localfile to FTP server as /htdocs/file.zip您首先将$localfile作为/htdocs/file.zip上传到 FTP 服务器

    $file = '/htdocs/file.zip'; ftp_fput($conn_id, $file, $fp, FTP_ASCII)
  • And then you try to open /htdocs/file.zip , as it it were a local file然后你尝试打开/htdocs/file.zip ,因为它是一个本地文件

    $zip->open($file);

    But such local file does not exists.但是这样的本地文件不存在。

  • And then you try to extract that non existing file to FTP URL.然后您尝试将该不存在的文件提取到 FTP URL。 And that's not supported.这不受支持。

    See ZipArchive::open(): support stream wrappers .请参阅ZipArchive::open(): support stream wrappers It's about open , but if open does not support wrappers, the extactTo won't either (it's a way more difficult to support).它是关于open的,但如果open不支持包装器,则extactTo也不会(这是一种更难以支持的方式)。 See a comment by cmb :查看cmb 的评论

    Anyhow, ZipArchive::open() is not supposed to accept any stream wrapper URLs, but only real file paths.无论如何, ZipArchive::open() 不应该接受任何流包装器 URL,而只能接受真实的文件路径。 Its documentation doesn't tell otherwise, and neither does the man page on "Supported Protocols and Wrappers"[1]:它的文档没有说明,“支持的协议和包装器”[1] 的手册页也没有说明:

    | | PHP comes with many built-in wrappers for various URL-style | PHP 带有许多用于各种 URL 样式的内置包装器 | protocols for use with the filesystem functions [...]与文件系统功能一起使用的协议 [...]

    However, ZipArchive::open() is not a filesystem function for that matter.但是, ZipArchive::open() 不是一个文件系统函数。

    So, actually, this is not a bug, not even a documentation bug in the strict sense.所以,实际上,这不是一个错误,甚至不是严格意义上的文档错误。 Therefore I'm changing to feature request.因此,我将更改为功能请求。


As your code is just wrong, its difficult to guess, what you are actually trying to do.由于您的代码是错误的,因此很难猜测您实际上要做什么。 I can imagine these two possibilities.我可以想象这两种可能性。

  • You wanted to upload a ZIP file to FTP server and unzip it there.您想将 ZIP 文件上传到 FTP 服务器并在那里解压缩。 It's simply not possible to unzip a ZIP file on an FTP server .根本不可能在 FTP 服务器上解压缩 ZIP 文件

  • You wanted to extract a local ZIP to FTP server.您想将本地 ZIP 提取到 FTP 服务器。 While it may seem that it's possible with use of URL wrapper in the ZipArchive:extractTo call, it's not.虽然在ZipArchive:extractTo调用中使用 URL 包装器似乎是可能的,但事实并非如此。 As I've shown above.正如我在上面展示的那样。 Nor there is any other way to extract local ZIP file to FTP server with some simple one-liner in PHP.也没有任何其他方法可以使用 PHP 中的一些简单的单行代码将本地 ZIP 文件提取到 FTP 服务器。

    All you can do, is to extract the ZIP file locally (on the web server);您所能做的就是在本地(在 Web 服务器上)提取 ZIP 文件; and then upload it file-by-file to the FTP server.然后将其逐个文件上传到 FTP 服务器。


Also note that you upload the file using ASCII mode.另请注意,您使用 ASCII 模式上传文件。 A ZIP format is binary. ZIP 格式是二进制的。 By uploading binary file in ASCII mode, you damage it.通过以 ASCII 模式上传二进制文件,您会损坏它。

ftp_fput($conn_id, $file, $fp, FTP_ASCII);

Actually none of the FTP clients can extract or unzip archive files on the FTP server because there is no such command in FTP.实际上没有一个 FTP 客户端可以在 FTP 服务器上提取或解压缩存档文件,因为 FTP 中没有这样的命令。 However, net2ftp makes it possible by downloading the archive file from your web server, extracts the files in their server and then upload all the files back to your server.但是,net2ftp 可以通过从您的 Web 服务器下载存档文件,在其服务器中提取文件,然后将所有文件上传回您的服务器来实现。 Hence, you might notice that it takes quite a long time for net2ftp to complete uploading if there are thousands of files in the archive.因此,您可能会注意到,如果存档中有数千个文件,net2ftp 需要很长时间才能完成上传。

You can do this in 3 steps =>您可以通过 3 个步骤完成此操作 =>

Step 1: Upload your zip file (Uploaded.zip) to the ftp server.第 1 步:将您的 zip 文件 (Uploaded.zip) 上传到 ftp 服务器。 Code => ftp_put($ftp, $remote_file, "Upload,zip", FTP_BINARY) ;代码 => ftp_put($ftp, $remote_file, "Upload,zip", FTP_BINARY) ; Step 2: Upload 1 more file to your ftp server let's name it Extractor.php which has the code to extract the zip file.第 2 步:再上传 1 个文件到您的 ftp 服务器,我们将其命名为 Extractor.php,其中包含提取 zip 文件的代码。 Code =>代码 =>

$zip = new ZipArchive;
$res = $zip->open('uploaded.zip');
if ($res === TRUE) {
    $zip->extractTo('extract/path/');
    $zip->close();
    echo "Extracted" ;
    unlink("uploaded.zip");
} else {
    echo "Zip Corrupted" ;
}

Step 3: Call the Extractor.php using curl just after uploading both files.第 3 步:在上传两个文件后使用 curl 调用 Extractor.php。 Code =>代码 =>

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url/where/it/will/be/displayed/Extractor.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output) ;

Hope, it will be helpful for you as well :)希望,它也会对你有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM