本文介紹了使用 PHP 重命名上傳的文件但保留擴展名的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我正在使用 PHP 將圖像從表單上傳到服務器,并想重命名圖像 lastname_firstname.[原始擴展名].我目前有:
I'm using PHP to upload an image from a form to the server and want to rename the image lastname_firstname.[original extension]. I currently have:
當然,它會重命名文件 lastname_firstname 而不帶擴展名.如何重命名文件但保留擴展名?
which, of course, renames the file lastname_firstname without an extension. How do I rename the file but keep the extension?
謝謝!
推薦答案
你需要先找出原來的擴展名是什么;-)
You need to first find out what the original extension was ;-)
要做到這一點,pathinfo
函數可以創造奇跡;-)
To do that, the pathinfo
function can do wonders ;-)
引用手冊中給出的示例:
Quoting the example that's given in the manual :
會給你:
作為旁注,不要忘記安全性:
As a sidenote, don't forget about security :
- 在你的情況下,你應該轉義
$_POST[lastname]
,以確保它只包含 valid 字符- 順便說一句,你應該使用
$_POST['lastname']
-- 見 為什么$foo[bar]
錯了?
- In your case, you should escape
$_POST[lastname]
, to make sure it only contains valid characters- And, BTW, you should use
$_POST['lastname']
-- see Why is$foo[bar]
wrong?
- 參見
mime_content_type
用于 PHP <5.3 - 和/或
finfo_file
對于 PHP >= 5.3
這篇關于使用 PHP 重命名上傳的文件但保留擴展名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持! - And, BTW, you should use
- 順便說一句,你應該使用