Remote File Download and upload directly to Server using PHP

Sometime we require to Download some files from the remote location and upload it to our server, which is a time taking process.

Here We are describing how to download and upload the file directly to our server using PHP.

For this  to work , we will take the http location of the files and using the curl method , we will copy it to our server, so our downloading and uploading time will be saved, also it will be more fast , due to its directly copy the file from one remote server to another server.

On this code , we have created a form and its take only HTTP url of the file.


Here is the PHP code to do this:

<html>
<head>
</head>

<body>


<?php
if(isset($_POST['zip_file']))
{
if(!is_dir("folder"))
{
mkdir("folder", 0755);
}
$url = $_POST['zip_file'];
$newname = explode('/',$_POST['zip_file']);
$namewill = end($newname);
$namewill = rand()."_".$namewill;
$zipFile = "folder/".$namewill; // Local Zip File Path
$zipResource = fopen($zipFile, "w");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
if(!$page) {
echo "Error :- ".curl_error($ch);
}
curl_close($ch);
echo "Nmae of Local ZIP File: ".$namewill."<hr/>";
/* Open the Zip file
$zip = new ZipArchive;
$extractPath = "extractedzip";
if($zip->open($zipFile) != "true"){
echo "Error :- Unable to open the Zip File";
}
// Extract Zip File
$zip->extractTo($extractPath);
$zip->close();
*/
}
?>


<form method="post">
<label>
Enter The URL of the ZIP File:
<br/>
<input required type="text" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>


</html>

Comments

Popular posts from this blog

Page Unload Show Notification to user

Function Oriented File Upload PHP

Upload File Using PHP - Example For Image