Upload Multiple Files to PHP Server Using Curl Command Line

upload multiple files to php server using curl command line

The trick is to name the file uploading parameters unique.

curl -F "image=@file1.gif" -F "image2=@file2.gif"  http://localhost:8888/web/Upload.php

This will show up in the $_FILES superglobal as $_FILES['image'] and $_FILES['image2'].

To make the files grouped under one $_FILES index you need to name the parameters as arrays:

curl -F "image[]=@file1.gif" -F "image[]=@file2.gif"  http://localhost:8888/web/Upload.php

Uploading multiple files using PHP via curl

If you can use the same credentials to connect to FTP in parallel, you could try to do that with minimum changes via curl_multi_* with the code like this:

$chs = array();
$cmh = curl_multi_init();
for ($i = 0; $i < $filesCount; $i++)
{
$chs[$i] = curl_init();
// set curl properties
curl_multi_add_handle($cmh, $chs[$i]);
}

$running=null;
do {
curl_multi_exec($cmh, $running);
} while ($running > 0);

for ($i = 0; $i < $filesCount; $i++)
{
$content = curl_multi_getcontent($chs[$t]);
// parse reply
curl_multi_remove_handle($cmh, $chs[$t]);
curl_close($chs[$t]);
}
curl_multi_close($cmh);

Upload image to PHP server using cURL?

Change the php code to

<?php
$file = date("YmdHisms") . ".jpg";
move_uploaded_file($_FILES['image']['tmp_name'], $file);
?>

Upload multiple images with PHP Curl

try use assoc array, how it use into documentation CURLFile

$photos = [
'img1' => 'img1.jpg',
'img2' => 'img2.jpg'
]

$files = [];

foreach($photos as $key => $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', $key);
$files[$key] = $cfile;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));

$output = curl_exec($ch);

curl_close($ch);

PHP file upload (via Curl on the command line)

I think you want the argument to be a capital F (for Form). Lowercase f is for fail, and returns error code 22. Seriously, it's in the manual!

-f, --fail

(HTTP) Fail silently (no output at all) on server errors. This is
mostly done to better enable scripts etc to better deal with failed
attempts. In normal cases when an HTTP server fails to deliver a
document, it returns an HTML document stating so (which often also
describes why and more). This flag will prevent curl from outputting
that and return error 22.

This method is not fail-safe and there are occasions where
non-successful response codes will slip through, especially when
authentication is involved (response codes 401 and 407).

-F, --form

(HTTP) This lets curl emulate a filled-in form in which a user has
pressed the submit button. This causes curl to POST data using the
Content-Type multipart/form-data according to RFC 2388. This enables
uploading of binary files etc. To force the 'content' part to be a
file, prefix the file name with an @ sign. To just get the content
part from a file, prefix the file name with the symbol <. The
difference between @ and < is then that @ makes a file get attached in
the post as a file upload, while the < makes a text field and just get
the contents for that text field from a file.

Also, you've not named your file variable. I think you want:

curl -F "uploadedfile=@/path/to/my/file" http://mywebserver.com/uploads.php

http file upload using cURL in linux(command line)

Try

curl -i -F filename=image.jpg -F image=@/path/to/image.jpg http://localhost/xmlcreate/curlupload.php

how to upload file using curl with PHP

Use:

if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create($file_name_with_full_path);
} else { //
$cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

You can also refer:

http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

Important hint for PHP 5.5+:

Now we should use https://wiki.php.net/rfc/curl-file-upload but if you still want to use this deprecated approach then you need to set curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

How to upload file by PHP cURL using ftp connection?

To upload the file in curl you can use the curl_file_create.Try the below one:

$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$curl_file = curl_file_create($localfile,'zip');
$params = ['file' => $curl_file];

$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass@$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
//curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);


Related Topics



Leave a reply



Submit