php实现文件上传

5 篇文章 0 订阅

文件上传

1.upload.html


<html>
 <head>
  <meta charset="UTF-8">
  <title>文件上传</title>
 </head>
<form action='upload.php' method='post' enctype='multipart/form-data'>
请选择文件:<input type='file' name='myfile'>
<input type='submit' name='sub'>
</form>
</html>

2.upload.php

<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
</html>
<?php
	class UploadFile{
		private $arr = array('png', 'jpg', 'gif');
		private $size = 100000;
		private $path = './upload/';
		
		private $fileName;
		private $newFileName;
		private $ftype;
		private $fsize;
		private $tempName;
		
		function __construct($file){
			$this -> fileName = $file['myfile']['name'];
			$this -> fsize = $file['myfile']['size'];
			$this -> tempName = $file['myfile']['tmp_name'];
			$this -> getFileType();
			$this -> getNewFileName();
		}
		
		private function getFileType(){
			@$fh = array_pop(explode('.', $this->fileName));
			$this -> ftype = $fh;
		}
		
		private function getNewFileName(){
			$newFileName = time() . rand(100,999) . '.' . $this->ftype;
			$this -> newFileName = $newFileName;
		}
		
		#判断后缀 checkFile()
		
		private function checkFile(){
			//$ft = array_pop(explode('.', $_FILES['myfile']['name']));
			if(!in_array($this->ftype,$this->arr)){
				die('文件类型错误,上传失败');
			
			}
		}
		
		#判断路径 checkPath()
		
		private function checkPath(){
			if(!file_exists($this->path)){
				mkdir($this->path);
			}
			
			
		}
		#判断大小 checkSize()
		
		private function checkSize(){
			if($this->fsize>$this->size){
				die('文件过大,上传失败');
			}
		}
		#共有方法 upload();
		function upload(){
			$this->checkFile();
			$this->checkPath();
			$this->checkSize();
			
			if(!@move_uploaded_file($this->tempName, $this->path . $this->newFileName)){
				die('文件上传失败');
			}echo '文件上传成功';
		
	}
	
	}
	
	
	
	
	$f=new UploadFile($_FILES);
	$f->upload();

运行html代码
在这里插入图片描述
随便上传一个文件
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单的PHP文件上传页面的示例代码: ```php <!DOCTYPE html> <html> <head> <title>文件上传</title> </head> <body> <h1>文件上传</h1> <?php if(isset($_POST['submit'])){ $uploadDir = 'uploads/'; // 上传文件存储目录 // 检查目录是否存在,不存在则创建 if(!is_dir($uploadDir)){ mkdir($uploadDir, 0777, true); } $fileName = $_FILES['file']['name']; // 获取上传文件的名称 $tempFile = $_FILES['file']['tmp_name']; // 获取临时文件的路径 $targetFile = $uploadDir . $fileName; // 目标文件的路径 // 将临时文件移动到目标位置 if(move_uploaded_file($tempFile, $targetFile)){ echo "文件上传成功!"; }else{ echo "文件上传失败!"; } } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" name="submit" value="上传" /> </form> </body> </html> ``` 在上面的代码中,我们首先检查是否提交了文件上传表单(通过检查`$_POST['submit']`是否存在)。然后,我们指定了文件的上传目录,并确保该目录存在。接下来,我们获取上传文件的名称、临时文件的路径和目标文件的路径。最后,我们使用`move_uploaded_file()`函数将临时文件移动到目标位置。如果移动成功,则显示成功消息;否则,显示失败消息。 你可以将以上代码保存为一个名为`upload.php`的文件,并将其上传到你的PHP服务器上。然后,通过访问`http://yourdomain.com/upload.php`来访问文件上传页面。用户可以选择文件并点击"上传"按钮来上传文件。上传成功后,会显示相应的消息。记得在服务器上创建一个名为`uploads`的目录,用于存储上传的文件。 请注意,这只是一个基本的文件上传示例,你可能需要添加更多的验证和安全措施来保护你的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值