Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading multiple files in PHP using HTML Form or cURL

I was uploading a file using multipart/form-data, and getting the uploaded file in another php file to use it somewhere else.

This was the multipart/form-data I was using:

<html>
<body>
<form enctype="multipart/form-data" action="facebook.php" method="post">
    <p><label for="source">Photo</label><input type="file" name="source" /></p>
    <p><input type="submit" value="Upload" /></p>
</form>
</body>
</html>

Now I changed to use cURL to upload the file, because I wanted to upload more the one file, so i needed to not use the form.

This is the code I am using now:

<?php

$ch = curl_init('http://localhost/faceads/facebook.php');
curl_setopt_array($ch, array(
    CURLOPT_POSTFIELDS => array(
        'files[]' => '@C:\xampp\htdocs\faceads\Assento-Novo.png',
    ),
));
if (false === ($res = curl_exec($ch))) {
    die("Upload failed: " . curl_error($ch));
}
?>

I am trying to get the uploaded file in facebook.php in the same way I was doing before, but it is not working, and because I know almost nothing about cURL I don't know how to do it.

This is the facebook.php file now:

if (!empty($_FILES)) {
    $uploaddir = './uploads/'; // Upload folder
    $uploadfile = $uploaddir . basename($_FILES['source']['name']);
    if (move_uploaded_file($_FILES['source']['tmp_name'], $uploadfile)) {
       USE THE FILE URL
               .
               .
               .
    }    
}

Anyone can help me saying how can I get the uploaded file using cURL?

Thanks

like image 707
user3697768 Avatar asked Dec 14 '22 21:12

user3697768


1 Answers

You don't need cURL to upload multiple files; you can upload a bunch of them just using a regular form.

Uploading multiple files using HTML Form

Client-Side

<!-- uploader.html -->
<form enctype="multipart/form-data" method="post" action="uploader.php">
    <input name="blob[]" type="file" /><br />
    <input name="blob[]" type="file" /><br />
    <input type="submit" value="Upload these files" />
</form>

Server-Side

// uploader.php
if(isset($_FILES["blob"])){
    for($i = 0; $i < count($_FILES["blob"]["name"]); $i++){
        $tmp_name = $_FILES["blob"]["tmp_name"][$i];
        $blob_name = "file-name.ext"; // generate an unique name
        if(move_uploaded_file($tmp_name, "destination-folder/" . $blob_name)){
        // do something with the uploaded file
        }
    }
}

Uploading multiple files using cURL

$request = curl_init('http://domain.com/uploader.php');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
        'blob[0]' => '@' . realpath('first-file.jpg'),
        'blob[1]' => '@' . realpath('second-file.jpg')
    )
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
curl_close($request);
//
// for the other part use the uploader.php code from above
like image 162
hex494D49 Avatar answered Jan 21 '23 17:01

hex494D49