marklarah Posted March 19, 2009 Share Posted March 19, 2009 I've been trying now for ~3 hours :'( :'( :'( - I am stuck and fed up So I need to download a remote file, though my server. So piping the download, or tunneling it though my server. How would I do this? I've been at it ages with download.php's an headers and whatnot, but I just can't seem to figure it out. The file url is stored in a string, I just need for the file to be downloaded though the server. Any idea how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/ Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 One of the problems is it could be any type file that needs to be downloaded. Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788408 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 You should check out your favorite friend, Google Anyways, found this script here. <?php $url = 'http://www.assistprogramming/image.jpg'; $curl_handler = curl_init(); curl_setopt($curl_handler, CURLOPT_URL, $url); curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); //return the transfer in a binary format . $data = curl_exec($curl_handler); curl_close($curl_handler); // Do whatever you want to with $data - it holds the binary content ?> Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788416 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 You should check out your favorite friend, Google Anyways, found this script here. <?php $url = 'http://www.assistprogramming/image.jpg'; $curl_handler = curl_init(); curl_setopt($curl_handler, CURLOPT_URL, $url); curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); //return the transfer in a binary format . $data = curl_exec($curl_handler); curl_close($curl_handler); // Do whatever you want to with $data - it holds the binary content ?> Awesome...but how I do I get the browser to download $data? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788424 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 Specify the content-type in a HTTP header. (You'll have to have an array of extensions mapped to content-types.) Then readfile($data); You will need extra headers to make the save file dialog box come up. You can google that too: Download file headers cross-browser Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788441 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 Actually, check out this link: LitlURL link Use strlen($data) rather than filesize() You'll need to manually set the filename of whatever they're downloading. Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788444 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 Yeah, I'm completely lost. So the download.php gets the data....then what? Another page? What would be in that page? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788520 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 Yeah, I'm completely lost. So the download.php gets the data....then what? Another page? What would be in that page? What exactly are you trying to do? 1. Server downloads file Then: Server pipes file to user to download? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788523 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 I guess Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788525 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 So is there a way? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788568 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 Yah. Give me a few minutes. I'm tweaking the code. BTW, what file types will you mostly be downloading? mp3, png, jpg, gif, etc??? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788571 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 Perhaps rars and zips Couldn't you use header("Content-Type: application/octet-stream;"); ? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788579 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 Actually, I think I've done it! Hang on, I'll post code in a sec, lemme check it works properly Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788590 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 okay, it doesn't work. So I have this as my download.php: <?php $url = $_GET['url']; $name = $_GET['name']; header("Content-Type: application/octet-stream;"); header('Content-Disposition: attachment; filename="'.$name.'"'); $curl_handler = curl_init(); curl_setopt($curl_handler, CURLOPT_URL, $url); curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); $data = curl_exec($curl_handler); curl_close($curl_handler); echo $data; exit; ?> It works fine when I did a text file It worked fine on a rar file. It seemed to be going okay, but then it stopped: it only downloaded 0.2kb of the file (although it had correct file name and such) I think when I tried it before, when it just echoed the text onto the page, and I tired this file, the server timed out, it failed to allocate the data or something. Do you know a way around this? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788593 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 Okay, but it worked for a smaller file. So it won't download big files, but it will small(ish) files. Do you know why? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788604 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 Try the code here: LitlURL link Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788612 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 Hmm. I tried setting the time limit to 0, it doesn't make a difference. Is there like a max download limit I'm missing? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788620 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 Yeah, it just only downloads bits of the file if it's large Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788628 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 here we are! Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 33293348 bytes) in ****** on line 15 Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788640 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 Um...is this a problem with my server not having or enough space or something? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788658 Share on other sites More sharing options...
MadTechie Posted March 19, 2009 Share Posted March 19, 2009 Your memory usage is going over 32Mb, update the php.ini file to increase the memory why not just do this <?php $url = 'http://www.dynamixlabs.com/images/largeimage.bmp'; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($url)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); ob_clean(); flush(); readfile($url); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788709 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 hmm. Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788710 Share on other sites More sharing options...
MadTechie Posted March 19, 2009 Share Posted March 19, 2009 Why the bump ? lass than an hour had passed and their was a unread reply? Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788712 Share on other sites More sharing options...
marklarah Posted March 19, 2009 Author Share Posted March 19, 2009 I didn't see the reply :-X But anyway. Warning: readfile() [function.readfile]: URL file-access is disabled in the server configuration in ******* on line 14 Warning: readfile(***) [function.readfile]: failed to open stream: no suitable wrapper could be found in *** on line 14 Thats what I get. It just downloads the filename.ext.html, and that was what was in it. Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788718 Share on other sites More sharing options...
MadTechie Posted March 19, 2009 Share Posted March 19, 2009 fopen wrappers have been disabled.. that sucks! if its your server just update allow_url_include in the php.ini if not then get a better server i guess your stuck with curl (not a bad thing) but i would update to this <?php $url = $_GET['url']; $name = $_GET['name']; header("Content-Type: application/octet-stream;"); header('Content-Disposition: attachment; filename="'.$name.'"'); $curl_handler = curl_init(); curl_setopt($curl_handler, CURLOPT_URL, $url); curl_setopt($curl_handler, CURLOPT_HEADER, false); //added curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 0); //pointless having this on! curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); curl_exec($curl_handler); curl_close($curl_handler); exit; ?> EDIT: turned off header Quote Link to comment https://forums.phpfreaks.com/topic/150135-download-file-through-php/#findComment-788731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.