Content types for file upload - php

I wanted to upload a file(any format) to an api. If I upload the file using multipart/form-data then the file gets uploaded.
I wanted to upload the file using application/json as the Content-type in the header.
Could you'll tell me if this method is possible/allowed?
Which are Content-type besides multipart/form-data which supports file upload?
Is there a single standard for content type which can be used for get, put, post etc.
Thanks in Advance.

Only multipart/form-data can trigger the population of the PHP $_FILES global, otherwise you're going to need to nest your attachment data in your API request if using something like JSON or XML (base64_encoding comes to mind)
You could also process a PUT request - http://www.php.net/manual/en/features.file-upload.put-method.php

RFC 4627:
The MIME media type for JSON text is application/json

Related

Multipart PUT in PHP (data and files)

I have been tasked with building the API for an existing mobile app. App is sending multipart data and files in the same PUT request. As an example, there is /api/employee/personal-info PUT endpoint (notice no ID in the URI) which is a multipart - JSON data and 2 images.
PHP does not have native support for PUT, it doesn't nicely put everything in $_FILES and $_POST so I have to decode the input manually.
I first have to do file_get_contents("php://input") which gives me the raw data. I have to use regex to extract the boundary string, then I need to split the input into blocks using that boundary (and regex again) and then decide whether the block is JSON or a file, by looking at Content-Disposition on each of the blocks. If it is a file, I have to regex out the filename and extension, and populate $_FILES array manually.
Json bloks have those headers (inside the body, just before the actual data)
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=model
and file blocks only have this:
Content-Disposition: form-data; name=file; filename=IMG_20180208_1.jpg; filename*=utf-8’’IMG_20180208_1.jpg
So the Content-Disposition of the whole input is multipart/form-data but then each of the blocks have their own headers, depending whether it is a file or JSON data.
Is this really the only way to do it in PHP?
Should the PUT endpoints NOT be multipart as a rule when it comes to PHP?
Am I missing something?

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

Copy images in Google Drive to own server using PHP

I have a folder in Google Drive with product images (the folder is public). On request I want these images to be uploaded to my website.
Is there a simple way to copy all images to my own server using PHP, whitout having to set my mind into Google Drive API?
This gives me nothing to work with:
<?php
$url = "https-link-to-the-public-folder";
$html= file_get_contents($url);
print_r($html);
?>
The most straightforward method for uploading a file is by making a simple upload request. This option is a good choice when:
The file is small enough to upload again in its entirety if the connection fails.
There is no metadata to send. This might be true if you plan to send metadata for this resource in a separate request, or if no metadata is supported or available.
To use simple upload, make a POST or PUT request to the method's /upload URI and add the query parameter uploadType=media. For example:
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media
The HTTP headers to use when making a simple upload request include:
Content-Type. Set to one of the method's accepted upload media data types, specified in the API reference.
Content-Length. Set to the number of bytes you are uploading. Not required if you are using chunked transfer encoding.
The following example shows the use of a simple upload request for the Drive API.
POST /upload/drive/v3/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: number_of_bytes_in_file
Authorization: Bearer your_auth_token
JPEG data

How to create a image when a different header is already set (for a JSON response)

I am writing a simple PHP file to upload a file on POST, and to also create a new image and include the path to that new image in the response.
For the JSON response I have set headers in the main PHP file that the POST is sent to - like so:
header("Content-Type: application/json");
Just before my JSON encoded echo this is.
Above that part I call a function from another PHP file that uploads the POST file, and one that is called after that rendering a different image using that uploaded file.
In that function in order to create the PNG file I need put on the disc I have to declare a new header right? Like so:
header("Content-type: image/png");
I then ofc get the doublet header problem.
How can I get around this problem?
I cannot use header_remove() BTW.
"In that function in order to create the PNG file I need put on the
disc I have to declare a new header right?"
No.
Headers should only be sent to browsers. You don't need to send a header to write to disc.
In that function in order to create the PNG file I need put on the
disc I have to declare a new header right?
You don't have to declare a new header. As Danack pointed out, headers are only used for sending information to the browser. For instance, if you want to output the binary data of that image and then call that header, it will render the image in the browser. For writing the image to disk, it's fairly simple.
<?php
move_uploaded_file($_FILES['image']['tmp_name'], 'Path/To/File/Location/'.$_FILES['image']['name']);
?>
And that should do it.

drag drop file upload with file api server side code

I am trying to make drag drop file upload using FILE api and folowing this tutorial
https://developer.mozilla.org/en/Using_files_from_web_applications
its works fine. when i see ajax request "POST" data with firebug i see something like this
--30000 Content-Disposition: form-data; name='fileId'; filename='header.jpg' Content-Type: application/octet-stream ÿØÿà �JFIF��H�H��ÿÛ�C� $.' ",#(7),01444'9=82<.342ÿÛ�C
How can i save this data in a image format at server end ??
You can check live demo # http://www.amitpatil.me//demos/in_progress/gmail_fupload/file.html
You need to use the global $_FILES. It contains all the information about the uploaded Data.
Have a look at http://www.php.net/manual/en/features.file-upload.post-method.php.
In your case you will get the tmp. uploaded-file by using $_FILES['fieldID']['tmp_name'].

Categories