$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
?>
$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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.