mattdavis90 Posted December 10, 2008 Share Posted December 10, 2008 Hi, I was wondering if it is possible to use PHP to download a file to the web server that is running the PHP script. For example, my computer at home has a web server running with PHP. I can access the machine from anywhere and would like to be able to set off downloads remotely. Is this possible using PHP? I tried using a file upload script and submitting a url instead of a file location however this seems to crash PHP. Sorry if this is a daft question, but it would be quite handy for me to have this functionality. Thanks, Matt Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/ Share on other sites More sharing options...
gevans Posted December 10, 2008 Share Posted December 10, 2008 If you can access your php server at home you could just do it with a simple form. You might want to make a login, or work this a bit more, but the following will work (as long as the download starts automatically from the site) <?php if(isset($_POST['submit']) && !empty($_POST['url'])){ header("Location: $url"); } else { echo ' <html> <head> <title>url submit</title> </head> <body> <form action="'.$_SERVER['PHP_SELF'].'" method="post"> <input type="text" name="url" /> <input type="submit" name="submit" /> </form> </body> </html> '; } ?> Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711710 Share on other sites More sharing options...
mattdavis90 Posted December 10, 2008 Author Share Posted December 10, 2008 Thanks for the quick response. I have put the file on my web serve but an Internal Server Error 500 when I try to access the page. I have tried it on another test machine however and the script seems to work. It forwards my browser on to the url that I enter into the form. I would like to download the file straight on to the server if that is possible, as opposed to downloading it locally. Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711740 Share on other sites More sharing options...
gevans Posted December 10, 2008 Share Posted December 10, 2008 Ahhh, so you want to forward a url that will take you to a file download that will be downloaded to your server that you can access from an external location? Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711744 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 I think you would want to use use file_get_contents to get the file then use fopen and write the contents from file_get using fwrite then close the file with fclose I am not sure if this will work, but yea, especially depending on the file size the script would time out. Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711750 Share on other sites More sharing options...
mattdavis90 Posted December 10, 2008 Author Share Posted December 10, 2008 @gevans Yes, I think so. That sounds like what I would like to do. Thanks @premiso Thanks for the functions, Sure they'll be a great starting point Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711752 Share on other sites More sharing options...
gevans Posted December 10, 2008 Share Posted December 10, 2008 I have an idea. I use Firefox, and you can choose the download directory, if you set the download directory to one in your server hierarchy all your downloaded files (using my first script) will be accessible via the server. I'm only suggesting this because your using it on a local machine, but it should work!! Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711756 Share on other sites More sharing options...
mattdavis90 Posted December 10, 2008 Author Share Posted December 10, 2008 Using the functions suggested by premiso I have come up with; <?PHP $filename = $_POST['file']; $dir = "/uploader/uploads/"; if(empty($filename)){ echo ' <html> <head> <title>File Downloader</title> </head> <body> <form action="'.$_SERVER['PHP_SELF'].'" method="post"> <input type="text" name="file" /> <input type="submit" name="submit" /> </form> </body> </html> '; } else { $filepath = $_SERVER['DOCUMENT_ROOT'].$dir; $filecontents = file_get_contents($filename); $filehandle = fopen($filepath."file","w"); fwrite($filehandle,$filecontents); fclose($filehandle); } ?> Which takes the file and writes it into one on the server. I know it won't work for larger files but that is ok. I was wondering if there is a way to parse the filename from the url so each file is saved with it's correct filename and extension. Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711785 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 basename I think that would do the trick. Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711791 Share on other sites More sharing options...
mattdavis90 Posted December 10, 2008 Author Share Posted December 10, 2008 That's absolutely brilliant. Thanks for all the help Link to comment https://forums.phpfreaks.com/topic/136396-solved-download-a-url-to-web-server/#findComment-711804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.