0

I am downloading a file from Ftp folder after upload.

Problem is when i open the txt file it show s html page source appending with file content

If i preview image file(jpg or jpeg) it shows image is corrupted

If i open pdf error: Failed to load Pdf document

Please let me know where i am wrong.

Here is my code:

if (isset($_GET['id']) && basename($_GET['id']) == $_GET['id']) {
    $filename = $_GET['id'];
} else {
    $filename =NULL ;
}

$err = 'Sorry, the file you are requesting is unavailable.';
if (!$filename) {
    // if variable $filename is NULL or false display the message
    echo $err;
} else {
    // define the path to your download folder plus assign the file name
    $path = '/home/devestctrl/public_html/wp-content/uploads/'.$filename;
    // check that file exists and is readable
    if (file_exists($path) && is_readable($path)) {
        // get the file size and send the http headers
        $size = filesize($path);
        header('Content-Type: application/octet-stream');
        header('Content-Type: '.$mime);
        header('Content-Length: '.$size);
        header('Content-Disposition: attachment; filename='.$filename);
        header('Content-Transfer-Encoding: binary');
        // open the file in binary read-only mode
        // display the error messages if the file can´t be opened
        $file = @ fopen($path, 'rb');
        if ($file) {
            // stream the file and exit the script when complete
            fpassthru($file);
            exit();
        } else {
            echo $err;
        }
    } else {
        echo $err;
    }
}

I checked by using echo of $filename;

It shows the output in the file instead of printing in page.

error displays:

Sorry, the file you are requesting is unavailable.

After error also i can download, but echoed $filename displayed in file.

Insert into table:

 echo "<tr><a href='?id=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";

1 Answer 1

1

You cannot output data to a page and download a file at the same time. One request from your browser will let you do one response. This response is normally displayed in the browser as a page, unless you set the download headers, then the browser will ask you to download the response to a file.

So what you'll need to do is have two requests and responses: In the first, you output your page and a link the user needs to click (or JavaScript or a refresh meta tag to do it automatically) to download the file in a second request.

Sign up to request clarification or add additional context in comments.

7 Comments

I just described it in my second paragraph: In your page, include either a link/button (for the user to click on manually) or some JS or meta tag to trigger a second request, in which you send the actual file.
's currenty I have already included a link for the user to download
Problem is am facing here is after download page is not getting opened.for pdf n txt. But for txt file it's getting appended with html page source
Hm, maybe you are using a layout which is already output when you try to send the file? Then you would need to fix your code so that the layout isn't used in this case.
Can u be more specific about what do u mean by layout
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.