cURL: Copy all files of a directory to remote server

Upload files with CURLFile

Modern PHP uses CURLFile for multipart uploads. Do not build upload values with the old @filename syntax. Validate the local file and use credentials/tokens appropriate to the remote service.

<?php
$path = __DIR__ . '/upload/report.pdf';
if (!is_file($path)) {
    exit('File not found.');
}

$ch = curl_init('https://example.com/upload');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile($path, 'application/pdf', 'report.pdf'),
    ],
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 20,
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new RuntimeException(curl_error($ch));
}
curl_close($ch);
?>

The examples below build on this pattern and show common cURL request types.

  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
foreach ($ar as $f_name) {
    // Process each entry in the directory.
}
Full code is here.
<?php
$path = __DIR__ . '/upload/';
$url = 'https://example.com/upload';

foreach (scandir($path) as $f_name) {
    if ($f_name === '.' || $f_name === '..') { continue; }
    $f_path = $path . $f_name;
    if (!is_file($f_path)) { continue; }

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => ['file_up' => new CURLFile($f_path)],
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => false,
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_TIMEOUT => 30,
    ]);

    $response = curl_exec($ch);
    if ($response === false) {
        throw new RuntimeException(curl_error($ch));
    }
    curl_close($ch);
    echo htmlspecialchars($response, ENT_QUOTES, 'UTF-8');
}
?>

For real deployments, authenticate the upload endpoint, restrict allowed file types and sizes, and keep the destination URL fixed in server-side configuration rather than accepting it from user input.

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



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-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer