[英]Removing special characters from filename
I'm making a website where people can upload images. 我正在建立一个人们可以上传图片的网站。 However when end-users are uploading an image that contains special characters or a foreign character, the image does upload, But It cannot be display'd on the website, Like the image doesn't exist on the server.
但是,当最终用户上传包含特殊字符或外来字符的图像时,图像会上传,但无法在网站上显示,就像服务器上不存在图像一样。
I see a lot of people answering on another post saying that I need to use preg_replace
我看到很多人在另一篇文章中回答说我需要使用
preg_replace
But the thing is, I don't really know how to use it. 但问题是,我真的不知道如何使用它。 How would I use it in my code.
我将如何在我的代码中使用它。
Here is my controller method: 这是我的控制器方法:
public function upload(Requests\CreatePostsRequest $request)
{
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['fileToUpload']["name"]);
$uploadOk = 1;
$findme = ".";
$pos = strpos($target_file, $findme);
//echo $target_file;
//dd($_POST);
echo ini_get('upload_max_filesize');
echo $_FILES["fileToUpload"]["error"];
$imageFileType = strtolower(substr($target_file,$pos+1));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
//$check = true;
//dd($check);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
$filecheck = 0;
$orgFileName = $target_file;
while (file_exists($target_file)) {
$target_file = substr($orgFileName,0,$pos).$filecheck.substr($orgFileName,$pos);
$filecheck++;
$uploadOk = 1;
}
if ($_FILES["fileToUpload"]["size"] > 500000000*8) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
echo $imageFileType;
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$post = new Posts($request->all());
$post->fileToUpload = $target_file;
Auth::user()->posts()->save($post);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
return redirect('');
echo $target_file;
}
Using preg replace: 使用preg替换:
$target_file = $target_dir . preg_replace("/[^a-z0-9\_\-\.]/i", '', basename($_FILES['fileToUpload']["name"]));
This will remove all characters that's not a letter (az), a number (0-9) or a dash, underscore or dot (we want to keep the file extension). 这将删除所有不是字母(az),数字(0-9)或破折号,下划线或点(我们希望保留文件扩展名)的字符。 The
i
flag in the end makes the match case insensitive. 最后的
i
标志使得匹配大小写不敏感。
To make the expression shorter, you can replace a-z0-9\\_
-part with the word token \\w
. 要使表达式更短,可以使用单词token
\\w
替换a-z0-9\\_
-part。
The pattern would then be: /[^\\w\\-\\.]/
. 那么模式将是:/ [
/[^\\w\\-\\.]/
。] /。 Here we don't need the i
flag, since the word token handles that for us. 在这里,我们不需要
i
标志,因为单词标记为我们处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.