通过cURL从PHP中的表单POST发送文件 [英] Send file via cURL from form POST in PHP

查看:167
本文介绍了通过cURL从PHP中的表单POST发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个API,我想要处理来自 POST 的文件上传。表单的标记没有太复杂:

I'm writing an API and I'm wanting to handle file uploads from a form POST. The markup for the form is nothing too complex:

<form action="" method="post" enctype="multipart/form-data">
  <fieldset>
    <input type="file" name="image" id="image" />
    <input type="submit" name="upload" value="Upload" />
  </fieldset>
</form>

但是,我很难理解如何处理这个服务器端并与cURL

However, I'm having difficulties understanding how to handle this server-side and send along with a cURL request.

我很熟悉使用cURL发送带有数据数组的 POST 请求,阅读上传文件告诉我前缀的文件名与 @ 符号。但这些相同的资源有一个硬编码的文件名,例如

I'm familiar with sending POST requests with cURL with a data array, and resources I've read on uploading files tell me to prefix the filename with an @ symbol. But these same resources have a hard-coded file name, e.g.

$post = array(
    'image' => '@/path/to/myfile.jpg',
    ...
);

这是哪个文件路径?我在哪里可以找到它?会是 $ _ FILES ['image'] ['tmp_name'] ,在这种情况下我的 $ post 数组应如下所示:

Well which file path is this? Where would I find it? Would it be something like $_FILES['image']['tmp_name'], in which case my $post array should look like this:

$post = array(
    'image' => '@' . $_FILES['image']['tmp_name'],
    ...
);

还是我走错了路?

编辑:如果有人可以给我一个代码片段,我会使用下面的代码片段,然后我会非常感谢。我主要是在我将发送作为cURL参数,和一个样例如何使用这些参数与接收脚本(让我们叫它 curl_receiver.php 为参数的缘故) 。

If someone could give me a code snippet of where I would go with the following code snippets then I'd be most grateful. I'm mainly after what I would send as cURL parameters, and a sample of how to use those parameters with the receiving script (let's call it curl_receiver.php for argument's sake).

我有这种网络表单:

<form action="script.php" method="post" enctype="multipart/form-data">
  <fieldset>
    <input type="file" name="image />
    <input type="submit" name="upload" value="Upload" />
  </fieldset>
</form>

> script.php :

And this would be script.php:

if (isset($_POST['upload'])) {
    // cURL call would go here
    // my tmp. file would be $_FILES['image']['tmp_name'], and
    // the filename would be $_FILES['image']['name']
}


推荐答案

下面是一些将文件发送到ftp的生产代码(可能是一个很好的解决方案):

Here is some production code that sends the file to an ftp (may be a good solution for you):

// This is the entire file that was uploaded to a temp location.
$localFile = $_FILES[$fileKey]['tmp_name']; 

$fp = fopen($localFile, 'r');

// Connecting to website.
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERPWD, "email@email.org:password");
curl_setopt($ch, CURLOPT_URL, 'ftp://@ftp.website.net/audio/' . $strFileName);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'CURL_callback');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
curl_exec ($ch);

if (curl_errno($ch)) {

    $msg = curl_error($ch);
}
else {

    $msg = 'File uploaded successfully.';
}

curl_close ($ch);

$return = array('msg' => $msg);

echo json_encode($return);

这篇关于通过cURL从PHP中的表单POST发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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