Advancing in FTP small connection system through PHP

<?php
require_once 'config.php';
require_once 'functions.php';


// Ftp connection
$ftp_connection = ftp_connect($ftp_host) or die("Couldn't connect to $ftp_host");
ftp_login($ftp_connection, $ftp_user, $ftp_pass);
ftp_pasv($ftp_connection, true);

$local_dir = $_SERVER["DOCUMENT_ROOT"].'/all/clevertechie/ftptransfer/baseurl/';
$remote_server_dir = "/ftptransfer/here";

$files = clean_scandir($local_dir);
// pre_r($files);
echo $ftp_connection !== FALSE ? 'Connected' : "Couldn't connect";
echo "</br>";

for ($i=0; $i<count($files) ; $i++) {
	$files_on_server = ftp_nlist($ftp_connection, $remote_server_dir);
	// pre_r($files_on_server);die;
	if (!in_array("$files[$i]",$files_on_server)) {
		// upload images/files to the remote server
		if(ftp_put($ftp_connection, "$remote_server_dir/$files[$i]", "$local_dir/$files[$i]",FTP_BINARY)){
			echo "Succesfully uploaded $files[$i]";
			echo "<br>";
		}else {
			echo "There was a problem while uploading a file";
			echo "<br>";
		}
	}else {
		echo "$files[$i] exists";
		echo "<br>";
	}
}

ftp_close($ftp_connection);

Above code works fine what it is intended for.

But this part →

$local_dir = $_SERVER["DOCUMENT_ROOT"].'/all/clevertechie/ftptransfer/baseurl/';

I want the local directory also to be fetched up through PHP and FTP. How should I accomplish this?
Both the source and destination are on the same server and on the same FTP connection.

I tried something like this →

$local_dir_url = "/ftptransfer/baseurl";
$local_dir = ftp_nlist($ftp_connection, $local_dir_url);
$remote_server_dir = "/ftptransfer/here";

Set up an FTP server on your local machine, and create an FTP account that points to the local directory. Then you can log into that account (even though it’s on the same machine) and retrieve the files via FTP.

Try setting the following which should show errors and warnings:

<?php declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors','1');

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.