php $_Files fails to save the file when (http) post with Qt - php

I am posting a form with an image with Qt (which I do not think is important) and trying to same the image in the server but my php file keeps giving me the following error.
Notice: Undefined index: imagename in C:\wamp\www\fileupload.php on line 5
I used tcp monitor to detect the messages and they seem to be fine and it is as follows.
POST /fileupload.php HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: multipart/form-data; boundary=---------------------------723690991551375881941828858
Content-Length: 467204
Connection: Keep-Alive
Accept-Language: en-US,*
User-Agent: Mozilla/5.0
Host: localhost:8089
-----------------------------723690991551375881941828858
Content-Disposition: form-data; name="imagename"
1.jpg
-----------------------------723690991551375881941828858
Content-Disposition: form-data; name="message"
This is a Test Message
-----------------------------723690991551375881941828858
Content-Disposition: form-data; name="file"; filename="1.jpg"
Content-Type: image/jpg
Then to the end image content is attached. I am using following code to save the file on the server.
<?php
$userMessage = $_POST["message"];
$imagefilename = $_POST["imagename"];
echo($userMessage.":".$imagefilename);
if($_FILES['imagename']['error'] == 0){
echo("Post is ready");
move_uploaded_file($_FILES['imagename']['error'], './1.jpg');
}else{
echo("ERROR");
}
?>
I am following following documentations but I can not get this to work. I appreciate if someone can give me a hint to solve the issue. I am using PHP 5.3.18 and apache 2.22.
http://www.tizag.com/phpT/fileupload.php
http://www.saaraan.com/2012/02/post-picture-to-facebook-user-wall-php

You've not completed your multipart request. Add this:
-----------------------------723690991551375881941828858--
to end of your post.

<?php $userMessage = $_POST["message"];
echo($userMessage);
move_uploaded_file($_FILES['file']['tmp_name'], "./{$_FILES['file'] ['name']}");
?>

Related

PHP: Receiving PDF file via POST

I use a program called Nitro PDF to design PDFs, it has an option to create forms with submit buttons that submit to a URL. I tried to make a php script that would receive the PDF file and write it to disk but I can't figure out how to make this happen because normally you have to specify a name on $_FILES to receive it like "fileToUpload". Here is what it is sending to the server and then it starts sending the actual file:
POST /pdf.php HTTP/1.1..
Accept: */*..
Content-Type: application/pdf..
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)..
Host: 192.168.3.212..
Content-Length: 481677..
Connection: Keep-Alive..
Cache-Control: no-cache....
It's not being posted as a form, the PDF is being put directly into the POST data, which you can read with php://input.
<?php
copy("php://input", "filename.pdf");

Relative path in $_FILE["file"]["name"] truncated to basename?

I want to post a file to a server with a relative path supplied to the file's filename within the Content-Disposition header (using PHP 7.0 on Ubuntu with curl 7.47):
curl server/index.php -F "file=#somefile.txt;filename=a/b/c.txt"
Applying the --trace-ascii /dev/stdout option shows:
0000: POST /index.php HTTP/1.1
0031: Host: server
004a: User-Agent: curl/7.47.0
0063: Accept: */*
0070: Content-Length: 111511
0088: Expect: 100-continue
009e: Content-Type: multipart/form-data; boundary=--------------------
00de: ----e656f77ee2b4759a
00f4:
...
0000: --------------------------e656f77ee2b4759a
002c: Content-Disposition: form-data; name="file"; filename="a/b/c.txt
006c: "
006f: Content-Type: application/octet-stream
0097:
...
Now, my simple test script <?php print_r($_FILES["file"]); ?> outputs:
Array
(
[name] => c.txt
[type] => application/octet-stream
[tmp_name] => /tmp/phpNaikad
[error] => 0
[size] => 111310
)
However, I expected [name] => a/b/c.txt. Where is the flaw in my logic?
According to https://stackoverflow.com/a/3393822/1647737 the filename can contain relative path.
The PHP manual also implies this and suggests sanitizing with basename().
As we can see from the php-interpreter sources, _basename() filter invoked for security reason and/or to fix some cons particular browsers.
File: php-src/main/rfc1867.c
Lines ~1151 and below:
/* The \ check should technically be needed for win32 systems only where
* it is a valid path separator. However, IE in all it's wisdom always sends
* the full path of the file on the user's filesystem, which means that unless
* the user does basename() they get a bogus file name. Until IE's user base drops
* to nill or problem is fixed this code must remain enabled for all systems. */
s = _basename(internal_encoding, filename);
if (!s) {
s = filename;
}

PATCH/PUT not accepting multipart/form-data file uploads?

Any idea why PATCH and PUT wouldn't be accepting multipart/form-data file uploads?
When I run var_dump($_FILES) it outputs array(0) { }. Any ideas why this is happening? If I POST the file, it works fine.
Below is an example of the request I am running.
Thanks in advance!
PUT /test.php HTTP/1.1
Content-Type: multipart/form-data; boundary=__X_PAW_BOUNDARY__
Host: [redacted]
Connection: close
User-Agent: Paw/2.1.1 (Macintosh; OS X/10.10.2) GCDHTTPRequest
Content-Length: 17961
--__X_PAW_BOUNDARY__
Content-Disposition: form-data; name="avatar"; filename="default.png"
Content-Type: image/png
PNG
[IMAGE DATA]
--__X_PAW_BOUNDARY__--
When uploading files using a PUT request you don't use multipart/form-data. A PUT request is almost the same as a GET request. All you should be doing is putting the contents of the file in the body of the request. After that you can retrieve the file with the following code as explained in the php docs:
http://php.net/manual/en/features.file-upload.put-method.php):
<?php
/* 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);
?>

POST parameters sent by form but not present in php script

I have a strange problem. I'm sending a very large form with hundreds of fields. Basically I have no doubt there is no errors with the form itself because in firefox's console I can see in my request that the POST array is present :
-----------------------------14624305211779581122436169227
Content-Disposition: form-data; name="BlocDocumentDocument[blocsTabulardocumentsTabularn0][n0][file]"
34495153-98c5-4200-8545-2bb71d279ecd
-----------------------------14624305211779581122436169227
Content-Disposition: form-data; name="BlocDocumentDocument[blocsTabulardocumentsTabularn0][n0][title]"
asdf
-----------------------------14624305211779581122436169227
Content-Disposition: form-data; name="BlocDocumentDocument[blocsTabulardocumentsTabularn0][n0][description]"
-----------------------------14624305211779581122436169227
Content-Disposition: form-data; name="BlocDocumentDocument[blocsTabulardocumentsTabularn0][n0][datetime]"
2014-09-19 00:00:00
-----------------------------14624305211779581122436169227
Content-Disposition: form-data; name="BlocDocumentDocument[blocsTabulardocumentsTabularn0][n0][rank]"
1
But if in php I print_r($_POST['BlocDocumentDocument']['blocsTabulardocumentsTabularn0']) in the first line of php I get the following :
Array
(
[n0] => Array
(
[file] => 34495153-98c5-4200-8545-2bb71d279ecd
)
)
There are fields missing. And this problem seems to only occur if the form has a large amount of fields (I have the same form but with less fields (they're generated by javascript) and it works). I already tried to increase php's post_max_size and it didn't work.
I have found the problem. It was php's max_input_vars setting. By default it's 1000 in 5.3.

HTTP PUT method in PHP: separating files from form data

When a file is uploaded via POST, the form data is separated out from the file(s) via the $_POST and $_FILES variables (respectively). On the other hand, when a file is uploaded via PUT, the response must be retrieved from a single source (php://input). Unfortunately, when a file is involved, php://input seems to contain multiple headers, which appear to be divided by a key of some kind (--6OJvloM5owOQsn2b3APr-Ad9dDLvRqBxm in this case).
--6OJvloM5owOQsn2b3APr-Ad9dDLvRqBxm
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
<<<BINARY DATA>>>
--6OJvloM5owOQsn2b3APr-Ad9dDLvRqBxm
Content-Disposition: form-data; name="description"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
<<<FILE DESCRIPTION>>>
--6OJvloM5owOQsn2b3APr-Ad9dDLvRqBxm--
Short of iterating over the entire response and trying to pick out the different headers, is there a way to separate the files from the form data?
Note: I'm using a well-known 3rd-party application to make the API requests, so it's unlikely that the problem resides in the requests themselves.
It seems that you are trying to do to much in one PUT request. However, if you really need to handle the raw data, you should be able to parse it as a MIME string using a library such as this one: http://pear.php.net/package/Mail_mimeDecode

Categories