Uploading a file from a remote server to an FTP server using PHP

How can I upload a remote file from a link, for example, http://site.com/file.zip to an FTP server using PHP? I want to upload โ€œVanilla Forum Softwareโ€ to the server, and my mobile data carrier charges high prices, so if I could upload a file without having to download it from my mobile phone, I could save some money and get the job done too.

+4
source share
5 answers

Created this function:

 function downloadfile($file, $path) { if(isset($file) && isset($path)) { $fc = implode('', file($file)); $fp = explode('/', $file); $fn = $fp[count($fp) - 1]; if(file_exists($path . $fn)) { $Files = fopen($path . $fn, 'w'); } else { $Files = fopen($path . $fn, 'x+'); } $Writes = fwrite($Files, $fc); if ($Writes != 0){ echo 'Saved at ' . $path . $fn . '.'; fclose($Files); } else{ echo 'Error.'; } } } 

You can use it as follows:

 downloadfile("http://www.webforless.dk/logo.png","folder/"); 

Hope it works well, remember Chmod destination folder 777. ((If you need to upload it to another FTP server, you can use one of the FTP scripts posted in other comments))

Sincerely. Jonas

+4
source

Something like that

 $con=ftp_connect("ftp.yourdomain.com"); $login_result = ftp_login($con, "username", "password"); // check connection if ($conn_id && $login_result) { // Upload $upload = ftp_put($con, 'public_html/'.$name, "LOCAL PATH", FTP_BINARY); if ($upload) { // UPLOAD SUCCESS } } 

Additional information: http://php.net/manual/en/function.ftp-put.php

+1
source

A) upload the file at the url:

 $destination = fopen("tmp/myfile.ext","w"); //Myfile.ext is an example you should probably define the filename with the url. $source = fopen($url,"r"); while (!feof($source)) { fwrite($destination,fread($source, 8192)); } fclose($source); fclose($destination); 

B) Upload the file to FTP:

 $file = 'tmp/myfile.ext'; $fp = fopen($file, 'r'); $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) { echo "UPLOAD OK"; } else { echo "ERROR"; } ftp_close($conn_id); fclose($fp); 

This is just a quick example, perhaps many improvements that can be made to this code, but the basic idea is here.

Note. If you have a dedicated server, it may be faster and easier to download a file with a wget call.

More information on FTP can be found in the document.

+1
source

Just:

 copy('ftp://user: pass@from.com /file.txt', 'ftp://user: pass@dest.com /file.txt'); 

The PHP server will use load and load bandwidth at the same time.

+1
source

Create a php script in a folder accessible on the Internet on your target server, change the values โ€‹โ€‹of $ remotefile and $ localfile, point your browser to the URL of the script, and the file will be extracted.

 <?php $remotefile="http://sourceserver.com/myarchive.zip"; $localfile="imported_archive.zip"; if(!copy($remotefile, $localfile)) { echo("Transfer Failed: $remotefile to $localfile"); } ?> 
0
source

Source: https://habr.com/ru/post/1381456/


All Articles