Jump to content

Recommended Posts

Hi Guys,

 

I am trying to download a file from remote ftp server. When i am trying to download binary data (jpg, gif) it appears to be corrupt.

 

I tried downloading txt files. They seem to have extra space at the top of the file.

 

The upload script is working fine and there is no change in the file contents. When i am uploading i strip off the the extension and assign a unique name to the file.

 

<?php

//$filename = from db;
//$mime  = from db;
//$size = from db;

url = $GLOBALS['FILESERVER'] . $_SESSION['getfile'];

$fp = fopen($url, 'rb');
$contents = stream_get_contents($fp, -1, 0);
fclose($fp);

header("Content-Transfer-Encoding: binary");
//header("Content-Type: application/force-download; charset=windows-1251"); 
header("Content-Type: $mime");
header("Content-Length: $size");

header("Content-Disposition: attachment; filename=$filename");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");

header("Pragma: no-cache");
echo $contents;

exit;
}
?>

 

I have been stuck with this problem for the past 2 days. Any help will be appreciated.

 

Thanks for your time,

Sujith

The url variable doesn't start with a dollar sign (maybe just a forum copy/paste typo):

 

$url = $GLOBALS['FILESERVER'] . $_SESSION['getfile'];

 

Try a different tactic. Remove:

 

$fp = fopen($url, 'rb');

$contents = stream_get_contents($fp, -1, 0);

fclose($fp);

 

and replace:

 

echo $contents;

 

with:

 

readfile($url);

 

 

I would use filesize() to get the most current size of the file and not rely on what's in the database.

 

Why are you using $filename in one place and $url in another?

 

Well, double check everything and make sure nothing is outputing a space or newline. At this point I'm not sure if you're talking about a binary file that's not working or the newline is with a text file that's not working. You also don't show us what your mime type is set to.

 

 

http://www.phpit.net/code/force-download/

 

http://elouai.com/force-download.php

 

http://en.wikipedia.org/wiki/MIME

Hi toplay,

 

When i upload the file, i copy the filename and mime to database and rename the file with a unique name (d7497f3e-8496-428f-b573-ac9f1a0d7f3d) with no extension.

 

And when i download the file from the remote server. I get the filename and mime type from the database for the corresponding file name (d7497f3e-8496-428f-b573-ac9f1a0d7f3d)

 

Download is working except for the fact that the file is corrupted on download. I tried binary files (jpg, gif) and they all seem to be corrupt as \n characters are added to the beginning of the files. (which i found out by using text files)

 

I hope that would explain the code a bit. The problem is - \n characters are added to the files during download which makes the data to be corrupt!

 

Best Regards

Sujith

 

 

Hi everyone,

 

Update:

I edited the downloaded binary file (jpg) using vi. And removed the newline characters.

Now i am able to view the file. It seems that the addition of \n characters make the downloaded

files to be corrupt. I dont understand how the \n characters are added to the file contents.

Any pointers would be helpful.

 

Regards

Sujith

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.