Using cURL to create a stream (line by line) FILE UPLOAD via POST with MULTIPART / FORM-DATA

I want to upload a file to a PHP form on a remote server with a specific url. The upload form is the file upload form ( Multipart/form-data ), and my script should take the local file and submit it to this form.

The files are somewhat large, but the size limit for the form file is 1 GB, which is not a problem. But more urgent is that, due to some circumstances, I have to send the file as a stream!

This means that the file is read line by line, and then somehow load it without creating a temporary file for assignment via CURLOPTS_POSTFILDS .

Shortly speaking:

  • I need to use CURLOPTS_READFUNCTION (I think) to get the contents of line by line line by line
  • method must be POST
  • this should mimic a regular file upload in the form of a remote server boot (so, I think, for this I will need some kind of dummy file name).

I tried many ways to do this, but I failed. I am very new to cURL , I have tried a lot of information from other StackOverflow questions and other forums to no avail.

I came to the conclusion that this may not be possible, but, as I said, I have little information about what I am doing, so I need information or recommendations from someone more experienced. So far, I think that CURLOPT_INFILE and CURLOPT_READFUNCTION only work with the PUT method, but I should use POST .

Sorry for the long question, I hope this makes sense. And thanks in advance for any help or information.

EDIT

Below is the code:

 $fh = fopen('php://memory','rw'); fwrite( $fh, $content); //maybe write the contents to memory here? rewind($fh); $options = array( CURLOPT_RETURNTRANSFER => true ,CURLOPT_SSL_VERIFYPEER => false ,CURLOPT_SSL_VERIFYHOST => 1 ,CURLOPT_FOLLOWLOCATION => 0 ,CURLOPT_HTTPHEADER => array( 'Content-type: multipart/form-data' ) ,CURLOPT_INFILE => $fh //I want to read the contents from this file ,CURLOPT_INFILESIZE => sizeof($content) ); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, 'remote_form_url_here'); curl_setopt ($ch, CURLOPT_POST, true); $post = array( 'userfile' => '@i_do_not_have_a_file_to_put_here;filename=myfile.txt' ); curl_setopt ($ch, CURLOPT_POSTFIELDS, $post); curl_setopt_array ($ch, $options); //have the reading occur line by line when making the infile curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($fh) { $line = fgets($fh); if ($line !== false) return $line; else return false; }); $response = curl_exec($ch); echo $response; fclose($fh); 

This code is mostly compiled from the answers found around, but the parts that use the file handler do not seem to fit. I want to use a file handler, but it seems that if there is no way to confuse the form, assuming the content is a file and pass some random file name.

This code does not even work (it will not be able to publish the form at all), or some of its variants even show a ban as a result.

Just for reference, this is a test form that I use to emulate the real situation in which I work until I work (I don’t want to send a ton of requests to a real server):

 <form enctype="multipart/form-data" action="up.php" method="POST"> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form> 

And this is for the code:

 $target_path = "./ups/"; $target_path = $target_path . basename( $_FILES['userfile']['name']); if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['userfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } var_dump($_FILES['userfile']); 
+4
source share
1 answer

If it works fine in the browser, you can use the hrome dev tools. On the Network tab, find the sending request. Right click -> Copy as cURL.

+1
source

Source: https://habr.com/ru/post/1487076/


All Articles