Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Copy Files Around FTP Using PHP

Tags:

php

ftp

I am tring to copy a file from one folder to another using the php ftp functions.

e.g

Copy This File: httpdocs/user_images/Services/File 1.jpg

To: httpdocs/user_images/folder11

i have tried to use ftp_fput but i am not have any luck with it.

like image 283
Rickstar Avatar asked Jan 31 '11 16:01

Rickstar


2 Answers

Perhaps a little known fact: the copy() function in PHP can be used to copy files to an FTP server, though without as much control as you get by using the ftp-specific functions.

In other words, this can do the job:

if(copy('local/file.img', 'ftp://user:[email protected]/remote/dir/file.img')) {
  echo "It worked!!!";
}

http://php.net/manual/en/function.copy.php

like image 130
Mark Kennedy Avatar answered Nov 09 '22 14:11

Mark Kennedy


From the manual page on ftp_put on PHP.net:

<?php 
// bool ftp_copy  ( resource $ftp_stream  , string $initialpath, string $newpath, string $imagename ) 
function ftp_copy($conn_distant , $pathftp , $pathftpimg ,$img){ 
        // on recupere l'image puis on la repose dans le nouveau folder 
        if(ftp_get($conn_distant, TEMPFOLDER.$img, $pathftp.'/'.$img ,FTP_BINARY)){ 
                if(ftp_put($conn_distant, $pathftpimg.'/'.$img ,TEMPFOLDER.$img , FTP_BINARY)){ 
                        unlink(TEMPFOLDER.$img) ;                                              
                } else{                                
                        return false; 
                } 

        }else{ 
                return false ; 
        } 
        return true ; 
} 
?>
like image 42
Merijn Avatar answered Nov 09 '22 14:11

Merijn