werty37 Posted January 20, 2008 Share Posted January 20, 2008 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 Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/ Share on other sites More sharing options...
toplay Posted January 20, 2008 Share Posted January 20, 2008 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? Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444142 Share on other sites More sharing options...
werty37 Posted January 20, 2008 Author Share Posted January 20, 2008 Hi sorry i missed $... I tried you method. The downloaded file still has the newline.... Any other way? Thanks for your time Sujith Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444159 Share on other sites More sharing options...
toplay Posted January 20, 2008 Share Posted January 20, 2008 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 Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444170 Share on other sites More sharing options...
werty37 Posted January 20, 2008 Author Share Posted January 20, 2008 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 Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444174 Share on other sites More sharing options...
werty37 Posted January 20, 2008 Author Share Posted January 20, 2008 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 Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444200 Share on other sites More sharing options...
Barand Posted January 20, 2008 Share Posted January 20, 2008 Have you got a blank line before the "<?php" Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444207 Share on other sites More sharing options...
werty37 Posted January 20, 2008 Author Share Posted January 20, 2008 Hi Barand Thanks a lot. I had \n characters after ?> in one of my includes. Have a nice day Cheers Sujith Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444213 Share on other sites More sharing options...
toplay Posted January 20, 2008 Share Posted January 20, 2008 Well, double check everything and make sure nothing is outputing a space or newline. I guess you didn't take my advice earlier. That's alright...I forgive you Link to comment https://forums.phpfreaks.com/topic/86871-solved-corrupted-download-from-remote-server/#findComment-444455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.