使用PHP上传文件之前,请替换特殊字符 [英] Replace special characters before the file is uploaded using PHP

查看:75
本文介绍了使用PHP上传文件之前,请替换特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以更改要上传的文件的名称.我的意思是我要尝试执行的操作是,用户上载一个文件,其中可能包含一些特殊字符,例如某些欧洲语言中的特殊字符.

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.

我打算做的是,在使用move_uploaded_file命令之前,可以用常规字符来更改/准备/替换特殊字符,以便使用仅具有常规字符的新名称来上载和存储文件.

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.

推荐答案

// 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);

这篇关于使用PHP上传文件之前,请替换特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆