- Get Mime Type of External File Using Curl and PHP
- How to get the mime type of a file after using file_get_contents from a remote server
- Download external file with PHP fopen and/or cURL
- php/curl - get extension of image on remote server using headers
- Send file via cURL from form POST in PHP
- Get mime type of external file using cURL and php
- Get mime type of external file using cURL and php
- PHP curl_getinfo()
- curl
- output
- PHP get remote file mime-type
- How do I get the MIME type of a file basing on its content and not its extension?
- How to get the mime content type from 'php://input' in PHP
- Sending MIME type files using cURL in PHP
- Sending simple type files using cURL in PHP
Get Mime Type of External File Using Curl and PHP
How to get the mime type of a file after using file_get_contents from a remote server
Use cURL instead of file_get_contents, then you can see the response header, which will hopefully have the mime type.
Or you could try using this http://www.php.net/manual/en/ref.fileinfo.php or this deprecated function http://php.net/manual/en/function.mime-content-type.php
Download external file with PHP fopen and/or cURL
You can try this process as well, I am assuming that your source url is $sourceUrl and destination/ path to save file is $destinationPath
$destFilename = 'my_file_name.ext';
$destinationPath = 'your/destination/path/'.$destFilename;
if(ini_get('allow_url_fopen')) <
if( ! @file_put_contents($destinationPath, file_get_contents($sourceUrl))) $http_status = $http_response_header[0];
sprintf('%s encountered while attempting to download %s',$http_status, $sourceUrl );
break;
>
> elseif(function_exists('curl_init')) $ch = curl_init($sourceUrl);
$fp = fopen($destinationPath, "wb");
$options = array(
CURLOPT_FILE => $fp,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_TIMEOUT => 120); // in seconds
curl_setopt_array($ch, $options);
curl_exec($ch);
$http_status = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
curl_close($ch);
fclose($fp);
//delete the file if the download was unsuccessful
if($http_status != 200) unlink($destinationPath);
sprintf('HTTP status %s encountered while attempting to download %s', $http_status, $sourceUrl );
>
> else <
sprintf('Looks like %s is off and %s is not enabled. No images were imported.', 'allow_url_fopen
', 'cURL
' );
break;
>
You can use curl_getinfo($ch, CURLINFO_CONTENT_TYPE); in case of curl to get the file info and use it as per your requirement.
php/curl - get extension of image on remote server using headers
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec ($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo $content_type;
Send file via cURL from form POST in PHP
Here is some production code that sends the file to an ftp (may be a good solution for you):
// This is the entire file that was uploaded to a temp location.
$localFile = $_FILES[$fileKey]['tmp_name'];
$fp = fopen($localFile, 'r');
// Connecting to website.
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "email@email.org:password");
curl_setopt($ch, CURLOPT_URL, 'ftp://@ftp.website.net/audio/' . $strFileName);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'CURL_callback');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
curl_exec ($ch);
if (curl_errno($ch))
$msg = curl_error($ch);
>
else
$msg = 'File uploaded successfully.';
>
curl_close ($ch);
$return = array('msg' => $msg);
echo json_encode($return);
Get mime type of external file using cURL and php
Solution 1: Suppose you have the file binary source sent in a post request JSON encoded or any encoding method you could get the MIME type like in the following piece of code Hint :Its better to send the file mime type in the request as encoding and decoding may cause not get the MIME type. Solution 3: When checking only the of the remote server you're trusting it: The remote server could be malicious and serve a wrong Content-Type on purpose Content-Type is generated using the file extension, so a malicious user could store a wrong file type with an authorized extension to bypass your security A safer approach is download the file in a temporary folder and then use to check the MIME-type locally using .
Get mime type of external file using cURL and php
PHP curl_getinfo()
curl
output
HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Fri, 09 Apr 2010 20:35:12 GMT Expires: Sun, 09 May 2010 20:35:12 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219
You can use a HEAD request via curl. Like:
$ch = curl_init(); $url = 'http://sstatic.net/so/img/logo.png'; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $results = explode("\n", trim(curl_exec($ch))); foreach($results as $line) < if (strtolower(strtok($line, ':')) == 'content-type') < $parts = explode(":", $line); echo trim($parts[1]); >>
Which returns: image/png
If you are ok with a more elegant Zend Framework version, here is a class which makes use of Zend_Http_Client component.
$sniffer = new Smartycode_Http_Mime(); $contentType = $sniffer->getMime($url);
Get mime type of external file using cURL and php,
PHP get remote file mime-type
HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Fri, 09 Apr 2010 20:35:12 GMT Expires: Sun, 09 May 2010 20:35:12 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219
To actually confirm if the file contains actual MP3 data or any other media format, I use getID3().
Make a HEAD request. See what Content-Type the server claims it is.
This stackoverflow question discusses accessing HTTP headers via PHP.
When checking only the Content-Type of the remote server you're trusting it:
- The remote server could be malicious and serve a wrong Content-Type on purpose
- Content-Type is generated using the file extension, so a malicious user could store a wrong file type with an authorized extension to bypass your security
A safer approach is download the file in a temporary folder and then use mime_content_type to check the MIME-type locally using magic.mime .
This technique only use the magic number , only reading the first bytes (header) of the file, that can be bypassed.
The most secure approach is to use a library that verify that the given file is valid for the required type it should be.
How to check mimetype from uploaded file in php, You can use mime_content_type built in function like this:
How do I get the MIME type of a file basing on its content and not its extension?
Suppose you have the file binary source sent in a post request JSON encoded or any encoding method you could get the MIME type like in the following piece of code
$data = base64_decode(trim($_REQUEST['file'])); $finfo = finfo_open(); $mime_type = finfo_buffer($finfo, $data, FILEINFO_MIME_TYPE); finfo_close($finfo); // get file type png. jpg $ext = $mime_type ? str_replace('image/', '', $mime_type) : 'png';
Hint :Its better to send the file mime type in the request as encoding and decoding may cause not get the MIME type.
Using file_get_contents
$buffer = file_get_contents($url); $finfo = new finfo(FILEINFO_MIME_TYPE); $ext = $finfo->buffer($buffer);
More information found here
Save the file using file_save_data to be a managed file and you could then use it as a field in any entity
You can easily get the MIME type of a file using mime_content_type() function.
$mime_type = mime_content_type($path_to_the_file);
I've just tested it on my local machine; placed a test.pdf file in my home folder and renamed it giving it no and an incorrect extension. In both cases I got the right MIME type:
$ ls test.pdf $ drush eval 'var_dump(mime_content_type("/home/elin/test.pdf"))' string(15) "application/pdf" $ mv test.pdf test $ drush eval 'var_dump(mime_content_type("/home/elin/test"))' string(15) "application/pdf" $ mv test test.jpg $ drush eval 'var_dump(mime_content_type("/home/elin/test.jpg"))' string(15) "application/pdf" $ mv test.jpg test.png $ drush eval 'var_dump(mime_content_type("/home/elin/test.png"))' string(15) "application/pdf"
How to get MIME-type of an image with file_get_contents in PHP, If you download a file using HTTP, do not guess (aka autodetect) the MIME type. Even if you downloaded the file using file_get_contents
How to get the mime content type from 'php://input' in PHP
Thank you Cyclone, I did this:
To save in a temporary file :
$temp_file = tempnam(sys_get_temp_dir(), 'Tux'); file_put_contents($temp_file , $source);
To get the mime content type :
$finfo = finfo_open(FILEINFO_MIME_TYPE); $content_type = finfo_file($finfo, $temp_file);
How to get the mime content type from 'php://input' in PHP, Can't you just save the file in the temporary directory, then use mime_content_type($tmp_name) and then choose if you like to remove the file or
Sending MIME type files using cURL in PHP
In this post we will show you Sending MIME type files using cURL in PHP, hear for Sending MIME type files using cURL in PHP we will give you demo and example for implement.
The cURL functions in PHP can be used to make HTTP requests to remote servers. The curl_setopt function allows a wide range of options to be set, but none of these directly relate to sending MIME type files. Instead, MIME type files are sent using a special format for POST parameter values.
The MIME type of the file sent in the cURL request can be specified in a similar way. Extending the previous example, the following code also sends the MIME type of the uploaded file:
// initialise the curl request $request = curl_init('https://onlinecode.org/'); // send a file curl_setopt($request, CURLOPT_POST, true); curl_setopt( $request, CURLOPT_POSTFIELDS, array( 'file' => '@' . $_FILES['files']['tmp_name'] . ';filename=' . $_FILES['files']['name'] . ';type=' . $_FILES['files']['type'] )); // output the response curl_setopt($request, CURLOPT_RETURNTRANSFER, true); echo curl_exec($request); // close the session curl_close($request);
Sending simple type files using cURL in PHP
The following code makes a request to https://onlinecode.org/, uploads the file sample.txt, and outputs the response.
// initialise the curl request $request = curl_init('https://onlinecode.org/'); // send a file curl_setopt($request, CURLOPT_POST, true); curl_setopt( $request, CURLOPT_POSTFIELDS, array( 'file' => '@' . realpath('sample.txt') )); // output the response curl_setopt($request, CURLOPT_RETURNTRANSFER, true); echo curl_exec($request); // close the session curl_close($request);
Hope this code and post will helped you for implement Sending MIME type files using cURL in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org