前端代码:

<form action="upload.php" enctype="multipart/form-data" method="post">
上传文件<input type="file" name="myfile"/>
<input type="submit" value="上传"/>
</form>

  

后端代码:

$upload_path = dirname(__FILE__).'/upload/';

//var_dump($_FILES);exit;
if(!is_dir($upload_path)) {
mkdir($upload_path);
} $file_type = get_ext($_FILES['myfile']['name']);
echo $file_type;
if($file_type!="xls" && $file_type!='xlsx') {
echo "只支持excel导入";
exit();
}
//判断是否上传成功(是否使用post方式上传)
if(is_uploaded_file($_FILES['myfile']['tmp_name'])) {
//把文件转存到你希望的目录(不要使用copy函数)
$uploaded_file=$_FILES['myfile']['tmp_name']; //$move_to_file=$user_path."/".$_FILES['myfile']['name'];
$file_true_name=$_FILES['myfile']['name'];
$move_to_file = $upload_path."/".time().rand(1,1000).$file_type;
//echo "$uploaded_file $move_to_file";
if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))) {
echo $_FILES['myfile']['name']."上传成功";
} else {
echo "上传失败";
}
} else {
echo "上传失败";
} function get_ext($file_name){
$build_file_arr = explode('.', $file_name);
return array_pop($build_file_arr);
}

  

文件上传demo的更多相关文章

  1. WebSite 文件上传Demo

    知识点: 1 <!--上传文件时:        1.必须使用Post方式来提交数据        2.必须设置表单的enctype属性        3.必须在表单中包含文件域.input t ...

  2. shutil模块和几种文件上传Demo

    一.shutil模块 1.介绍 shutil模块是对os中文件操作的补充.--移动 复制 打包 压缩 解压 2.基本使用 1. shutil.copyfileobj(文件1, 文件2, 长度) 将文件 ...

  3. 基于tornado的文件上传demo

    这里,web框架是tornado的4.0版本,文件上传组件,是用的bootstrap-fileinput. 这个小demo,是给合作伙伴提供的,模拟APP上摄像头拍照,上传给后台服务进行图像识别用,识 ...

  4. Spring文件上传Demo

    package com.smbea.controller; import java.io.File; import java.io.FileOutputStream; import java.io.I ...

  5. java文件上传Demo

    说到文件上传我们要做到: 1.引入两个包:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 2.将form改为上传文件模式:enctype=" ...

  6. springMVC+uploadify3.1 文件上传 demo

    uploadify3.1 api 可参考:(点击打开链接) 需要springmvc的jar包 1.upload.jsp(主要代码) <script type="text/javascr ...

  7. 多文件上传demo

    @ApiOperation(value = "批量上传", notes = "批量上传", httpMethod = "POST") @Po ...

  8. OSS阿里云文件上传 demo。

    所需jar包: aliyun-openservices-1.2.3.jar jdom-1.1.jar commons-codec-1.4.jar commons-logging-1.1.1.jar g ...

  9. nodejs 简单http 文件上传demo

    // 这是一个简单的Node HTTP,能处理当前目录的文件 // 并能实现良种特殊的URL用于测试 // 用http://localhost:8000 或http://127.0.0.1:8000 ...

随机推荐

  1. 使用IDEA进行打包

    使用IDEA打jar包: 1.

  2. Hash 分布均衡算法

    1.移位实现 public static int GetIndex(string str, int count) { , (current, c) => (current << ) ...

  3. java基础33 Set集合下的HashSet集合和TreeSet集合

    单例集合体系: ---------| collection  单例集合的根接口--------------| List  如果实现了list接口的集合类,具备的特点:有序,可重复       注:集合 ...

  4. group by 并且 count(1)的linq写法

    SELECT [MobleNo],count(1) FROM [CustMobleNo] group by [MobleNo] GO ===作用等于=== var rst = from c in da ...

  5. Vue select 下拉菜单

    1.html <div id="app-8"> <select v-model="selected"> <option v-for ...

  6. linux 系统网卡无法识别,缺少驱动

    #linux网卡驱动安装# Linux设备加载 #lsmod Module Size Used by e1000e 查看硬件设备 ls /usr/share/hwdata 查看pci网卡设备 lspc ...

  7. svn 批量添加命令

    svn st | awk '{if ($1 == "?") {print $2} }' | xargs svn add

  8. 20155225 实验二《Java面向对象程序设计》实验报告

    20155225 实验二<Java面向对象程序设计>实验报告 一.单元测试 三种代码 知道了伪代码.产品代码.测试代码的关系和用途,并根据老师的例子,按测试代码调试了产品代码. 值得注意的 ...

  9. Spark(五)Spark任务提交方式和执行流程

    一.Spark中的基本概念 (1)Application:表示你的应用程序 (2)Driver:表示main()函数,创建SparkContext.由SparkContext负责与ClusterMan ...

  10. USACO 5.3 Big Barn

    Big BarnA Special Treat Farmer John wants to place a big square barn on his square farm. He hates to ...