Title: Send file using wp_safe_remote_post
Last modified: April 18, 2017

---

# Send file using wp_safe_remote_post

 *  [vinigarcia87](https://wordpress.org/support/users/vinigarcia87/)
 * (@vinigarcia87)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/send-file-using-wp_safe_remote_post/)
 * On my local tests, I’m using the follow code to send a file on HTTP connection:
 *     ```
       $url = 'https://...';
   
       $ch = curl_init();
   
       $tmpfile = 'teste.txt';
       $curl_file = new CURLFile(realpath($tmpfile),'text/csv',$tmpfile);
       $file = array('file[]' => $curl_file);
   
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $file); //$data);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       $response = curl_exec($ch);
       if(curl_error($ch)) {
           echo 'error:' . curl_error($ch);
       } else {
           echo $response;
       }
       ```
   
 * This code works perfectly!
 * Now, I wanna connect to this URL from my WordPress application.
 * The problem is that I don’t know how to send files using the wp_safe_remote_post.
 * I tried the following code:
 *     ```
       $url = 'https://...';
   
       $tmpfile = WP_CONTENT_DIR . '/test.txt';
       $params = array(
           'method'   => 'POST',
           'timeout'  => 60,
           'body'     => [
               'file[]'   => realpath( $tmpfile ),
           ]
       );
   
       $response = wp_safe_remote_post( $url, $params );
       ```
   
 * But the remote server says that no file was sended!
 * What I did wrong? How to make this work?
 * Thanks for your help!

Viewing 1 replies (of 1 total)

 *  [jkovis](https://wordpress.org/support/users/jkovis/)
 * (@jkovis)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/send-file-using-wp_safe_remote_post/#post-9045454)
 * What happens if you try calling wp_remote_post() instead of wp_safe_remote_post()?
 * wp_safe_remote_post() calls [wp_http_validate_url()](https://developer.wordpress.org/reference/functions/wp_http_validate_url/)
   which might be rejecting the URL.

Viewing 1 replies (of 1 total)

The topic ‘Send file using wp_safe_remote_post’ is closed to new replies.

## Tags

 * [curl](https://wordpress.org/support/topic-tag/curl/)
 * [HTTP](https://wordpress.org/support/topic-tag/http/)
 * [webservice](https://wordpress.org/support/topic-tag/webservice/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [jkovis](https://wordpress.org/support/users/jkovis/)
 * Last activity: [9 years, 1 month ago](https://wordpress.org/support/topic/send-file-using-wp_safe_remote_post/#post-9045454)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
