Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify content type of uploaded file in php?

I want to use an Image Upload API. It receives Image file via POST and sends me that URI of image which I uploaded. but that API sends me error If Content-Type of image is not a kind of image mime type (such as image/jpeg, image/png, ...)

But when I upload image file via FTP and use CURL as file uploads, It sends me error because Content-Type of that image is always 'application/octet-stream'. I want to modify this mime type. Is there any way to modify this?

Here is the code:

<?php
$fileUploadPostParams = Array(
    "uploadedfile" => "@/files.jpg"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://uix.kr/widget/parameter.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileUploadPostParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
?>

and here is the result:

...
===== file ===== 
Array
(
    [uploadedfile] => Array
        (
            [name] => 2312.jpg
            [type] => application/octet-stream
            ^^^^ I want to change here...
            [tmp_name] => /tmp/php5bigGV
            [error] => 0
            [size] => 383441
        )
)

sorry for bad english.. thanks

like image 550
user1035957 Avatar asked Jul 13 '12 10:07

user1035957


People also ask

How do I edit an upload in PHP?

"". $ext; $edit_file = fopen($file_name, 'w'); fwrite($edit_file, $write_text); fclose($edit_file); } if(isset($_POST['delete_file'])) { $file_name=$_POST['file_name']; $folder="files/"; $ext=". txt"; $file_name=$folder.

Which of the following provides content type of the uploaded file in PHP?

The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file name and errors associated with file.

How can I get MIME type from uploaded file in PHP?

The mime_content_type() function is an inbuilt function in PHP which is used to get the MIME content-type of a file. Parameters: This function accepts single parameter $file which specifies the path of the file which MIME details to be find. Return Value: This function returns the MIME content type or False on failure.


1 Answers

You can specify the type in the parameters like this:

$fileUploadPostParams = array(
    "uploadedfile" => "@/files.jpg;type=image/jpeg"
);

From the curl's man page:

-F accepts parameters like -F "name=contents". If you want the contents to be read from a file, use <@filename> as contents. When specifying a file, you can also specify the file content type by appending ';type='

like image 88
complex857 Avatar answered Sep 27 '22 23:09

complex857