使用cURL上传多个文件 [英] Multiple file uploads with cURL

查看:1580
本文介绍了使用cURL上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cURL将图像文件从一个服务器传输到另一个使用PHP。这是我的cURL代码:

I'm using cURL to transfer image files from one server to another using PHP. This is my cURL code:

// Transfer the original image and thumbnail to our storage server
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://' . $server_data['hostname'] . '.localhost/transfer.php');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    'upload[]' => '@' . $tmp_uploads . $filename,
    'upload[]' => '@' . $tmp_uploads . $thumbname,
    'salt' => 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$resp = curl_exec($ch);

服务器上的transfer.php中的代码上传到:

This is the code in transfer.php on the server I'm uploading to:

if($_FILES && $_POST['salt'] == 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q')
{
    // Save the files
    foreach($_FILES['upload']['error'] as $key => $error)
    {
        if ($error == UPLOAD_ERR_OK)
        {
            move_uploaded_file($_FILES['upload']['tmp_name'][$key], $_FILES['upload']['name'][$key]);
        }
    }
}

似乎除了一个小的逻辑错误,只有一个文件被保存在服务器上,这可能是因为我在我的post字段数组中调用两个图像 upload [] ,但我不知道如何做。我试图模仿这样做:

All seems to work, apart from one small logic error. Only one file is getting saved on the server I'm transferring to. This is probably because I'm calling both images upload[] in my post fields array, but I don't know how else to do it. I'm trying to mimic doing this:

<input type="file" name="upload[]" />
<input type="file" name="upload[]" />

任何人都知道如何让这个工作?感谢!

Anyone know how I can get this to work? Thanks!

推荐答案

这里是您在curl调用中的错误...

here is your error in the curl call...

var_dump($post)

$ post数组的数组条目,因为键字符串是相同的...

you are clobbering the array entries of your $post array since the key strings are identical...

进行此更改

$post = array(
    'upload[0]' => '@' . $tmp_uploads . $filename,
    'upload[1]' => '@' . $tmp_uploads . $thumbname,
    'salt' => 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q'
);

这篇关于使用cURL上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆