4

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

1 Answer 1

12

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
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is that I want to it without the necessity to fill the form manually, thats why I am using cURL. My question here is how I get the results from the cURL. How can I get the uploaded file in the facebook.php
Well, make a file, let's say curl.php, then use the snippet from the above, make another file and name it uploader.php or facebook.php, use the other snippet I sent in my answer and that's it. Be sure to use the right path for the files and check if you have a permission to write on that uploading folder.
My ploblem is: I don't know what to write in the uploader.php/facebook.php to use the uploader file, like I did when I was using the form.
You just have it from my answer. Otherwise, you may use the same file you used before.
In my file, I had the uploaded file URL in the $uploadfile variable do I can use it later. In your answer, using the cURL, where can I find it? Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.