Get the App
SLTechnology News&Howtos  ›  Development  › 

What to do if the php file cannot be opened after download

Shulou Source: shulou.com Published: 2022-06-01 03:29:58 09月22日 Update

This article mainly explains the "php file download can not be opened after how to deal with", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "php file download can not open how to deal with" it!

The file opens and shows the problem of corruption after PHP downloads the picture.

Use php to write a picture download method, the test found that the size of the downloaded pictures are all right, but can not open the file.

The solution is as follows:

First open the file to download the code and add

Ob_clean (); flush ()

Download the picture code completely

If (isset ($_ GET ["action"]) & & $_ GET ["action"] = "download") {if ($_ GET ["file"]) {$fileinfo = pathinfo ($_ GET ["file"]); header ("Content-type: application/x-". $fileinfo ["extension"]); header ("Content-Disposition: attachment; filename=favicon.ico"); ob_clean (); flush (); readfile ($_ GET ["file"]); exit ();}}

Content extension:

Php downloads the excel file

1. Do not output any non-file information, such as echo log information, during the download process. Otherwise, the downloaded file cannot be opened, indicating that the format is incorrect or the file is corrupted.

2. The output excel format must be kept with the suffix name, otherwise it will also prompt the format error or the file is corrupted.

The code is as follows:

If (file_exists (CACHE_PATH. $file_name) {/ / $this- > logger- > error ("file realpath:" .realpath (CACHE_PATH. $file_name); header ("Pragma: public"); header ("Expires: 0"); header ("Content-Encoding: none"); header ("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header ("Cache-Control: public"); header ("Content-Type: application/vnd.ms-excel"); header ("Content-Description: File Transfer") Header ("Content-Disposition: attachment; filename=". $file_name); header ("Content-Transfer-Encoding: binary"); header ("Content-Length:". Filesize (CACHE_PATH. $file_name); readfile (CACHE_PATH. $file_name);} else {$this- > logger- > error ("export model:". $id. " Error: no production file "); echo";} Thank you for your reading, the above is the content of "php file can not be opened after download". After the study of this article, I believe you can not open the php file after downloading how to deal with this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: File file download picture code content format error problem learning information method prompt output suffix picture download size that is ideas situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MariaDB Linux MySQL vpn