drag drop file upload with file api server side code - php

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'].

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

What is the best way to send image file to server: as base64 or image file?

I have an app with REST on AngularJS/PHP(Yii);
I'm not good in backend.
I'm interesting what is the best way to upload a picture to server.
I'm using Angular Material directive and get image in base64 format.
Should I post image as base64?
Or it's better to convert it and what format?
Or maybe (It's not my opinion) to upload it from html form directly
to backend using Yii?
I think you trying to upload an image file from restangular,
Try the following code:
function upload($scope.image) {
var formData = new FormData();
formData.append($scope.image);
Restangular.one('/* your url*/').withHttpConfig({transformRequest:angular.identity})
.customPOST(formData, '', undefined, {'Content-Type': undefined}) }
by setting the content type undefined browser will automatically detect file type
that will handle your UI side. I have less idea about yii framework, but in following you can get the image file by using following type:
java : #ResponseBy : multipart/form-data,
php : $_file
I think its better to upload file using form-data.
If you plan to display those uploaded images on a website or mobile application, you can use ImageKit.io. Easy API to upload and you get an URL for displaying that image. It will take care of optimization and also provides URL-based parameters for resizing, cropping etc.
If you have a base64 data url representation of the file as you are saying, then you can convert that into a file Blob, add a name to it and upload the Blob as data file.

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

Content types for file upload

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

Axis webcam http upload to php

Hi all i'm working on an axis p1343.
I tested the ftp upload succesfully.
Now i need to work with http upload to handle images seen that i do not need to keep ol images. So i'm writing a php script to hande the image buffer from that web cam.
After some work i realized that the cam was not sending anything.
So i created e really basic script to just test the jpg stream.
I receive a request with no data inside.
Does anyone know how that camera send the stream in the HU (Httpp Upload) mode ?
Thanks.
SOLVED!
Solution
to download data sent from the cam just use file_get_contents("php://input") so u can access the data stream a retrieve the data that cam sends as an attachment to the request.
This happens because the axis webcam use to send file as an attachment to the request simulating a "download link" request in a browser.
So to catch up those damn bits u have to read the php://input stream ....
It took some time time to figure out this workaround ... hope this will help you.
<?php
file_put_contents('image.jpg', file_get_contents('php://input'));
?>

Categories