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

时间:2012-10-20 08:00:24

标签: php file fwrite

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

<?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();
?>

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

提前致谢

2 个答案:

答案 0 :(得分:1)

试试这个标题:

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");

答案 1 :(得分:0)

这是拍摄网址

的示例

你可以尝试,或将其付诸实践

 <?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;
?>
相关问题