Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form boundaries and writing php://input to a file in php

So I want users to be able to upload big files without having to worry about the post max size values. The alternative is using PUT and send a file as raw data. When using jquery I can do this:

var data = new FormData();
  jQuery.each($('#file_upload')[0].files, function(i, file) {
  data.append('file-'+i, file);
});
$.ajax({
  url: 'upload.php?filename=test.pdf',
  data: data,
  cache: false,
  contentType: false,
  processData: false,
  type: 'PUT',
});

In PHP I can do this:

$f = fopen($_GET['filename'], "w");
$s = fopen("php://input", "r");

while($kb = fread($s, 1024))
{ 
  fwrite($f, $kb, 1024); 
}
fclose($f);
fclose($s);
Header("HTTP/1.1 201 Created");

I am not doing:

$client_data = file_get_contents("php://input");

Since putting the whole file into a variable will surely fill up all memory when uploading huge files.

The thing I cannot figure out is how to write the file data without the form boundaries. Right now it writes at the top of the file something like this:

------WebKitFormBoundaryVz0ZGHLGxBOCUVQG
Content-Disposition: form-data; name="file-0"; filename="somename.pdf"
Content-Type: application/pdf

and at the bottom something like this:

------WebKitFormBoundaryVz0ZGHLGxBOCUVQG--    

So I need to parse the data. But for that I need to read the whole data stream into memory and with large video files I don't want to do that. I did read something about maybe creating a php://temp stream. But no luck yet with that. How can I write just the content to a file, without the boundary header? And without first pumping all the data into a variable?

like image 636
user1494552 Avatar asked May 18 '13 14:05

user1494552


People also ask

How is form data sent to a PHP file?

When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.

How to display the submitted FORM data in PHP?

Try it Yourself ». When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. To display the submitted data you could simply echo all the variables.

Why can't I write to a txt file in PHP?

You should also check if the webserver has write rights in the directory where you are trying to write your "data.txt" file. Depending on your PHP version (if it's old) you might not have the file_get/put_contents functions. Check your webserver log to see if any error appeared when you executed the script.

How do I upload an image to a form in PHP?

Configure The "php.ini" File. First, ensure that PHP is configured to allow file uploads. In your "php.ini" file, search for the file_uploads directive, and set it to On: Create The HTML Form. Next, create an HTML form that allow users to choose the image file they want to upload: Select image to upload:


2 Answers

Maybe a combination of fgets to stop reading at a newline and checking for the boundaries:

while($kb = fgets($s, 1024))
{ 
    if(strpos($kb, '------') === false) //or !== 0 for first position
    {
        fwrite($f, $kb, 1024); 
    }
}
like image 73
AbraCadaver Avatar answered Sep 23 '22 02:09

AbraCadaver


You can use this (there are many like it). It supports chunked uploads which means you won't hit any post/file max sizes as long as each upload chunk is less than the post max size.

It also includes the PHP code you would need on the server side.

like image 37
aljo f Avatar answered Sep 25 '22 02:09

aljo f