问题描述
我想制作一个表格,您可以在其中填写FTP登录服务器并获得上载ZIP文件的选项.该脚本的工作方式不同于我要执行UNZIP上传文件的最后一部分(UNZIP文件).有谁知道这是什么问题? TIA
I want to make a form where you can fill FTP login server and get option to upload ZIP file. 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
<?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>
错误
警告:ZipArchive :: close():中的无效或未初始化的Zip对象 第30行的C:\ xampp \ htdocs \ upload.php
Warning: ZipArchive::close(): Invalid or uninitialized Zip object in C:\xampp\htdocs\upload.php on line 30
推荐答案
ZipArchive
不支持URL包装器.
The ZipArchive
does not support URL wrappers.
反正您的代码没有多大意义:
And your code does not make much sense anyway:
-
您首先将
$localfile
作为/htdocs/file.zip
$file = '/htdocs/file.zip';
ftp_fput($conn_id, $file, $fp, FTP_ASCII)
然后您尝试打开/htdocs/file.zip
,因为它是本地文件
And then you try to open /htdocs/file.zip
, as it it were a local file
$zip->open($file);
但是这样的本地文件不存在.
But such local file does not exists.
然后尝试将不存在的文件提取到FTP URL.而且不支持.
And then you try to extract that non existing file to FTP URL. And that's not supported.
请参见 ZipArchive :: open():支持流包装器.它是关于open
的,但是如果open
不支持包装器,则extactTo
也不会(这是一种更难以支持的方式).请参见 cmb的评论:
See 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). See a comment by cmb:
但是,ZipArchive :: open()不是该文件系统的函数 问题.
However, ZipArchive::open() is not a filesystem function for that matter.
所以,实际上,这不是错误,甚至也不是文档中的文档错误. 严格意义上讲.因此,我将更改为功能请求.
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.
-
您想将ZIP文件上传到FTP服务器并在那里解压缩.只是无法在ZIP服务器上解压缩ZIP文件.
您想将本地ZIP提取到FTP服务器.尽管在ZipArchive:extractTo
调用中似乎可以使用URL包装器,但事实并非如此.如上所示.也没有其他方法可以使用PHP中的一些简单的单行代码将本地ZIP文件提取到FTP服务器.
You wanted to extract a local ZIP to FTP server. While it may seem that it's possible with use of URL wrapper in the ZipArchive:extractTo
call, it's not. 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.
您所能做的就是在本地(在Web服务器上)提取ZIP文件.然后将其逐个文件上传到FTP服务器.
All you can do, is to extract the ZIP file locally (on the web server); and then upload it file-by-file to the FTP server.
-
创建一个临时文件夹,然后在其中提取(
ZipArchive::extractTo
)ZIP存档.
将临时文件夹上传到FTP服务器.
Upload the temporary folder to the FTP server.
删除临时文件夹.
还请注意,您使用ASCII模式上传文件. ZIP格式为二进制.通过以ASCII模式上载二进制文件,会损坏它.
Also note that you upload the file using ASCII mode. A ZIP format is binary. By uploading binary file in ASCII mode, you damage it.
ftp_fput($conn_id, $file, $fp, FTP_ASCII);
这篇关于通过PHP上传ZIP文件和UNZIP ftp文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!