0

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.

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

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).

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!

1 Answer 1

1

You can do that like this

$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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I messed around with that for a while, but eventually found that WordPress has a nice HTTP API with a wp_remote_post method. That paired with this article solved my issue codechutney.com/posting-file-using-wp_remote_post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.