(CURL, PHP) Uploading files with PUT request, How do I process this? - php

this is what i have in process.php
parse_str(file_get_contents("php://input"),$upload_data);
if (isset($upload_data)) {
print_r($upload_data);
exit;
}
this is how i query to server using curl.
weldan#prindu:~$ curl -X PUT -H "X-TOKEN: test123" -F filedata=#/home/weldan/Pictures/Cool-Pictures1.jpg http://host.tld/process.php
Array
(
[------------------------------22031b6e799c
Content-Disposition:_form-data;_name] => "filedata"; filename="Cool-Pictures1.jpg"
Content-Type: image/jpeg
���
)
so that how i know there is uploaded file there.
current problem is, how do I process this file like $_FILES variable?
open for other way to achieve this too.
Thanks

The $_FILES array being populated on PUT requests. That's because a PUT request will specify the file name in the url and the file content in the body. Nothing more.
You'll have to use php://input as you already suggested.
upload.php
<?php
/* PUT data goes to php://input */
echo file_get_contents("php://input");
Then use the following curl command line:
curl --upload -H "X-TOKEN: test123" a.txt http://localhost/upload.php
Update after comments: Using the --upload option should fit your needs. In difference to -X PUT together with -F, the data will be send raw and get not multipart/form-data encoded.. One of the hidden treasures of curl ;)

Short answer
The only way in your situation is to parse raw request manually (look at this gist)
Thoughts
PUT request could be used to upload one file only (not from html upload form, but using custom request). On the server you just get raw file content and writes it into some file.
Use PUT without files (application/x-www-form-urlencoded) and parse raw request using parse_str(file_get_contents("php://input"), $_PUT);
Use POST to upload files (multipart/form-data).

Related

How can I tell the filetype of images without extension? How do they work?

I am dealing with images on the web that come without a file extension, like this:
https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w
Images like these can be found, e.g., on websites made with squarespace, like this demo: https://bedford-demo.squarespace.com/
I'm trying to download these images and store them on my server, using PHP. But how can I find out the actual URL of those images? How does this work? And how can I tell the filetype of this image? What is this sorcery?
Any hints are appreciated!
Quick answer:
To find out the Content-Type returned for any URL, look at this answer:
Get Content-Type of requested URL in PHP
Why you need the Content-Type:
Just like how not every webpage on the internet has an URL that ends with .html, images are not required to have an "extension" in their URL either.
What determines whether the browser will treat the data retrieved from the URL as an image is the Content-Type header in the HTTP response.
The URL you posted returns the following HTTP headers:
For HTML documents the Content-Type is text/html. You can inspect the headers as you browse by opening the Network tab of the developer console in your browser. Look for the "response headers".
You can get the mime type of the file with getimagesize:
<?php
$size = getimagesize("https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9".
"/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w");
print_r($size["mime"]);
?>
Prints:
image/jpeg

WinINET and PHP php://input POST read

I have a weird problem. I want to upload some data with WinInet to a PHP script.
When I upload the data at once with HttpSendRequest(), then PHP reads the uploaded data correctly, e.g.
$entityBody = file_get_contents('php://input');
When I upload the data in parts with HttpSendRequestEx() and InternetWriteFile() , then the same data is uploaded, but PHP fails to read the input (empty).
What could be wrong?
Is the PHP script "called" before the entire data is uploaded?
If so, how do I get the data?
Found it, Content-Length header was missing.

Adding attachments to tasks using php asana library results in empty files in task

I am trying to add attachments to a task using the php asana library (https://github.com/Asana/php-asana). I am successful in 'adding' the files, but when I open them in the task they are completely empty or corrupt.
I have tried .png, .doc, and .pdf all with the same result. My request looks like this:
$attachment = $client->attachments->createOnTask(
$task->id,
'screenshot_from_2016-07-08_140457.png',
'http://edit.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_8.png',
'image/png'
);
I have also tried using the relative path for the filename /sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_8.png but get the same result.
This is the 'example code' I used from the library, seems pretty straightforward.
// add an attachment to the task
$demoAttachment = $client->attachments->createOnTask(
$demoTask->id,
"hello world",
"upload.txt",
"text/plain"
);
Also tried using 2 versions of the the curl request in https://asana.com/developers/api-reference/attachments just to see if I could get the attachments to work at all.
First one:
curl -H "Authorization: Bearer <personal_access_token>" https://app.asana.com/api/1.0/tasks/152938418205845/attachments --form "file=#http://edit.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_12.png;type=image/png"
Resulted in
curl: (26) couldn't open file "http://edit-fca.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_12.png"
And I've 777 on the file and the folder the file is in. So I decided to remove the '#' in front of the file and then got:
{"errors":[{"message":"file: File is not an object","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}
which when I went to that URL didn't really tell me anything about the File not an object error.
A bit stuck as the php-asana library seems to at least put the files there, but they are empty. While the curl request just seems to not work at all.
Btw I'm using php 5.5.9.
I've tried out the example code in the php library and it seems to work fine. I think you may have misunderstood the arguments you are passing to createOnTask. Also, it seems you are passing it the path of a file on the internet you want to upload, but what you can actually do is pass the exact data you want to upload. I'm not familiar enough with php to show you how to get the contents of a file from the internet, but if you have a file locally you can use file_get_contents.
Let's examine the sample code and your code:
$demoAttachment = $client->attachments->createOnTask(
$demoTask->id,
"hello world", // Contents of file
"upload.txt", // The file name of the attachment
"text/plain" // encoding
);
vs
$attachment = $client->attachments->createOnTask(
$task->id,
'screenshot_from_2016-07-08_140457.png', // This should be your image data- but like encoded the right way
'http://edit.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_8.png', // this should be the filename from above
'image/png'
);
Heres an example of how you can do this with an image in the same folder.
$demoAttachment = $client->attachments->createOnTask(
$demoTask->id,
file_get_contents("someimage.png"), // Contents of file
"upload.png", // The file name of the attachment
"image/png" // encoding
);
For reference, see https://asana.com/developers/api-reference/attachments.

getting file information via url in php

like file upload there are
<?php
$_FILES['file']['tmp_name'];
$_FILES['file']['name'];
$_FILES['file']['size'];
$_FILES['file']['type'];
?>
now.
i have a file that is sitting on my other web server, and i want to get the name size and mime type of that file via url.. is this possible?..
i've alreay tried to use this code below. but it doesn't work
$url = "http://mydomain.com/myfile.rar";
filesize ( $url );
mime_content_type ( $url );
You can try native php function get_headers it's very fast way to read file data
You can't do it like this. The information you get when you use $_FILES is meta-information that is sent along with the file (and the size can even be calculated after the file is retrieved).
You cannot get this information like that, but you can download the actual file and inspect the header information to get that information. To do this, read about curl, which allows you to do HTTP requests to another server.
It might be possible to request just the headers, so you get the information without getting the file, which is obviously more efficient.
Another solution is to implement a file-info script on the other server that allows you to get the file info.
So you could request http://mydomain.com/fileinfo.php?file=myfile.rar. In fileinfo.php you can get all the file info of the given file and just echo it.

Getting filetype/filesize from stream

I'm getting file contents from a stream with
$src_file = file_get_contents("php://input");
but I need to know the filetype and filesize of the file as well. This doesn't work:
$src_type = filetype("php://input");
$src_size = count($src_file);
I suppose I could write the file and then call filetype/filesize on that, but is there a way to get filetype and filesize from a stream or contents of a variable?
To get the full length of a string, use strlen, this must work.
There's no simple way to sniff the filetype of a stream, and you shouldn't try to do this.
But since you are dealing with php://input, likely from a PUT request, clients should generally set a proper mimetype in the Content-Type header. You can get this from $_SERVER['CONTENT_TYPE'] or $_SERVER['HTTP_CONTENT_TYPE'] depending on the sapi.
In a stream, the 'type' of the file does not make any sense. A stream just represents an input of bits. So the filetype call will fail, because PHP simply cannot find the type. But php://input is not a file. It is the input given to php. In linux for example, it would be able to do the following:
ls | php file.php
In which case the input to PHP is not a file, but the output of another program.

Categories