【问题标题】:Download file to website using php [duplicate]使用php将文件下载到网站[重复]
【发布时间】:2015-02-14 18:58:41
【问题描述】:

我有一个带有 URL 的文件!

$url = "http://www.example.com/aa.txt";

我想下载此文件并将其保存到我网站上的路径

这是我的网站(在线)

$path = "server/username/";

我希望将 $url 文件保存到 $path ,,

我试试这个

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
?>

当我测试代码时,它使文件下载到我的电脑而不是我的网站

【问题讨论】:

    标签: php


    【解决方案1】:

    你可以使用file_put_contents();

    在此处查看手册:http://php.net/manual/en/function.file-put-contents.php

    这里有一个不同的实现:http://www.finalwebsites.com/forums/topic/php-file-download

    【讨论】:

    • 我不想阅读文件里面的内容,我想把文件 xx.txt 下载到我的服务器上,比如 www.xx.com/xx.txt
    • @AhmedSafadi 关注这个帖子:serverfault.com/questions/316814/…
    • 下载到另一台服务器是自相矛盾的。您指的是“上传”吗?
    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多