1

我正在使用 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。

4

2 回答 2

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.";
}
于 2015-06-16T13:51:34.650 回答
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);
于 2015-06-16T13:52:28.813 回答