PHP File Downloader



It is a simple PHP program to download any file from URL to your server

Let’s say we have two websites, the old one has some files that we need to move them or copy them to the second website. Obviously we can download those files from 1st website and upload them to 2nd website. But, with this simple PHP program, we can move the files between those two without downloading them and uploading again.

The way it works is very simple, first you need to specify a file url which you want to download, and second specify a folder/directory where you want the downloaded file to be there (leave it empty if you want to download the file on the root directory). Then click the download button. Done!

Here it is:

<!DOCTYPE html>
<html>
    <head>
        <title>PHP File Downloader</title>
    </head>
    <body>
        
        <?php
        if(isset($_POST["url"])){
            $url = $_POST["url"]; 
            $directory = $_POST["directory"];
            
            if($url != ""){
                
                $dirname = "";
                
                if($directory != ""){
                    if(!file_exists($directory))
	                    mkdir($directory);
	                $dirname = $directory . "/";
                }
                
                $file_name = $dirname . basename($url); 
                   
                if(file_put_contents( $file_name,file_get_contents($url))) { 
                    echo "File downloaded successfully"; 
                }
                else { 
                    echo "File downloading failed."; 
                } 
            }
        }
        ?>
        
        <h1>PHP File Downloader</h1>
        <p>This program will help you to download a file from any url to a directory inside your server</p>
        <form method="post">
            <label>File URL to download</label>
            <input name="url">
            <label>Target Directory</label>
            <input name="directory">
            <input type="submit" value="Download">
        </form>
    </body>
</html>
loading...

Leave a Reply

Your email address will not be published. Required fields are marked *