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
https://forums.phpfreaks.com/topic/34402-php-downloaded-file-is-bugged/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.