Hello,
I have a series of scripts that allow someone to process a text file on an FTP server. After that's completed, I would like to then move the entire directory that they were working in, to a new directory so it doesn't get processed again. I got this to work, but it appeared the moving function downloaded the files to the webserver, then reuploaded them to the FTP server - not good 🙂

function ftp_move_directory($dir,$year)
{
	include 'ftp.inc.php';
	$src_dir = $ftp_dir.$dir."/";
	$dst_dir = "/media/disk/server/".$year."/".$dir."/";
	if(is_dir($dst_dir))
	{ 
		return "<br> Dir <b> $dst_dir </b> Already exists  <br> "; 
    }else{ 
	//file('ftp://'.$ftp_user_name.':'.$ftp_user_pass.'@'.$ftp_server.'/'.$v));
		$d = dir('ftp://'.$ftp_user_name.':'.$ftp_user_pass.'@'.$ftp_server.'/'.$src_dir); 
		ftp_mkdir($conn_id, $dst_dir);
		echo "create dir <b><u> $dst_dir </u></b><br>"; 
		while($file = $d->read()) 
		{ // do this for each file in the directory 
			if ($file != "." && $file != "..")
			{ // to prevent an infinite loop 
				if (is_dir('ftp://'.$ftp_user_name.':'.$ftp_user_pass.'@'.$ftp_server.'/'.$src_dir.$file))
				{ // do the following if it is a directory 
					ftp_copyAll($conn_id, $src_dir.$file, $dst_dir.$file); // recursive part 
				} else { 
					$upload = ftp_put($conn_id, $dst_dir.$file, ('ftp://'.$ftp_user_name.':'.$ftp_user_pass.'@'.$ftp_server.'/'.$src_dir.$file), FTP_BINARY); // put the files 
					echo "create files::: <b><u>".$dst_dir.$file ." </u></b><br>"; 
				} 
			} 
			ob_flush(); 
			sleep(1); 
		} 
		$d->close(); 
	}
	ftp_close($conn_id);
	return "<br><br><font size=3><b>All Copied  ok </b></font>"; 

I've seen a few threads on this forum that look close to this, but none with enough information to really get me started in the right direction. Like I said, the above works, but the files I'm moving can be quite large, so waiting on the down/up isn't feasible. I wanted to use FTP because I have file permissions since it's a linux box, and this way I can control the user that's doing the move. Thanks!

    I think from now on when I have a question, I'll hit the preview button and wait another 30 minutes. It seems like every time after I post, I find exactly what I need.

    The ftp_rename() function doesn't just rename files, it can rename directories, which in turn can move them on the remote server.

      Write a Reply...