user1035957
user1035957

Reputation: 327

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

Upvotes: 5

Views: 4381

Answers (1)

complex857
complex857

Reputation: 20753

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='

Upvotes: 8

Related Questions