简体   繁体   English

使用HTML表单或cURL在PHP中上载多个文件

[英]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. 我使用multipart / form-data上传文件,并将上传的文件放在另一个php文件中,以便在其他地方使用它。

This was the multipart/form-data I was using: 这是我使用的multipart / form-data:

<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. 现在我改为使用cURL上传文件,因为我想上传更多的文件,所以我不需要使用表格。

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. 我试图像以前一样在facebook.php中获取上传的文件,但它不起作用,因为我对cURL几乎一无所知,我不知道该怎么做。

This is the facebook.php file now: 这是facebook.php文件现在:

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? 任何人都可以帮我说如何使用cURL获取上传的文件?

Thanks 谢谢

You don't need cURL to upload multiple files; 您不需要cURL来上传多个文件; you can upload a bunch of them just using a regular form. 你可以使用常规表单上传一堆。

Uploading multiple files using HTML Form 使用HTML表单上传多个文件

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 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM