I've found tons of answers for how to upload or download files between local and remote server using PHP's ftp functions. However I've found nothing that tells me how to copy a file from one location to another on the same server. There's no ftp_copy or ftp_dup function. There is ftp_rename, but that moves the file, it doesn't copy it.

Should I download the file using ftp_get and then upload it using ftp_put once the download is done? That's the best I've come up with so far, but it feels pretty clunky.

Any suggestions?

  • Bob

    rename is for moving files, slightly confusing, but if you think of the file path as part of the name it makes sense.

      Right - same as the ftp_rename function I refer to. But rename doesn't copy, it moves. I need to create copies in a new/different directory. Unless there's some way to trick rename into copying instead of moving?

        Unfortunately I'm pretty sure copy() only works within the server's own filesystem (unless perhaps if you have url_fopen enabled, which I don't). Working with a separate/remote FTP server - that is, separate from the main site server - seems to require the FTP functions.

        Here's my current solution (omitting less relevant code from class). Not ideal since every file copied has to be transferred twice.

        public function copyRemoteFtp()
        {
        	$srcDir = $this->fieldarray['sourceItemId'];
        	$destDir = $this->fieldarray['itemId'];
        	$localDir = PUBLIC_PATH . 'photos/' . $this->fieldarray['imageType'] . '/original/' . $this->fieldarray['itemId'];
        
        if ($conn = ftp_connect(PHOTO_SERVER))
        {
        	if (ftp_login($conn, PHOTO_SERVER_USER, PHOTO_SERVER_PW))
        	{
        		ftp_pasv($conn, true);
        		if (ftp_chdir($conn, PHOTO_SERVER_UPLOAD_DIR))
        		{
        			$rawList = ftp_nlist($conn, ".");
        			if (!in_array($destDir, $rawList))
        			{
        				ftp_mkdir($conn, $destDir);
        				//@ftp_chmod($conn, 0777, $image_dir);
        			}
        
        			$files = ftp_nlist($conn, $srcDir);
        			foreach ($files as $file)
        			{
        				if ($file != "." && $file != "..")
        				{
        					$srcFile = PHOTO_SERVER_FTP_GET_SRC_DIR . '/' . $srcDir . '/' . $file;
        					$localFile = $localDir . '/' . $file;
        
        					if (ftp_get($conn, $localFile, $srcFile, FTP_BINARY))
        					{
        						$upload = ftp_put($conn, $destDir . "/" . $file, $localFile, FTP_BINARY);
        					}
        				}
        			}
        		}
        	}
        }
        }
        

          well you did say "copy a file from one location to another on the same server."

            Ahh - sorry. I figured since I was talking about moving between host and remote server, but I guess I wasn't clear.

            So - copying a file between locations on a remote server, using FTP...that's the puzzle.

              Not very secure at all but you could just send commands right to the console.

                Write a Reply...