通过 cURL 从 PHP 中的 POST 表单发送文件
我正在编写一个 API,并且想要处理从表单 POST
上传的文件。表单的标记并不太复杂:
<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 请求一起发送。
我熟悉使用带有数据数组的 cURL 发送 POST
请求,并且我读过的有关上传文件的资源告诉我在文件名中添加 @
符号作为前缀。但这些相同的资源有一个硬编码的文件名,例如,
$post = array(
'image' => '@/path/to/myfile.jpg',
...
);
这是哪个文件路径?我在哪里可以找到它?是否会类似于 $_FILES['image']['tmp_name']
,在这种情况下我的 $post
数组应该如下所示:
$post = array(
'image' => '@' . $_FILES['image']['tmp_name'],
...
);
或者我正在考虑这个方式错误?任何建议将不胜感激。
编辑:如果有人能给我一个代码片段,说明我将使用以下代码片段去哪里,那么我将不胜感激。我主要关注的是我将作为 cURL 参数发送的内容,以及如何在接收脚本中使用这些参数的示例(为了论证,我们将其称为 curl_receiver.php
)。
我有这个网络表单:
<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
:
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']
}
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>
However, I'm having difficulties understanding how to handle this server-side and send along with a cURL request.
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',
...
);
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'],
...
);
Or am I going about this the wrong way? Any advice would be most appreciated.
EDIT: 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).
I have this web form:
<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>
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']
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
以下是一些将文件发送到 ftp 的生产代码(可能对您来说是一个很好的解决方案):
Here is some production code that sends the file to an ftp (may be a good solution for you):
对于发现这篇文章并使用 PHP5.5+ 的人来说,这可能会有所帮助。
我发现 netcoder 建议的方法不起作用。即这不起作用:
我会在
$_POST
var 中收到'uploaded_file'
字段 - 而在$_FILES
var 中什么也没有。事实证明,对于php5.5+,您需要使用一个新的
curl_file_create()
函数。因此上面的内容将变为:由于
@
格式现已弃用。For people finding this post and using PHP5.5+, this might help.
I was finding the approach suggested by netcoder wasn't working. i.e. this didn't work:
I would receive in the
$_POST
var the'uploaded_file'
field - and nothing in the$_FILES
var.It turns out that for php5.5+ there is a new
curl_file_create()
function you need to use. So the above would become:As the
@
format is now deprecated.这应该有效:
在接收脚本中,您将:
然后,如果您想正确处理文件上传,您将执行以下操作:
This should work:
In the receiving script, you would have:
Then, if you want to properly handle the file upload, you would do something like this:
对于我来说,@符号不起作用,所以我做了一些研究,发现这种方法对我有用,我希望这对你有帮助。
For my the @ symbol did not work, so I do some research and found this way and it work for me, I hope this help you.
当我通过 Mercadolibre 的消息系统发送附件时,它对我有用。
答案 https://stackoverflow.com/a/35227055/7656744
It works for me when sending an attachment to Mercadolibre, through its messaging system.
The anwswer https://stackoverflow.com/a/35227055/7656744
我们可以通过curl请求上传图像文件,将其转换为base64字符串。因此,在post中我们将发送文件字符串,然后将其隐藏在图像中。
然后在curl请求中发送它,
请参阅此处 http://technoblogs.co.in/blog/How-to-upload-an-image-by-using-php-curl-request/118
we can upload image file by curl request by converting it base64 string.So in post we will send file string and then covert this in an image.
then send it in curl request
see here http://technoblogs.co.in/blog/How-to-upload-an-image-by-using-php-curl-request/118
这是我的解决方案,我读了很多帖子,它们真的很有帮助,最后我用 cUrl 和 Php 构建了一个小文件的代码,我认为它非常有用。
有了这个,我们应该在“api.endpoint.post”上发布以下变量
您可以轻松地使用此脚本进行测试,并且您应该在最后一行的函数 postFile() 上收到此调试信息
print_r($response); //打印响应
在这里,它应该工作得很好,可能是更好的解决方案,但这确实有效,并且对于理解边界和多部分/来自数据mime如何在php和curl库上工作非常有帮助,
我最好的问候,
我的歉意我的英语,但不是我的母语。
Here is my solution, i have been reading a lot of post and they was really helpfull, finaly i build a code for small files, with cUrl and Php, that i think its really usefull.
With this we shoud be get on the "api.endpoint.post" the following vars posted
You can easly test with this script, and you should be recive this debugs on the function postFile() at the last row
print_r($response); //print response
Here you are it should be work good, could be better solutions but this works and is really helpfull to understand how the Boundary and multipart/from-data mime works on php and curl library,
My Best Reggards,
my apologies about my english but isnt my native language.