Php file download error

Source: Internet
Author: User
Php file download_file ($ filename) {belowtoprovidethedownloadif (file_exists ($ filename) {php file download error
Write a program for downloading files.
PHP code
  function download_file($filename){     //below to provide the download                    if (file_exists($filename)) {                                                 $file = fopen($filename);                    header('Content-Description: File Transfer');                    header('Content-Type: application/octet-stream');                    header('Content-Disposition: attachment; filename=' . basename($filename));                    header('Content-Transfer-Encoding: binary');                    header('Expires: 0');                    header('Cache-Control: must-revalidate');                    header('Pragma: public');                    header('Content-Length: ' . filesize($filename) . ' bytes');                    //ob_clean();                    //flush();                    readfile($filename);                    fclose($file);                    return true;                }else{                    echo("the file not exist");                    return false;                }                //above to provide the download }

But there is no download function. The file has been generated in the previous step. However, if I change "return true" to "exit", the download function can be implemented. However, after downloading an object, I need to perform other functions. Ask the gods what is going on? The download example on the Internet does not mean that exit is required.


------ Solution --------------------
Check where you are calling this function and where you are calling it. is there a problem?
------ Solution --------------------
In this case:
PHP code
If (download_file ($ filename) {# the following operations} else {# error prompt}
------ Solution --------------------
Discussion
Write the PHP code of an object download program.
Function download_file ($ filename ){
// Below to provide the download
If (file_exists ($ filename )){

$ File ......

------ Solution --------------------
Discussion

Reference:

In this case:
PHP code

If (download_file ($ filename )){
# The following operations
} Else {
# Error message
}



I don't quite understand what you said. did you omit the return statement? Return is omitted and cannot be run.

------ Solution --------------------
Your function return false is equivalent to preventDefault and does not submit a form.
When the answer is met, for example, if the check field is not empty and then return true, the page will jump and your action will be called.
This is a common method for form submission to prevent abnormal submission.

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.