在使用 PHP 上传文件之前替换特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9838994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:45:13  来源:igfitidea点击:

Replace special characters before the file is uploaded using PHP

phpspecial-characters

提问by 125369

I was wondering if it is possible to change the name of the file to be uploaded. I mean what I am trying to do is that, the user uploads a file which may have some special characters like special characters in some European languages.

我想知道是否可以更改要上传的文件的名称。我的意思是我想要做的是,用户上传一个文件,该文件可能包含一些特殊字符,例如某些欧洲语言中的特殊字符。

What I am planning to do is that before using the move_uploaded_file command is it possible to change/preg_replace the special characters with normal characters, so that the file is uploaded and stored with the new name which has only normal characters.

我打算做的是,在使用 move_uploaded_file 命令之前,是否可以用普通字符更改/preg_replace 特殊字符,以便使用只有普通字符的新名称上传和存储文件。

回答by 472084

// Get the original file name from $_FILES
$file_name= $_FILES['file']['name'];

// Remove any characters you don't want
// The below code will remove anything that is not a-z, 0-9 or a dot.
$file_name = preg_replace("/[^a-zA-Z0-9.]/", "", $file_name);

// Get the location of the folder to upload into
$location = 'path/to/dir/';

// Use move_uploaded_file()
move_uploaded_file($_FILES["file"]["tmp_name"], $location.$file_name);

回答by joni_demon

try to use this bro

尝试使用这个兄弟

   $result = iconv("UTF-8", "ASCII//TRANSLIT", $text);

to know more visit how to replace special characters with the ones they're based on in PHP?

要了解更多信息,请访问如何用它们在 PHP 中基于的特殊字符替换特殊字符?

回答by Jon

You can get the original filename for an uploaded file from $_FILES, and you can create your "special" version by replacing characters in it with strtr(which sounds as the best match for this case), str_replace, preg_replaceor any other string processing function.

您可以从 获取上传文件的原始文件名$_FILES,您可以通过将其中的字符替换为strtr(这听起来最适合这种情况)str_replacepreg_replace或任何其他字符串处理函数来创建您的“特殊”版本。

The best approach depends on what exactlyyou want to do.

最好的方法取决于你到底想做什么。

回答by slash197

You could do it like this, write a simple function strip_special_chars()to replace characters you want in a string

你可以这样做,写一个简单的函数strip_special_chars()来替换你想要的字符串中的字符

$tmp_name = $_FILES["file"]["tmp_name"];
$name = strip_special_chars($tmp_name);
move_uploaded_file($name, "path/to/dir/");

回答by canerkoroglu

Also you can use a function for special characters like this:

您也可以使用特殊字符的函数,如下所示:

function safename($theValue)
{
    $_trSpec = array(
        '?' => 'C', 
        '?' => 'G', 
        '?' => 'I',
        '?' => 'O', 
        '?' => 'S', 
        'ü' => 'U',
        '?' => 'c', 
        '?' => 'g', 
        '?' => 'i',
        'i' => 'i',
        '?' => 'o', 
        '?' => 's', 
        'ü' => 'u',
    );
    $enChars = array_values($_trSpec);
    $trChars = array_keys($_trSpec);
    $theValue = str_replace($trChars, $enChars, $theValue); 
    $theValue=preg_replace("@[^A-Za-z0-9\-_.\/]+@i","-",$theValue);
    $theValue=strtolower($theValue);
    return $theValue;
}

Be carefull about allow . for file extension.

小心 allow 。为文件扩展名。

And then change your original temp file name,

然后更改您的原始临时文件名,

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetFile = safename($targetFile);

$location = 'path/to/dir/';
move_uploaded_file($_FILES["file"]["tmp_name"], $location.$targetFile);