Php downloads the image to a local device. the image cannot be opened. how can this problem be solved?

Source: Internet
Author: User
Php downloads the image to the local device. the image cannot be opened. environment: wampserveer database encoding: Unified utf8_general_ciphp file encoding: UTF-8 when the function is not connected to the database, download is normal. I uploaded a unified image using php and then downloaded it again. I suspect that the file encoding problem occurs after the image is uploaded to mysql. However, this php has never been involved in downloading the image locally and the image cannot be opened.
Environment: wampserveer
Database encoding: Unified utf8_general_ci
Php file encoding: UTF-8
The function is downloaded normally when it is not connected to the database.
I uploaded a unified image using php and then downloaded it.
I suspect it is the file encoding problem after uploading to mysql, but it has never been involved in this aspect. I would like to ask how to solve this problem.

PHP code
  Function dl_file ($ file) {// This is the function for downloading files. $ file is the file path. // First, see if the file exists if (! Is_file ($ file) {die ("404 File not found!");} // Gather relevent info about file $ len = filesize ($ file); $ filename = basename ($ file ); $ file_extension = strtolower (substr (strrchr ($ filename ,". "), 1); // This will set the Content-Type to the appropriate setting for the file switch ($ file_extension) {case" pdf ": $ ctype = "application/pdf"; break; case "exe": $ ctype = "application/octet-stream"; break; case "zip ": $ ctype = "application/zip"; break; case "doc": $ ctype = "application/msword"; break; case "xls": $ ctype = "application/vnd. ms-excel "; break; case" ppt ": $ ctype =" application/vnd. ms-powerpoint "; break; case" gif ": $ ctype =" image/gif "; break; case" png ": $ ctype =" image/png "; break; case "jpeg": case "jpg": $ ctype = "image/jpg"; break; case "mp3": $ ctype = "audio/mpeg"; break; case "wav": $ ctype = "audio/x-wav"; break; case "mpeg": case "mpg": case "mpe ": $ ctype = "video/mpeg"; break; case "mov": $ ctype = "video/quicktime"; break; case "avi ": $ ctype = "video/x-msvideo"; break; // The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files) case "php ": case "htm": case "html": case "txt": die ("Cannot be used for ". $ file_extension." files!"); Break; default: $ ctype =" application/force-download ";} // Begin writing headers header (" Pragma: public "); header (" Expires: 0 "); header (" Cache-Control: must-rev alidate, post-check = 0, pre-check = 0 "); header (" Cache-Control: public "); header (" Content-Description: File Transfer "); // Use the switch-generated Content-Type header (" Content-Type: $ ctype "); // Force the download $ header = "Content-Disposition: attachment; filename = ". $ filename. ";"; header ($ header); header ("Content-Transfer-Encoding: binary"); header ("Content-Length :". $ len); @ readfile ($ file); exit ;}



------ Solution --------------------
1. check whether the content of the input parameter $ file is normal.
2. check whether all involved files have bom headers.
------ Solution --------------------
Obtain the extension. pathinfo is recommended.

------ Solution --------------------
Discussion
I store the image path, and I have echo in the path. No problem. how can I view the bom header?

------ Solution --------------------
It must be about database encoding, because it is normal when you say you don't need a database,
SET Names UTF-8 when accessing the database again;
All formats are unified;
We recommend that you check the image file name and size to see if it is a normal file. make sure that the image does not exist in the database in binary format.
Whether the image is normal on the server and whether the transmission process is encrypted
------ Solution --------------------
First, let's take a look at the difference between the abnormal image and the normal image. Then, find out the cause of the exception...

If the abnormal image is damaged only at the beginning or end, it is a problem with the php code itself, where the exception data is truncated or added;
If the entire image content is damaged, the encoding problem is basically solved.

In addition,... are images stored in the database? Messy...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.