生成可通过php下载的文件时,不会下载内容

时间:2017-06-12 03:34:11

标签: php download

我正在创建一个生成带有文本的文本文件的内容,但每当我下载文件时,我得到的只是<h文件内部,并且没有内容。这是我的代码:

$filename = 'download.txt';
$content = "Hi";
$f = fopen($filename, 'w');
fwrite($f, $content);
fclose($f);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");

readfile($filename);

4 个答案:

答案 0 :(得分:0)

应该filesize(&#34; $ filename&#34;)是filesize($ filename)吗?

答案 1 :(得分:0)

试试这个

header('Content-disposition: attachment; filename=gen.txt');
header('Content-type: text/plain');

答案 2 :(得分:0)

尝试指定要保存文件的目录,如import Foundation import UIKit class PhotoController:UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate{ @IBOutlet weak var myImageView: UIImageView! @IBAction func PhotoSelect(_ sender: Any) { } @IBAction func PhotoSend(_ sender: Any) { let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary self.present(myPickerController, animated: true, completion: nil) } private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage self.dismiss(animated: true, completion: nil) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //myImageUploadRequest() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

答案 3 :(得分:0)

我复制了你的代码并将其粘贴到一个文件中并运行它并创建了'download.txt'文件并包含“Hi”

所以这不是你的代码,它可能是你的网络服务器设置。