Home > php教程 > php手册 > body text

php文件上传简单实现方法

WBOY
Release: 2016-06-06 20:12:33
Original
1458 people have browsed it

这篇文章主要介绍了php文件上传简单实现方法,以一个完整实例形式分析了文件上传的过程,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php文件上传的简单实现方法。分享给大家供大家参考。具体如下:

文件1:index.php

复制代码 代码如下:


用户名:

密码:

上传图片:


文件2:uploadProcess.php

复制代码 代码如下:

$filetype_arr = array("image/jpeg","image/png","image/gif","image/gif");//允许上传的图片类型
if(!empty($_POST['sub'])){
 $username = $_POST['username'];//用户名
 $password = $_POST['pwd'];//密码
 
 if(is_uploaded_file($_FILES['upimage']['tmp_name'])){//判断是否是通过http上传的图片
  $tmp_name = $_FILES['upimage']['tmp_name'];//上传图片临时名
  //判断图片类型是否支持
  if(!in_array($_FILES['upimage']['type'],$filetype_arr)){
   echo "抱歉,上传图片只支持";
   foreach($filetype_arr as $value){
    echo $value.",";
   }
   echo "等类型";
   exit();
  } 
  $exten_name = substr($_FILES['upimage']['name'],strrpos($_FILES['upimage']['name'],"."));//获取上传图片的后缀名
  $dest_name = $_SERVER['DOCUMENT_ROOT']."/php/upfile/".$username."http://www.jb51.net/";//
  //判断保存的目录是否存在,,不存在则创建
  if(!file_exists($dest_name)){
   if(!mkdir($dest_name)){//如果创建不成功
    echo "创建目录失败,文件无法上传";
    exit();
   }
  }
  //如果已经存在保存的目录
  if(move_uploaded_file($tmp_name,$dest_name.time().rand(1,100).$exten_name)){//移动临时文件到保存的目录,并改名
   echo "上传图片成功";
  }else{
   echo "图片上传失败";
  }
 }else{
  echo "找不到你上传的图片,请重新上传";
 }
}
?>

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!