简体   繁体   English

如何在php表单文件上传中从文件名中删除特殊字符,如撇号/引号

[英]how to remove special char like apostrophes/quotes from filename in php form file upload

i have problem in file upload with filename vikas's photo.jpg.i have to upload and insert filename (viaks's photo.jpg)into mysql database table using php script.我在使用文件名 vikas 的 photo.jpg 上传文件时遇到问题。我必须使用 php 脚本上传并插入文件名(viaks 的 photo.jpg)到 mysql 数据库表中。 filename can be handled at client side if possible using jquery.如果可能,可以使用 jquery 在客户端处理文件名。

You have two choices for this situation.对于这种情况,您有两种选择。 Either escape the quote or remove the quote.要么转义引号,要么删除引号。 If you escape the single quote then your file name will look like vikas\\'s photo.jpg So it can be inserted to database without any problem.如果您对单引号进行转义,那么您的文件名将看起来像vikas\\'s photo.jpg所以它可以毫无问题地插入到数据库中。 If you remove single quote then your file name will become vikass photo.jpg如果您删除单引号,那么您的文件名将变为vikass photo.jpg

If you want to escape the quote then you can use mysql_real_escape_string($string) since mysql_* functions are depreciated you can also use如果你想转义引号,那么你可以使用mysql_real_escape_string($string)因为 mysql_* 函数被贬低你也可以使用

mysqli_real_escape_string ( mysqli $link , string $escapestr )

in your case在你的情况下

$oldFileName = $_FILES['file']['name'];
$fileName = mysql_real_escape_string($oldFileName);

If you want to replace quotes then you can use $fileName = str_replace("'","", $oldFileName);如果要替换引号,则可以使用$fileName = str_replace("'","", $oldFileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM