PHP函式:CURLFile::setMimeType()
適用版本:PHP 5 >= 5.5.0, PHP 7
用法: CURLFile::setMimeType() 方法用於設定 CURLFile 物件的 MIME 型別。MIME 型別指示檔案的內容型別。
語法: public void CURLFile::setMimeType ( string $mime )
引數:
- $mime:表示要設定的檔案的新 MIME 型別。它應該是一個有效的 MIME 型別字串。
示例:
// 建立 CURLFile 物件
$file = new CURLFile('path_to_file/filename.txt', 'text/plain', 'file.txt');
// 獲取原始的 MIME 型別
echo "原始的 MIME 型別:" . $file->getMimeType() . "\n";
// 設定新的 MIME 型別
$file->setMimeType('image/jpeg');
// 獲取更新後的 MIME 型別
echo "更新後的 MIME 型別:" . $file->getMimeType() . "\n";
以上示例中,首先使用 new CURLFile()
建立一個 CURLFile 物件,指定檔案路徑、原始 MIME 型別和檔名。然後透過呼叫 getMimeType()
方法獲取原始的 MIME 型別,並列印輸出。接下來,使用 setMimeType()
方法將檔案的 MIME 型別更改為 'image/jpeg'。最後,再次呼叫 getMimeType()
方法獲取更新後的 MIME 型別,並列印輸出。
請注意,在使用 CURLFile::setMimeType()
方法之前,您必須先建立一個 CURLFile 物件,並透過 new CURLFile()
建構函式為其提供檔案路徑、MIME 型別和檔名。