PHP上传和发送文件与cURL [英] PHP upload and send file with cURL

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

问题描述

我正在尝试将文件上传到WordPress安装,然后将其与其他表单字段中的其他数据一起发送到 Lever的API

I'm attempting to upload a file to a WordPress installation and then send it, along with other data from other form fields, to Lever's API.

我可以将数据发送到端点,但是文件上传不会太多。下面的内容实际上是上传到wp-content / uploads,但我认为问题在于下一行 move_uploaded_file 或者我把它传递到 $ data 数组。

I can send data to the endpoint just fine, but not so much with the file uploading. The following does in fact upload to wp-content/uploads, but I think the problem lies either on the next line move_uploaded_file or where I'm passing it in the $data array.

<form enctype="multipart/form-data" method="post" action="<?php echo get_template_directory_uri(); ?>/jobForm.php">
    <input type="file" name="resume">
    <button type="submit">Submit</button>
</form>







<?php
// URL
$url = "https://api.lever.co/v0/postings/XXXX/XXXXXX";

$name = $_POST["name"];
$email = $_POST["email"];
$urls = $_POST["urls"];

$target = "/www/wp-content/uploads/" . basename($_FILES["resume"]["name"]);

move_uploaded_file($_FILES["resume"]["tmp_name"], $target);

// data
$data = array(
    "name" => $name,
    "email" => $email,
    "urls" => $urls,
    "resume" => @$_FILES["resume"]
);

// initiate curl instance, set options, and post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // url
curl_setopt($ch, CURLOPT_POST, true);                                                   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // full data to post                                              
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return results as a string instead of outputting directly                                                             

echo $data["resume"];
// $output 
$output = curl_exec($ch);  
var_dump($output);

// close curl resource to free up system resources 
curl_close($ch);
?>



我尝试使用 $ target resume $ data 正如你可能会说,我不知道这是怎么回事(我是一个前端开发人员从我的元素:D)。

I tried using the $target variable for the "resume" $data value, but that didn't seem to work either. As you can probably tell, I'm not exactly sure where this is going wrong (I'm a front-end developer out of my element :D).

回声 $ data [resume] 给出一个数组,同时回应 $ target 给出位置+文件的名称,如预期。我想我不确定什么我需要通过在 $ data 数组...任何想法我做错了在这里?如果有帮助,我在提交时没有收到来自Lever的错误。事实上,它返回一个200 OK消息和发布就好,只是没有恢复字段!

Echoing $data["resume"] gives an Array, while echoing $target gives the location + name of the file, as expected. I guess I'm unsure what I need to be passing through in the $data array...Any ideas what I'm doing wrong here? If it helps, I get no error from Lever when submitting. In fact, it returns a 200 OK message and posts just fine, just without a resume field!

推荐答案

$localFile = $_FILES[$fileKey]['tmp_name']; 

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'someurl' . $strFileName); //$strFileName is obvious
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
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);

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

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