Jump to content

PHP: Downloaded file is bugged


Highlord

Recommended Posts

Oke, I have the following problem. I made a small messagesystem for users, and they can attach a document to the message they send.
I can upload the document to a folder on the server. If I open the document straight of the server, its an exact copy of the file I uploaded, which is good and normal.

However, when I download the file using the php-page, the file suddenly contains pieces of my php-layout and the original content of the file is at the end of the file I downloaded.


I will post the pieces of code that I think are relavant to the problem

//get the messages for the logged in user
$messages = get_messages($userID);
for ($i=0;$i<sizeof($messages);$i++)
{
    $message = $messages[$i];

    echo "<form name=\"frmMessage\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" enctype=\"multipart/form-data\">\n";
    echo "<input type=\"hidden\" name=\"userID\" value=\"$userID\">\n";
    echo "<input type=\"hidden\" name=\"submitAction\">\n";
    echo "<input type=\"hidden\" name=\"fileName\">";

/**Displays the attached file as a link
action = download: the action that is being used
fileName: the name of the file as it is saved on the webserver (=$message['path']
$message['attachment'] = the name of the original file
**/
<a href="javascript: document.frmMessage.submitAction.value='action=download&fileName=<?php echo $message['path']; ?>'; document.frmMessage.submit();">
                            <?php echo $message['attachment'];    ?>             
                            </a><?php     

}

if($action == "download")
        {
         
            $path = "C:\attachments/";
            $full_file_name = $path.$fileName;
            $len = filesize($full_file_name);
              $content_type = file_get_mime_type($fileName);
            header("Cache-Control: cache, must-revalidate"); 
            header("Pragma: public");
            header("Content-Type: " . $content_type);
            header("Content-Length: " . $len);
            header("Content-Disposition: attachment; filename=\"".$fileName."\";");
            header("Content-Transfer-Encoding: binary");
            readfile($full_file_name);
         
            exit;
           
             
        }?>


Does anyone know what i'm doing wrong? I think it's the way I link to the file (with the javascript) but I tried with $_SERVER['PHP_SELF']?action=download&fileName= ... also and it just does the same thing.

Every help is welcome.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.