Get mime type of external file using cURL and php
PHP curl_getinfo()
<?php
# the request
$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
# get the content type
echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
# output
text/html; charset=ISO-8859-1
?>
curl
curl -I http://www.google.com
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
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.', '<code>allow_url_fopen</code>', '<code>cURL</code>' );
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
solution
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec ($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo $content_type;
Thanks to Mario!
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);
Related Topics
Set Active State on Navigation Dynamically
PHP Function That Creates a Nested Ul Li
Use Openssl_Encrypt to Replace Mcrypt for 3Des-Ecb Encryption
How So I Convert This Sha256 + Base64 from Swift to PHP
Connect to PHP Server Using Swift
Data from Two Tables with Same Column Names
PHP Session for Tracking Unique Page Views
Insert Query Check If Record Exists - If Not, Insert It
Forget Password Page, Creating a Generated Password to Email to the User
Woocommerce: Display Some Reviews Randomly on Home Page
How to Query Employee Details and Relate Their Performance Metrics
How to Define Global Functions in PHP
Performance of Static Methods VS. Functions
$Stmt->Execute():How to Know If Db Insert Was Successful
How to Define a Laravel Route with a Parameter That Contains a Slash Character