使用php和任何方法将大文件下载到服务器

使用php和任何方法将大文件下载到服务器,php,curl,ftp,download,Php,Curl,Ftp,Download,第一次尝试: $url = 'http://site.com/archive.tar'; file_put_contents("archive.tar", fopen($url, 'r')); $fh = fopen(basename($url), "wb"); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fh); curl_exec($ch); curl_close($ch); $fp = fopen (dirname(_

第一次尝试:

$url = 'http://site.com/archive.tar';

file_put_contents("archive.tar", fopen($url, 'r'));
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
$fp = fopen (dirname(__FILE__) . '/archive.tar', 'w+');//This is the file where we save the    information
$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
function download($file_source, $file_target) {
    $rh = fopen($file_source, 'rb');
    $wh = fopen($file_target, 'w+b');
    if (!$rh || !$wh) {
        return false;
    }

    while (!feof($rh)) {
        if (fwrite($wh, fread($rh, 4096)) === FALSE) {
            return false;
        }
        echo ' ';
        flush();
    }

    fclose($rh);
    fclose($wh);

    return true;
}


set_time_limit(0);

var_dump(download($url, 'archive.tar')); // Returns bool(false)
我已经使用了这个代码:(感谢)

该归档文件是一个小于
2GB
的大文件

但几秒钟后,我发现出现了一个问题


第二次尝试:

$url = 'http://site.com/archive.tar';

file_put_contents("archive.tar", fopen($url, 'r'));
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
$fp = fopen (dirname(__FILE__) . '/archive.tar', 'w+');//This is the file where we save the    information
$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
function download($file_source, $file_target) {
    $rh = fopen($file_source, 'rb');
    $wh = fopen($file_target, 'w+b');
    if (!$rh || !$wh) {
        return false;
    }

    while (!feof($rh)) {
        if (fwrite($wh, fread($rh, 4096)) === FALSE) {
            return false;
        }
        echo ' ';
        flush();
    }

    fclose($rh);
    fclose($wh);

    return true;
}


set_time_limit(0);

var_dump(download($url, 'archive.tar')); // Returns bool(false)

第三次尝试:

$url = 'http://site.com/archive.tar';

file_put_contents("archive.tar", fopen($url, 'r'));
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
$fp = fopen (dirname(__FILE__) . '/archive.tar', 'w+');//This is the file where we save the    information
$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
function download($file_source, $file_target) {
    $rh = fopen($file_source, 'rb');
    $wh = fopen($file_target, 'w+b');
    if (!$rh || !$wh) {
        return false;
    }

    while (!feof($rh)) {
        if (fwrite($wh, fread($rh, 4096)) === FALSE) {
            return false;
        }
        echo ' ';
        flush();
    }

    fclose($rh);
    fclose($wh);

    return true;
}


set_time_limit(0);

var_dump(download($url, 'archive.tar')); // Returns bool(false)

第四次尝试:

$url = 'http://site.com/archive.tar';

file_put_contents("archive.tar", fopen($url, 'r'));
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
$fp = fopen (dirname(__FILE__) . '/archive.tar', 'w+');//This is the file where we save the    information
$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
function download($file_source, $file_target) {
    $rh = fopen($file_source, 'rb');
    $wh = fopen($file_target, 'w+b');
    if (!$rh || !$wh) {
        return false;
    }

    while (!feof($rh)) {
        if (fwrite($wh, fread($rh, 4096)) === FALSE) {
            return false;
        }
        echo ' ';
        flush();
    }

    fclose($rh);
    fclose($wh);

    return true;
}


set_time_limit(0);

var_dump(download($url, 'archive.tar')); // Returns bool(false)

第五名:

$url = 'http://site.com/archive.tar';

file_put_contents("archive.tar", fopen($url, 'r'));
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
$fp = fopen (dirname(__FILE__) . '/archive.tar', 'w+');//This is the file where we save the    information
$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
function download($file_source, $file_target) {
    $rh = fopen($file_source, 'rb');
    $wh = fopen($file_target, 'w+b');
    if (!$rh || !$wh) {
        return false;
    }

    while (!feof($rh)) {
        if (fwrite($wh, fread($rh, 4096)) === FALSE) {
            return false;
        }
        echo ' ';
        flush();
    }

    fclose($rh);
    fclose($wh);

    return true;
}


set_time_limit(0);

var_dump(download($url, 'archive.tar')); // Returns bool(false)


我应该感谢SO中的许多人使用了他们的代码,但仍然没有运气。

试试。

你检查了你的
php.ini
文件以确保你上传的文件配置正确吗?我生活在一个完美配置的
cpanel
共享主机中(很多其他正常脚本都在运行,没有任何问题)。我到底应该检查什么?我可以
phpinfo()
。查看本文中的建议。注意-您可以更改一些
.htaccess
参数!谢谢,但即使这样也没用。它等待了更多的回应,但最后没有什么不同。