简体   繁体   English

使用 cURL 上传多个文件

[英]Multiple file uploads with cURL

I'm using cURL to transfer image files from one server to another using PHP.我正在使用 cURL 使用 PHP 将图像文件从一台服务器传输到另一台服务器。 This is my cURL code:这是我的 cURL 代码:

// 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);

This is the code in transfer.php on the server I'm uploading to:这是我要上传到的服务器上的 transfer.php 中的代码:

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]);
        }
    }
}

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.这可能是因为我在我的帖子字段数组中调用了两个图像upload[] ,但我不知道该怎么做。 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!谢谢!

here is your error in the curl call...这是您在 curl 调用中的错误...

var_dump($post)

you are clobbering the array entries of your $post array since the key strings are identical...您正在破坏 $post 数组的数组条目,因为键字符串是相同的......

make this change做这个改变

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

The code itself looks ok, but I don't know about your move() target directory.代码本身看起来不错,但我不知道您的 move() 目标目录。 You're using the raw filename as provided by the client (which is your curl script).您正在使用客户端提供的原始文件名(这是您的 curl 脚本)。 You're using the original uploaded filename (as specified in your curl script) as the target of the move, with no overwrite checking and no path data.您使用原始上传的文件名(在 curl 脚本中指定)作为移动目标,没有覆盖检查和路径数据。 If the two uploaded files have the same filename, you'll overwrite the first processed image with whichever one got processed second by PHP.如果两个上传的文件具有相同的文件名,您将使用 PHP 第二个处理的图像覆盖第一个处理的图像。

Try putting some debugging around the move() command:尝试围绕 move() 命令进行一些调试:

if (!move_uploaded_file($_FILES['upload']['tmp_name'][$key], $_FILES['upload']['name'][$key])) {
   echo "Unable to move $key/";
   echo $_FILES['upload']['tmp_name'][$key];
   echo ' to ';
   echo $_FILES['upload']['name'][$key];
}

(I split the echo onto multiple lines for legibility). (为了清晰起见,我将回声分成多行)。

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

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