繁体   English   中英

PHP文件下载而不保存到我的目录?

[英]PHP file Download without saving to my directory?

我必须创建一个文本文件并使其可下载,而无需单击按钮保存到我的目录中。 下面我提到了生成文件的文件代码。

<?php
    $data = "ffff dfjdhjf  jhfjhf f f hlm hoejrherueirybgb,nvbd;ihrtmrn.";
    $filename = "SU_Project_Startup.txt";
    $fh = fopen($filename, "w+");
    fwrite($fh, $data, strlen($data));
    fclose($fh);

    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    ob_end_flush();
    echo $data;
    exit();
?>

您能告诉我在这段代码中我还要做哪些更改吗?

提前致谢

试试这个标题:

header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/json"); // here is your type
header("Content-Transfer-Encoding: binary");

这是为网址拍照的示例

您可以尝试或将其投入使用

 <?php
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="File-name.jpg"');
    $fileurl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/c/cc/Ramses_II_at_Kadesh.jpg/120px-Ramses_II_at_Kadesh.jpg';
    $data =  file_get_contents($fileurl);
    echo $data;
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM