Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php传输zip文件会产生0字节_Php_Curl_Ftp - Fatal编程技术网

使用php传输zip文件会产生0字节

使用php传输zip文件会产生0字节,php,curl,ftp,Php,Curl,Ftp,我已经为此工作了好几天,我尝试了许多不同的方法。我现在拥有的内容至少开始传输.zip文件,但是,其中没有任何内容,只有文件名。 我将此连接到文件所在的服务器(服务器a): <?php header('Content-Transfer-Encoding: binary'); header('Content-Length: '.filesize($file)); header('Content-Type: application/zip'); ini_set('

我已经为此工作了好几天,我尝试了许多不同的方法。我现在拥有的内容至少开始传输
.zip
文件,但是,其中没有任何内容,只有文件名。
我将此连接到文件所在的服务器(服务器a):

<?php
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file));
    header('Content-Type: application/zip');
    ini_set('display_errors',"1");
    require_once('ftp.php');

    // set up basic connection
    $conn_id = ftp_ssl_connect($ftp_server);//ftp_connect

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

    // check connection
    if ((! $conn_id ) || (! $login_result )) {
        echo "FTP connection has failed!" ;
        exit;
    } else {
        echo "Connected for user $ftp_user_name" ;
        echo "<br/>";
    }
?>

这将把文件名放在远程文件夹(服务器b)中,但其中没有任何内容,大小为0字节。如何获取整个文件的传输,以便提取并使用信息?

我可能遗漏了一些内容,但为什么要建立FTP连接,然后使用CURL获取文件?你不能用FTP抓取文件吗?那只是从代码传输到SO。“我现在就要修复它了。”andrewsi我以前试过,但什么都不管用,所以我决定改用curl,至少我现在在服务器(服务器b)上得到了以前没有的东西。我保留了连接,因为它需要密码。如果需要,我可以发布ftp.php。如果你是通过CURL获取文件的,那么你根本不需要ftp,当然?即使没有ftp连接,我仍然有一个0字节的文件。
// using curl to get file
$url = $file;
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);

curl_close($ch);