Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文件下载而不保存到我的目录?_Php_File_Fwrite - Fatal编程技术网

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

PHP文件下载而不保存到我的目录?,php,file,fwrite,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));

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

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

这是一个拍摄url图片的示例

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

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

您很可能希望浏览器询问保存文件的位置,而不是盲目保存文件?这实际上是一个浏览器配置的例子,在服务器端你们无能为力。这意味着你们很有可能正在使用Chrome。我想你们必须删除这个部分:
$fh=fopen($filename,“w+”);fwrite($fh,$data,strlen($data));fclose($fh)@毫无意义的死亡。。你想让他们下载一个空文件?(或者更好,一个不存在的文件?)@Daedalus,该部分将数据写入硬盘,
echo$data向用户发送数据!我尝试了这个标题,但它仍然在我的目录中创建了一个文件。我复制了相同的代码,它的图像效果很好。假设现在我想创建文本文件,那么我需要更改什么?您是否尝试过更改扩展名为txt“filename.txt”的文件名