使用HTML Form或cURL在PHP中上传多个文件 [英] Uploading multiple files in PHP using HTML Form or cURL
问题描述
这是multipart / form-data文件的上传文件,在另一个php文件中,我正在使用的表单数据:
< html>
< body>
< form enctype =multipart / form-dataaction =facebook.phpmethod =post>
< p>< label for =source> Photo< / label>< / p>< input type =filename =source/>
< p>< input type =submitvalue =Upload/>< / p>
< / form>
< / body>
< / html>
现在我更改为使用cURL上传文件,因为我想上传更多的一个文件,所以我不需要使用这个表单。
这是我现在使用的代码:
< ?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));
}
?>
我试图以前面的方式获取facebook.php中的上传文件,但它不工作,因为我几乎不知道如何做。
现在这是facebook.php文件:如果($ empty($ _ FILES)){
$ uploaddir ='./uploads/'; $>
//上传文件夹
$ uploadfile = $ uploaddir。基名($ _ FILES [源] [名称]);
if(move_uploaded_file($ _ FILES ['source'] ['tmp_name'],$ uploadfile)){
使用文件URL
。
。
。
$ b $ p
$ b 任何人都可以帮我说我怎样才能上传使用cURL文件?
谢谢
解决方案 cURL上传多个文件;您可以使用常规表单上传一大堆文件。
使用HTML表单上传多个文件
客户端-Side
<! - uploader.html - >
< form enctype =multipart / form-datamethod =postaction =uploader.php>
< input name =blob []type =file/>< br />
< input name =blob []type =file/>< br />
< input type =submitvalue =上传这些文件/>
< / form>
服务器端
($ _ $ FILES [blob])){
for $($ i = 0; $ i< count($ _ FILES [ blob] [name]); $ i ++){
$ tmp_name = $ _FILES [blob] [tmp_name] [$ i];
$ blob_name =file-name.ext; //生成一个唯一的名字
if(move_uploaded_file($ tmp_name,destination-folder /。$ blob_name)){
//对上传的文件做些什么
使用cURL上传多个文件
$ b $ h $>使用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);
//
//其他部分使用
上面的uploader.php代码
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
解决方案 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
这篇关于使用HTML Form或cURL在PHP中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!