file_put_contents无法获取整个文件

时间:2015-06-16 13:45:49

标签: php hostmonster

我使用hostmonster作为托管服务提供商。

我写了下面的代码。

$mp4_url = 'http://www.w3schools.com/html/mov_bbb.mp4';
$filename = 'sample.mp4';

file_put_contents( $filename , fopen($mp4_url, 'r' ) );

我看到了奇怪的行为。有时它可以工作,但有时它只复制少量的文件,有时文件的大小是100kb,有时600kb有时会复制整个文件。

请建议我如何将任何mp4文件从任何服务器复制到我们的服务器。

我们要复制大文件,大小可以是600MB或1GB。

2 个答案:

答案 0 :(得分:2)

试试copy()。这将完成你的工作:

$file = 'http://www.w3schools.com/html/mov_bbb.mp4';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/sample.mp4';

if ( copy($file, $newfile) ) {
    echo "Copy success!";
}else{
    echo "Copy failed.";
}

答案 1 :(得分:1)

使用curl而不是file_put_contents

$url='http://www.w3schools.com/html/mov_bbb.mp4';
$saveto='path';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$raw = curl_exec($ch);
curl_close($ch);
if (file_exists($saveto)) {
    unlink($saveto);
}
$fp = fopen($saveto, 'x');
fwrite($fp, $raw);
fclose($fp);