First, both file_put_contents and fwrite can be downloaded
Look at the introduction in the official manual, using fopen and fwrite to write 100,000 data is 1-4 times faster than file_put_contents, so I recommend using fopen and fwrite to download files
file_put_contents() for 1,000,000 writes - average of 3 benchmarks:
real 0m3.932s user 0m2.487s sys 0m1.437s
fopen() fwrite() for 1,000,000 writes, fclose() - average of 3
benchmarks:real 0m2.265s user 0m1.819s sys 0m0.445s
Download file demo:
Take batch download of MP3 files as an example, my file link is as follows : http://vcast-resource.cdn.bcebos.com/vcast-resource/a22acf58-b637-4ce9-9eec-01a31775979e.mp3
<?php
$conn = new mysqli('127.0.0.1', 'root', 'root', 'Access2016');
$conn->set_charset('utf8mb4');
$sql = "SELECT `mp3` FROM `mp3` WHERE 1";
$result = $conn->query($sql);
$result = $result->fetch_all(MYSQLI_ASSOC);
foreach ($result as $mp3){
down_file($mp3['mp3']);
}
function down_file($url, $folder = "./") {
set_time_limit (24 * 60 * 60); // 设置超时时间
$destination_folder = $folder . '/'; // 文件下载保存目录,默认为当前文件目录
if (!is_dir($destination_folder)) {
// 判断目录是否存在
mkdirs($destination_folder); // 如果没有就建立目录
}
$newfname = $destination_folder . basename($url); // 取得文件的名称
$file = fopen ($url, "rb"); // 远程下载文件,二进制模式
if ($file) {
// 如果下载成功
$newf = fopen ($newfname, "wb"); // 远在文件文件
if ($newf) // 如果文件保存成功
while (!feof($file)) {
// 判断附件写入是否完整
fwrite($newf, fread($file, 1024 * 8), 1024 * 8); // 没有写完就继续
}
}
if ($file) {
fclose($file); // 关闭远程文件
}
if ($newf) {
fclose($newf); // 关闭本地文件
}
return true;
}
function mkdirs($path , $mode = "0755") {
if (!is_dir($path)) {
// 判断目录是否存在
mkdirs(dirname($path), $mode); // 循环建立目录
mkdir($path, $mode); // 建立目录
}
return true;
}
After testing and downloading more than 800 mp3 files, the speed is still fast, more than 3 times faster than file_put_contents
result: