Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP PUT Request: Passing Parameters with File

After numerous various tests with uploading files throught HTTP POST Request, it looks that HTTP PUT Requests are the most suitable for very large files +1GB upload.

The below listed simple code I have tested for HTTP PUT file upload request works well:

JavaScript:

var req = createRequest();
req.open("PUT", "PHP/filePutLoad.php");
req.setRequestHeader("Content-type", "text/plain");
req.onload = function (event)
{
    console.log(event.target.responseText);
}
req.send(aUploadedFile.file_object);

PHP:

include 'ChromePhp.php';
require_once 'mysqlConnect.php';

ini_set('max_execution_time', 0);

ChromePHP::log( '$_PUT :' . print_r($_PUT));

/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time and write to the file */
while ($data = fread($putdata, 1024))
    fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);

However, I have difficulties delivering arguments and variables with the file being uploaded from JavaScript to PHP. For example, I need to deliver upload target folder, where the new data needs to be stored, ID of the uploader, etc..

  • Is there a way to combine HTTP PUT Request with HTTP POST to submit arguments?
  • What are my options if I wish to deliver parameters from JavaScript to PHP along HTTP PUT file upload?

Thank you.

like image 429
Bunkai.Satori Avatar asked Dec 15 '25 15:12

Bunkai.Satori


1 Answers

using PUT also, it works when you append the parameters in the query string. I'm also looking for another way for this. Although, this is a workaround I'm using currently

curl -X PUT "http://www.my-service.com/myservice?param1=val1" --data @file.txt

like image 57
saurshaz Avatar answered Dec 17 '25 06:12

saurshaz