当前位置:ag捕鱼王app官网 > 学无止境 > 编程语言 > php >
在 php 中使用 curl 下载文件
作者:迹忆客
最近更新:2023/03/29
浏览次数:
-output
或 -o
命令用于在 php 中下载带有 curl 的文件。
我们可以通过提供文件的 url 来下载带有 curl 的文件。curl 库在 php 中有多种用途。
使用 curl
从 php 中的给定 url 下载文件
首先,确保在你的 php 中启用了 curl。你可以通过运行 phpinfo()
并从 php.ini
文件启用它来检查它。
我们正在尝试将文件从本地服务器下载到本地服务器本身。
php
// file download information
set_time_limit(0); // if the file is large set the timeout.
$to_download = "http://localhost/delftstack.jpg"; // the target url from which the file will be downloaded
$downloaded = "newdelftstack.jpg"; // downloaded file url
// file handling
$new_file = fopen($downloaded, "w") or die("cannot open" . $downloaded);
// setting the curl operations
$cd = curl_init();
curl_setopt($cd, curlopt_url, $to_download);
curl_setopt($cd, curlopt_file, $new_file);
curl_setopt($cd, curlopt_timeout, 30); // timeout is 30 seconds, to download the large files you may need to increase the timeout limit.
// running curl to download file
curl_exec($cd);
if (curl_errno($cd)) {
echo "the curl error is : " . curl_error($cd);
} else {
$status = curl_getinfo($cd);
echo $status["http_code"] == 200 ? "the file is downloaded" : "the error code is : " . $status["http_code"] ;
// the http status 200 means everything is going well. the error codes can be 401, 403 or 404.
}
// close and finalize the operations.
curl_close($cd);
fclose($new_file);
?>
输出:
输出显示代码可以从给定的 url 下载文件。要下载大文件,我们可以增加超时限制。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
如何在 php 中获取时间差的分钟数
发布时间:2023/03/29 浏览次数:204 分类:php
-
本文介绍了如何在 php 中获取时间差的分钟数,包括 date_diff()函数和数学公式。它包括 date_diff()函数和数学公式。
发布时间:2023/03/29 浏览次数:156 分类:php
-
本教程演示了如何将用户从页面重定向到 php 中的其他页面