【发布时间】:2012-03-26 04:51:22
【问题描述】:
我正在寻找一种在 PHP 中执行许多任务的方法
- 从其他服务器获取文件
- 更改文件名和扩展名
- 将新文件下载给最终用户
我更喜欢充当代理服务器类型的方法,但文件下载就可以了
提前致谢
【问题讨论】:
标签: php file-upload download
我正在寻找一种在 PHP 中执行许多任务的方法
我更喜欢充当代理服务器类型的方法,但文件下载就可以了
提前致谢
【问题讨论】:
标签: php file-upload download
试试这个
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path-to-file/a-large-file.zip';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
保存后用你需要的任何名称重命名文件
参考这个
【讨论】:
查看http://www.php.net/manual/en/function.curl-init.php的示例
这会抓取数据并将其直接输出到浏览器、标题和所有内容。
【讨论】:
如果您将 allow_url_fopen 设置为 true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
其他使用 cURL:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
【讨论】:
我使用这样的东西:
<?php
$url = 'http://www.some_url.com/some_file.zip';
$path = '/path-to-your-file/your_filename.your_ext';
function get_some_file($url, $path){
if(!file_exists ( $path )){
$fp = fopen($path, 'w+');
fwrite($fp, file_get_contents($url));
fclose($fp);
}
}
?>
【讨论】: