![](/img/trans.png)
[英]how to remove special char like apostrophes/quotes from filename in php form file upload
[英]PHP: How to remove unnecessary dots in filename on file upload?
我有一個腳本可以使用PHP上傳文件。 我已經做了一些清潔工作,以刪除難看的字符。
我還想刪除文件名中的點,最后一個除外,表示文件擴展名。
有人知道我該怎么做嗎?
例如,您將如何獲得
$filename = "water.fall_blue.sky.jpg";
$filename2 = "water.fall_blue.sky.jpeg";
在兩種情況下都退貨..?
water.fall_blue.sky
使用pathinfo()
提取文件名(從PHP 5.2開始,“ filename”數組元素可用); str_replace()
所有點都刪除; 並重新粘貼文件擴展名。
這是如何完成此操作的示例:
<?php
$string = "really.long.file.name.txt";
$lastDot = strrpos($string, ".");
$string = str_replace(".", "", substr($string, 0, $lastDot)) . substr($string, $lastDot);
?>
它像這樣轉換文件名:
true.long.file.name.txt-> reallongfilename.txt
在這里檢查: 示例
[編輯]更新了腳本,現在緩存點位置
FILENAME = this / is(您的file.name.JPG
$basename=basename($_FILES['Filedata']['name']);
$filename=pathinfo($basename,PATHINFO_FILENAME);
$ext=pathinfo($basename,PATHINFO_EXTENSION);
//replace all these characters with an hyphen
$repar=array(".",","," ",";","'","\\","\"","/","(",")","?");
$repairedfilename=str_replace($repar,"-",$filename);
$cleanfilename=$repairedfilename.".".strtolower($ext);
結果=這是您的文件名.jpg
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.