【问题标题】:Download file from url as attachment从 url 下载文件作为附件
【发布时间】:2014-11-11 11:27:35
【问题描述】:

我无法设置正确的标题以从 URL 下载文件。 下面的代码只是在浏览器中显示 URL 的内容,而不是下载。

$file_name = "image1.jpg";
$content_type = "image/jpeg";
$url = "http://ctan.imsc.res.in/macros/latex/contrib/incgraph/example.jpg";

header('Pragma: public');   // required
header('Expires: 0');       // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
header('Cache-Control: private',false);
header("Content-type: ".$content_type);
header("Cache-Control: no-store, no-cache");
header("Content-disposition: attachment; filename=".$file_name);
header("Location: ".$url);
exit(0);

提前致谢。

【问题讨论】:

  • 你需要获取example.jpg的内容并通过浏览器。 Location: 标头只会重定向用户。
  • 感谢@BenM 的回复。但我不必在我的服务器中下载图像。
  • @GuruGGulab — 你错了。如果您想向其中添加自己的 HTTP 标头,您可以这样做。
  • 我需要直接从 URL 下载文件,而不是在浏览器中显示。如果我的代码有误,请建议我另一种解决方案。我的要求是允许用户下载外部 URL 文件而不是在浏览器中显示。

标签: php attachment force-download


【解决方案1】:
$originalPath = $_REQUEST['path'];  //path with filename
$fileName = $_REQUEST['fileName'];  // filename for downloaded file name    
if (file_exists($originalPath)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($fileName));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($originalPath));
    ob_clean();
    flush();
    readfile($originalPath);
    exit;
} else {
    echo 'FILE_NOT_FOUND';
}

【讨论】:

  • 我只有 URL 而不是文件的绝对路径。我必须使用 URL 而不是文件的实际位置。 URL 不是来自我的服务器的外部源,我没有该文件的实际路径。
【解决方案2】:

使用此代码,您可以将图像保存在我们的服务器中,您可以使用我的上层代码下载图像。

file_put_contents("Tmpfile.jpg", fopen("http://ctan.imsc.res.in/macros/latex/contrib/incgraph/example.jpg", 'r'));

【讨论】:

    【解决方案3】:

    此代码可以从 url 下载:

    set_time_limit(0);
    
    //File to save the contents to
    $fp = fopen ('file.type', 'w+');
    
    $url = "http://yoursite.come/file.type";
    
    //Here is the file we are downloading, replace spaces with %20
    $ch = curl_init(str_replace(" ","%20",$url));
    
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    
    //give curl the file pointer so that it can write to it
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
    $data = curl_exec($ch);//get curl response
    
    //done
    curl_close($ch);
    ?>
    

    【讨论】:

    • 以上代码会将文件下载到我的服务器中,我不必将文件下载到我的服务器空间中。我必须将该文件直接提供给用户,以便在他/她的个人电脑/笔记本电脑中下载。
    【解决方案4】:

    从 url(in list) 下载文件并使用 python 以 url 的姓氏保存文件

    import urllib.request as urllib2
    import re,os
    for url in open('C:\\Users\\SRINU\\Desktop\\Election_pdfs_urls\\33.txt'):
      possible_urls = re.findall(r'(https?://[^\s]+)', url)
      for links in possible_urls:
        response = urllib2.urlopen(links.format(url.encode('utf-8')))
        filename_w_ext = os.path.basename(links)
        file = open(filename_w_ext, 'wb')
        file.write(response.read())
        file.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      相关资源
      最近更新 更多