百科狗-知识改变命运!
--

move_uploaded_file() - 将上传的文件移动到新位置 - php 文件目录函数

百变鹏仔1年前 (2023-11-21)阅读数 14#技术干货
文章标签文件

move_uploaded_file()

(PHP 4 >= 4.0.3, PHP 5, PHP 7)

将上传的文件移动到新位置

说明

move_uploaded_file(string $filename,string $destination): bool

本函数检查并确保由$filename指定的文件是合法的上传文件(即通过 PHP 的 HTTP POST 上传机制所上传的)。如果文件合法,则将其移动为由$destination指定的文件。

这种检查显得格外重要,如果上传的文件有可能会造成对用户或本系统的其他用户显示其内容的话。

参数

$filename

上传的文件的文件名。

$destination

移动文件到这个位置。

返回值

成功时返回TRUE

如果$filename不是合法的上传文件,不会出现任何操作,move_uploaded_file()将返回FALSE

如果$filename是合法的上传文件,但出于某些原因无法移动,不会出现任何操作,move_uploaded_file()将返回FALSE。此外还会发出一条警告。

范例

Uploading multiple files

注释

Note:

move_uploaded_file() - 将上传的文件移动到新位置 - php 文件目录函数

move_uploaded_file()对安全模式和open_basedir都是敏感的。不过,限制只针对$destination路径,因为允许移动上传的文件名$filename可能会与这些限制产生冲突。move_uploaded_file()仅作用于通过 PHP 上传的文件以确保这个操作的安全性。Warning

如果目标文件已经存在,将会被覆盖。

参见

  • is_uploaded_file() 判断文件是否是通过 HTTP POST 上传的
  • rename() 重命名一个文件或目录
  • 参见文件上传处理一章中的简单使用例子。
Security tips you must know before use this function :
First : make sure that the file is not empty.
Second : make sure the file name in English characters, numbers and (_-.) symbols, For more protection.
You can use below function as in example

Third : make sure that the file name not bigger than 250 characters.
as in example :

Fourth: Check File extensions and Mime Types that you want to allow in your project. You can use : pathinfo() http://php.net/pathinfo
or you can use regular expression for check File extensions as in example
#^(gif|jpg|jpeg|jpe|png)$#i
or use in_array checking as

You have multi choices to checking extensions and Mime types.
Fifth: Check file size and make sure the limit of php.ini to upload files is what you want, You can start from http://www.php.net/manual/en/ini.core.php#ini.file-uploads
And last but not least : Check the file content if have a bad codes or something like this function http://php.net/manual/en/function.file-get-contents.php.
You can use .htaccess to stop working some scripts as in example php file in your upload path.
use :
AddHandler cgi-script .php .pl .jsp .asp .sh .cgi
Options -ExecCGI 
Do not forget this steps for your project protection.
The destination directory must exist; move_uploaded_file() will not automatically create it for you.
For those which will use inotify-tools to start an event when move_uploaded_file put the file in a specific directory, be aware that move_uploaded_file will trigger the create event, and not the move event of inotify-tools.
For those using PHP on Windows and IIS, you SHOULD set the "upload_tmp_dir" value in php.ini to some directory around where your websites directory is, create that directory, and then set the same permissions on it that you have set for your websites directory. Otherwise, when you upload a file and it goes into C:\WINDOWS\Temp, then you move it to your website directory, its permissions will NOT be set correctly. This will cause you problems if you then want to manipulate that file with something like ImageMagick's convert utility.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)