php文件上传 覆盖上传_使用PHP上传基本文件

php文件上传 覆盖上传

I create a lot of websites that allow administrators to upload files to their own website. Since allowing user customization has become more and more important on websites these days, I thought I'd share how easy it is to handle file uploads in PHP.

我创建了许多网站,允许管理员将文件上传到自己的网站。 由于近来允许用户自定义在网站上变得越来越重要,所以我想分享一下使用PHP处理文件上传的难易程度。

XHTML表单 (The XHTML Form)


<form action="accept-file.php" method="post" enctype="multipart/form-data">
	Your Photo: <input type="file" name="photo" size="25" />
	<input type="submit" name="submit" value="Submit" />
</form>


You'll need to use the multipart/form-data value for the form's enctype property. You'll also obviously need at least one input element of the file type. The form's action tag must provide a URL which points the a file containing the piece of PHP below.

您需要将multipart / form-data值用于表单的enctype属性。 显然,您还至少需要一个文件类型的输入元素。 表单的动作标签必须提供一个URL,该URL指向一个包含下面PHP片段的文件。

PHP (The PHP)

//if they DID upload a file...
if($_FILES['photo']['name'])
{
	//if no errors...
	if(!$_FILES['photo']['error'])
	{
		//now is the time to modify the future file name and validate the file
		$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
		if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
		{
			$valid_file = false;
			$message = 'Oops!  Your file\'s size is to large.';
		}
		
		//if the file has passed the test
		if($valid_file)
		{
			//move it to where we want it to be
			move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
			$message = 'Congratulations!  Your file was accepted.';
		}
	}
	//if there is an error...
	else
	{
		//set that to be the returned message
		$message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
	}
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']

My commenting in the PHP above outlines the way the process works, so I'll just mention a few notes about file uploads in PHP:

我在上面PHP中的评论概述了该过程的工作方式,因此,我仅提及有关PHP中文件上传的一些注意事项:

  • Many shared hosting servers allow a very low maximum file upload size. If you plan on accepting larger files, you should consider a dedicated or virtual dedicated server.

    许多共享的托管服务器允许非常小的最大文件上传大小。 如果计划接受较大的文件,则应考虑使用专用服务器或虚拟专用服务器。
  • To adjust the file upload size in PHP, modify the php.ini file's "upload_max_filesize" value. You can also adjust this value using PHP's .ini_set() function.

    要在PHP中调整文件上传大小,请修改php.ini文件的“ upload_max_filesize”值。 您还可以使用PHP的.ini_set()函数来调整此值。

  • The file upload counts towards the hosting environment's $_POST size, so you may need to increase the php.ini file's post_max_size value.

    文件上载将计入托管环境的$ _POST大小,因此您可能需要增加php.ini文件的post_max_size值。

  • Be sure to do a lot of file validation when allowing users to upload files. How horrible would it be to allow a user to upload a .exe file to your server? They could do horrible things on the server.

    允许用户上传文件时,请确保进行大量文件验证。 允许用户将.exe文件上传到您的服务器有多可怕? 他们可能在服务器上做可怕的事情。

翻译自: https://davidwalsh.name/basic-file-uploading-php

php文件上传 覆盖上传

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值