多个文件上传(php+js可动态增加文件上传框)

 更新时间:2016年11月25日 16:28  点击:1370

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net1999/xhtml">

<head>
<meta content="text/html; charset=gb2312" http-equiv="content-type" />
<title>多个文件上传(php+网页特效可动态增加文件上传框)</title>
<script language="网页特效" type="text/网页特效">
function addinput()//增加input节点
{
var input=document.createelement('input');//创建一个input节点
var br=document.createelement('br');//创建一个br节点
input.setattribute('type','file');// 设置input节点type属性为file
input.setattribute('name','pic[]');//设置input节点 name属性为files[],以 数组的方式传递给服务器端
document.form1.appendchild(br);//把节点添加到 form1表单中
document.form1.appendchild(input);
}
</script>
</head>
<?php
if($_post['sub'])
{
$ftype=array('image/jpg','image /jpeg','imgage/png','image/pjpeg','image/gif');//允许上传的文件类型
$files=$_files['files'];
$fnum=count($files['name']); //取得上传文件个数
for($i=0;$i<$fnum;$i++)
{
   if($files['name'][$i]!=''&&is_uploaded_file($files['tmp_name'][$i]))
   {
    if(in_array($files['type'][$i],$ftype))//判断文件是否是允许的类型
    {
     $fname='upfile/'.rand(0,10000).time().substr($files['name'] [$i],strrpos($files['name'][$i],'.'));//自动命名
     move_uploaded_file($files['tmp_name'][$i],$fname);
     echo '<br/>文件上传成功!';
    }
    else
    {
     echo '<br/>不允许的文件类型!';
    }
   }
   else
   {
    echo '<br/>该文件不存在!';
   }
}

}
?>
<body>
<form name="form1" method="post" action="" enctype="multipart/form-data" >
    <input type="file" name="pic[]" />
<input type="submit" name="sub" value="上传"/>
</form>
<a href="#" onclick="addinput()">再上传一张</a>
</body>

</html>

 代码如下 复制代码
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
  2  <html>
  3  <head>
  4     <title>投票结果</title>
  5     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6
  7  <script type="text/网页特效">
  8
  9     function getxmlhttpobject()
 10 {
 11   var xmlhttp=null;
 12   try
 13     {
 14     // firefox, opera 8.0+, safari
 15      xmlhttp=new xmlhttprequest();
 16     }
 17   catch (e)
 18     {
 19     // internet explorer
 20     try
 21       {
 22       xmlhttp=new activexobject("msxml2.xmlhttp");
 23       }
 24     catch (e)
 25       {
 26       xmlhttp=new activexobject("microsoft.xmlhttp");
 27       }
 28     }
 29   return xmlhttp;
 30 }
 31 function checkajax(){
 32    xmlhttp=getxmlhttpobject()
 33  
 34   if (xmlhttp==null)
 35     {
 36     alert ("您的浏览器不支持ajax!");
 37     return ;
 38     }else
 39     {
 40        return xmlhttp;
 41     }
 42 }
 43 function preshow(){
 44     ajaxobj = checkajax();
 45     var url = "page.php?page=1";
 46 ajaxobj.onreadystatechange=statechanged;
 47 ajaxobj.open("get",url,true);
 48 ajaxobj.send(null);
 49 }
 50 function showhint1(str)
 51 {
 52     ajaxobj = checkajax();
 53     document.getelementbyid("msg").innerhtml="正在读取数据……";
 54
 55 var url = str;
 56 ajaxobj.onreadystatechange=statechanged;
 57 ajaxobj.open("get",url,true);
 58 ajaxobj.send(null);
 59
 60 }

 

直接用php创建word文档代码(系统无需安装word软件) 使用方法: 首先用$word->start()表示要生成word文件了。 然后你可以输出任何的HTML代码,不论是从文件读过来再写到这里, 还是直接在这里输出HTML,都没有关系。

等你输出完毕后,用$word->save($path)方法,其中$path是你想 生成的word文件的名称(可以给出完整的路径).当你使用了$word->save() 方法后,这后面的任何输出都和word文件没有关系了,也就是说word的生成 工作就完成了。之后就和你平常使用php的方式一样拉。随便你输出什么东西, 都直接在浏览器里输出,而不会写到word里面去。

*/

 代码如下 复制代码

class word

function start()
{
ob_start();
print'<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';

}

function save($path)
{

print "</html>";
$data = ob_get_contents();

ob_end_clean();

$this->wirtefile ($path,$data);
}

function wirtefile ($fn,$data)
{

$fp=fopen($fn,"wb");
fwrite($fp,$data);
fclose($fp);
}

}
//生成word调用方法
include("word.php");
$word=new word;
$word->start();

关于购物车,这个是在电子商务方面使用的比较多,用户选择好自己的商品需要保存起来,最后去收银台,这很像我们实际生活的超市,所以我现来写一个简单的php购物车实例代码,比较详细只要一步步,处理好就OK了。

些购物车会用到php文件
 main.php 显示商品
 additem.php把商品加入购物车
 cearcart.php删除购物车中的商品
 shoppingcart.php 操作类
 
用户的数据库教程有

 代码如下 复制代码
 inventory
  create table inventory (
    product tinytext not null,
    quantity tinytext not null,
    id int(4) default '0' not null auto_increment,
    description tinytext not null,
    price float(10,2) default '0.00' not null,
    category char(1) default '' not null,
    key id (id),
    primary key (id),
    key price (price)
  );
  insert into inventory values ('硬盘','5','1','80g','5600','1');
  insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
  insert into inventory values ('dvd-rom','7','3','12x','2000','1');
  insert into inventory values ('主板www.111cn.net','3','4','asus','5000','2');
  insert into inventory values ('显示卡','6','5','64m','4500','1');
  insert into inventory values ('刻录机','4','6','52w','3000','1');
 
 shopping
  create table shopping (
    session tinytext not null,
    product tinytext not null,
    quantity tinytext not null,
    card tinytext not null,
    id int(4) default '0' not null auto_increment,
    key id (id),
    primary key (id)
  );
 shopper
 
  create database shopper;
  use shopper;
  create table shopping (
    session tinytext not null,
    product tinytext not null,
    quantity tinytext not null,
    card tinytext not null,
    id int(4) default '0' not null auto_increment,
    key id (id),
    primary key (id)
  );
  create table inventory (
    product tinytext not null,
    quantity tinytext not null,
    id int(4) default '0' not null auto_increment,
    description tinytext not null,
    price float(10,2) default '0.00' not null,
    category char(1) default '' not null,
    key id (id),
    primary key (id),
    key price (price)
  );
  insert into inventory values ('硬盘','5','1','80g','5600','1');
  insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
  insert into inventory values ('dvd-rom','7','3','12x','2000','1');
  insert into inventory values ('主板111cn.net','3','4','asus','5000','2');
  insert into inventory values ('显示卡','6','5','64m','4500','1');
  insert into inventory values ('刻录机','4','6','52w','3000','1');

*/

//main.php 显示购物车所有商品

 代码如下 复制代码

include("shoppingcart.php");
$cart = new cart;
$table="shopping";

/* 查询并显示所有存货表中的信息 */
    $query = "select * from inventory";
    $invresult = mysql教程_query($query);
    if (!($invresult)) {
       echo "查询失败<br>";
       exit;
    }
    echo "以下产品可供订购∶";
    echo "<table border=0>";
    echo "<tr><td bgcolor=#aaccff>产品编号</td><td bgcolor=#aaccff>产品名称</td><td bgcolor=#aaccff>单价</td>";
    echo "<td bgcolor=#aaccff>剩余数量</td><td bgcolor=#aaccff>产品描述</td><td bgcolor=#aaccff>放入购物车</td></tr>";
    while($row_inventory = mysql_fetch_object($invresult)) {
    echo "<tr><td bgcolor=#aaccff>".$row_inventory->id."</td>";
    echo "<td bgcolor=#aaccff>".$row_inventory->product."</td>";
    echo "<td bgcolor=#aaccff>".$row_inventory->price."</td>";
    echo "<td bgcolor=#aaccff>".$row_inventory->quantity."</td>";
    echo "<td bgcolor=#aaccff>".$row_inventory->description."</td>";
    echo "<td bgcolor=#aaccff><a href='additem.php?product=".$row_inventory->product."'><img border='0' src='cart.gif' width='81' height='17'></a></td></tr>";
    }
    echo "</table>";
    echo "<br>购物车中产品的数量∶".$cart->quant_items($table, $session);
    echo "<br><br><a href='clearcart.php'><img border='0' src='car.gif'></a>清空购物车";

 

这个文件上传类可以实现多个文件或单个文件进行上传了,下面小编来给各位推荐一个不错的例子。

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<?php
//php文件上传类(该类支持单个或者多个文件上传)
 /**
 * 类名:upfile
 * 作用:处理文件上传
 * 说明,该类处理单个或者多个文件上传,使用该类时,只需要实列化该类
 * 例:
 * $up = upfile()
 * $up->update_file($_file['filename'])
 *
 * $up->update_file   函数返回一个数组,如果是多文件上传,则为多维数据。
 * 数组的内容:
 * $fileinfo['file_size']   上传文件的大小
 * $fileinfo['file_suffix'] 上传文件的类型
 * $fileinfo['file_name']   上传文件的名字
 * $fileinfo['error']     上传文件产生的错误
 *

 */
class upfile {
 public $fcount = 1;           //上传文件的数量
 public $ftype  = array('jpg','jpeg','gif','png');  //文件格式
 public $fsize  = 1024;          //文件大小单位kb
 public $fdir   = 'www.111cn.net/';         //文件存放目录
 public $errormsg = '';          //产生的临时错误信息

 /**
  *函数名:get_tmp_file($putfile)
  *作用:取得上传的临时文件名
  *@param array $putfile
  *@return string $upimg 返回临时文件名
  */
  function get_tmp_file($putfile){
  if($this->fcount == 1){
   $tmpfile = $putfile['tmp_name'];
  }else{
   for($i=0;$i<$this->fcount;$i++){
    $tmpfile[] = $putfile['tmp_name'][$i];
   }
  }
  return $tmpfile;
  }

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • Php文件上传类class.upload.php用法示例

    本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • jQuery实现简单的文件上传进度条效果

    本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24
  • AI源文件转photoshop图像变模糊问题解决教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说下AI源文件转photoshop图像变模糊问题的解决教程,各位想知道具体解决方法的使用者们,那么下面就快来跟着小编...2016-09-14
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • Zend studio文件注释模板设置方法

    步骤:Window -> PHP -> Editor -> Templates,这里可以设置(增、删、改、导入等)管理你的模板。新建文件注释、函数注释、代码块等模板的实例新建模板,分别输入Name、Description、Patterna)文件注释Name: 3cfileDescriptio...2013-10-04
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • ant design中upload组件上传大文件,显示进度条进度的实例

    这篇文章主要介绍了ant design中upload组件上传大文件,显示进度条进度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29
  • C#使用StreamWriter写入文件的方法

    这篇文章主要介绍了C#使用StreamWriter写入文件的方法,涉及C#中StreamWriter类操作文件的相关技巧,需要的朋友可以参考下...2020-06-25
  • php实现文件下载实例分享

    举一个案例:复制代码 代码如下:<?phpclass Downfile { function downserver($file_name){$file_path = "./img/".$file_name;//转码,文件名转为gb2312解决中文乱码$file_name = iconv("utf-8","gb2312",$file_name...2014-06-07