cURL: Copy all files of a directory to remote server

  1. Create cURL handler
  2. Looping through all files inside the source director.
  3. Posting one by one files to remote server.
scandir() is used to get an array of files and directories of a given path.
is_dir() to check if input is directory or not

Listing all files of source directory

$ar=scandir($path);//Array with all files and directories names
$ar=array_diff($ar,array('.','..'));// remove dots
while (list ($key, $f_name) = each ($ar)) {
//echo "$key ( $f_name ) <br>";
}
Full code is here.
<?Php
$my_curl = curl_init(); //new cURL handler
$path = 'H:\\data\\'; // Source directory ...change the path
$url='http://localhost/t/curlck.php'; // destination script to receive
//$url='http://10.8.150.10/curlck.php'; // remote server path

$ar=scandir($path); // Array with all files and directories names
$ar=array_diff($ar,array('.','..'));// remove dots ( parent & up directoris ) 
while (list ($key, $f_name) = each ($ar)) {
//echo "$key ( $f_name ) <br>";

if(!is_dir($f_name)){ // If not directory 
	$f_path=$path.$f_name; // create the path 
	//echo $f_path."<br>";
	
    if (function_exists('curl_file_create')) { // php 5.5+
        $cFile = curl_file_create($f_path);
    } else { // 
        $cFile = '@' . realpath($f_path);
    }

    $my_array=array(
    CURLOPT_URL =>$url,
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_POSTFIELDS    => array(
        'file_up' => $cFile
		)
    );
    curl_setopt_array($my_curl, $my_array); // use the array 

    $return_str= curl_exec($my_curl); // Execute/ upload and get data

    echo $return_str; // Display the output
} // end of if not directory
} // end of while loop listing all files 
curl_close($my_curl); // close the handler
?>

curlck.php

$url='http://localhost/t/curlck.php'; // destination script to receive
//$url='http://10.8.150.10/curlck.php'; // OR remote server path
Here we are pointing to a file curlck.php in remote server. This file receives the uploaded file and place them in a directory. The directory name is given like this.
$add="upload/$file_name"; // path to store uploaded file 
This file will receive files at remote server. The directory ( Here upload ) must have the permission to store the files.
<?Php
// This script will allow all file types of any size. 
// This can be a security issue and if allowed it will allow others to upload malacious files. 
// Use carefully or in local environment only . 
// Use at your own risk. 

$file_name=$_FILES['file_up']['name'];
// Create upload directory on same path of the script. 
$add="upload/$file_name"; // path to store uploaded file 

if(move_uploaded_file ($_FILES['file_up']['tmp_name'], $add)){
	echo "<br> $file_name uploaded ";
}else{
	echo "<br> $file_name Not uploade ";
}
?>
All files uploaded will have the same name as source file.
cURL cURL DEMO scripts

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer