Jump to content

[SOLVED] Download a URL to Web Server


mattdavis90

Recommended Posts

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

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>
';
}
?>

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.

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.

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!!

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.

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.