0

I need to send data to my own server for test. I've found such implementation.

 <?php 
        $data = array("a" => $a);
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

        $response = curl_exec($ch);
        if(!$response) {
            return false;
        }
        else
        {
            echo"OK";
        }
?>

This says "OK" so script is working. But how can I send a file? This is my attempts: HTML:

  <form action="upload.php" method="put" enctype="multipart/form-data">
  <input type="file" name="filename"><br> 
  <input type="submit" value="Load"><br>
  </form>

PHP:

 <?php 
        $data = $_FILES['filename']['tmp_name']
        $ch = curl_init('http://xmpp1.feelinhome.ru/');

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

        $response = curl_exec($ch);
        if(!$response) {
            return false;
        }
        else
        {
            echo"OK";
        }
?>

This doesn't says "OK" so my script is wrong. Where is my mistake how can I send file?

Sergey Scopin
  • 2,217
  • 9
  • 39
  • 67

2 Answers2

0

As phihag states in their answer:

According to the HTML standard, you can not. The only valid values for the method attribute are get and post, corresponding to the GET and POST HTTP methods. <form method="put"> is invalid HTML and will be treated like <form>, i.e. send a GET request.

Try to change form method to post

 <form action="upload.php" method="post" enctype="multipart/form-data">

Also set in curl

curl_setopt($ch, CURLOPT_POST, 1);
Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

The best answer is right but for the wrong question. Yes for html forms you can only use GET and POST but you didn't ask how you can do it according to html standard. You can always use PUT and DELETE through an XHR request. Of course the best answer here didn't touch another part of the truth, that php doesn't properly support PUT and DELETE requests as the appropriate super globals are not available. In order to to make things work you should use the stream class gist here. Make sure that you use my version as I have slightly modified it so that the files get uploaded correctly and the $_files super global is set as expected. Then at your client you should perform an ajax request this way:

  var formData =new FormData($('form')[0]);
  $.ajax({
        type: "PUT",
        //in this example we put on the current uri
        url: 'upload.php',
        dataType: 'json',
        data: formData,
        async:false,
        cache:false,
        contentType:false,
        processData:false,
        }).done(function(data) {
          //whatever happens on sucess goes here
       ).fail(function(){
          //whatever happens on failure goes here
        });

A discussion about the lack of this feature and and the relevance of the problem to RFC 2616 can be found here

Andreas
  • 970
  • 18
  • 28