1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
4 <title>无标题文档</title>
5 <script type="text/javascript">
6 function startUpload() {
7 // document.getElementById('processing').innerHTML = 'loding...';
8 return true;
9 }
10 function stopUpload(rel){
11 var rel = eval('(' + rel + ')');
12 document.getElementById('processing').innerHTML = document.getElementById('processing').innerHTML + '<img src="'+rel.thumbMap+'" alt="">';
13 }
14 </script>
15 </head>
16
17 <body style="font-size:12px;">
18 <div id="processing"></div>
19
20 <form action="upload.php" method="post" enctype="multipart/form-data" onsubmit="startUpload();" target="form-target" name="form1">
21 本地图片:<input type="file" name="file"><br>
22 远程图片:<input type="text" name="url"><br>
23 <input type="submit" name="Submit" value="提交">
24 <input name="scan" type="hidden" id="up" value="true">
25 </form>
26 <iframe style="width:0; height:0; border:0;" name="form-target"></iframe>
27 <b /><br/>
28 </body>
29 </html>
  1 <?php
2 class ieb_upload
3 {
4 public $FormName; //文件域名称
5 public $Directroy; //上传至目录
6 public $MaxSize; //最大上传大小
7 public $CanUpload = true; //是否可以上传
8 public $doUpFile = ''; //上传的文件名
9 public $sm_File = ''; //缩略图名称
10 public $Error = 0; //错误参数
11
12 // 初始化(1024*2)*1024=2097152 就是 2M
13 public function ieb_upload($FormName = '', $dirPath = '', $maxSize = 2097152) {
14 // 初始化各种参数
15 $this->FormName = $FormName;
16 $this->MaxSize = $maxSize;
17 if ($this->FormName == '') {
18 $this->CanUpload = false;
19 $this->Error = 1;
20 return;
21 }
22 $this->Directroy = $dirPath."/";
23 }
24
25 //获取远程图片
26 public function GrabImage($url) {
27 if($url=="") return false;
28
29 $ext=strrchr($url,".");
30 if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") { return false; }
31 //echo $filename=date("YmdHis").$ext;
32 $fileName = $this->Directroy.$this->newName().$ext; //主图地址
33 $this->createDir(dirname($fileName));
34
35 ob_start();
36 readfile($url);
37 $img = ob_get_contents();
38 ob_end_clean();
39 $size = strlen($img);
40 $fp2 = @fopen($fileName, "a");
41 fwrite($fp2,$img);
42 fclose($fp2);
43
44 $this->doUpFile = $fileName;
45
46 chmod($this->sm_File.$fileName, 0777);
47 return true;
48 }
49
50 // 上传文件
51 public function upload() {
52 if ($this->CanUpload) {
53 if ($_FILES[$this->FormName]['size'] == 0) {
54 $this->Error = 3;
55 $this->CanUpload = false;
56 return $this->Error;
57 }
58
59 $fileName =$this->Directroy.$this->newName().".".$this->getExt(); //主图地址
60 $this->createDir(dirname($fileName));
61 $doUpload = @copy($_FILES[$this->FormName]['tmp_name'], $fileName);
62
63 if ($doUpload) {
64 $this->doUpFile = $fileName;
65 chmod($this->sm_File.$fileName, 0777);
66 return true;
67 } else {
68 $this->Error = 4;
69 return $this->Error;
70 }
71 }
72 }
73
74 // 创建图片缩略图
75 public function thumb($dscChar = '', $width = 150, $height = 150) {
76 if ($this->CanUpload && $this->doUpFile != '') {
77
78 if ($dscChar == '')
79 $dscChar = 'sm_';
80
81 $data = getimagesize($this->doUpFile, $info);
82
83 switch ($data[2]) {
84 case 1:
85 $this->sm_File = $this->doUpFile.$dscChar.".gif";
86 $im = @imagecreatefromgif($this->doUpFile);
87 break;
88 case 2:
89 $this->sm_File = $this->doUpFile.$dscChar.".jpg";
90 $im = @imagecreatefromjpeg($this->doUpFile);
91 break;
92 case 3:
93 $this->sm_File = $this->doUpFile.$dscChar.".png";
94 $im = @imagecreatefrompng($this->doUpFile);
95 break;
96 }
97
98 $srcW = imagesx($im);
99 $srcH = imagesy($im);
100 $ni = imagecreatetruecolor($width, $height);
101 imagecopyresized($ni, $im, 0, 0, 0, 0, $width, $height, $srcW, $srcH);
102 $cr = imagejpeg($ni, $this->sm_File);
103 chmod($this->sm_File, 0777);
104
105 if ($cr) {
106 return true;
107 } else {
108 $this->Error = 5;
109 return $this->Error;
110 }
111 }
112 }
113
114 // 检查文件是否存在
115 public function scanFile() {
116 if ($this->CanUpload) {
117 $scan = is_readable($_FILES[$this->FormName]['name']);
118 if ($scan)
119 $this->Error = 2;
120 return $scan;
121 }
122 }
123
124 // 获取文件大小
125 public function getSize($format = 'B') {
126 if ($this->CanUpload) {
127 if ($_FILES[$this->FormName]['size'] == 0) {
128 $this->Error = 3;
129 $this->CanUpload = false;
130 }
131 switch ($format) {
132 case 'B':return $_FILES[$this->FormName]['size'];break;
133 case 'K':return ($_FILES[$this->FormName]['size']) / (1024);break;
134 case 'M':return ($_FILES[$this->FormName]['size']) / (1024 * 1024);break;
135 }
136 }
137 }
138
139 // 获取文件类型
140 public function getExt() {
141 if ($this->CanUpload){
142 $ext = $_FILES[$this->FormName]['name'];
143 $extStr = explode('.', $ext);
144 $count = count($extStr)-1;
145 return $extStr[$count];
146 }
147 }
148 // 获取文件名称
149 public function getName() {
150 if ($this->CanUpload)
151 return $_FILES[$this->FormName]['name'];
152 }
153
154 //创建文件夹
155 public function createDir($path){
156 if (!file_exists($path)){
157 $this->createDir(dirname($path));
158 mkdir($path, 0777);
159 }
160 }
161
162 // 新建文件名
163 public function newName() {
164 if ($this->CanUpload) {
165 //$FullName = $_FILES[$this->FormName]['name'];
166 //$extStr = explode('.', $FullName);
167 //$count = count($extStr)-1;
168 //$ext = $extStr[$count];
169 //return date('YmdHis').rand(0, 9).'.'.$ext;
170 return date('YmdHis').rand(0, 9);
171 }
172 }
173
174 // 显示错误参数
175 public function Err() {
176 return $this->Error;
177 }
178
179 // 上传后的文件名
180 public function UpFile() {
181 if ($this->doUpFile != '')
182 return $this->doUpFile;
183 else
184 $this->Error = 16;
185 }
186
187 // 上传文件的路径
188 public function filePath() {
189 if ($this->doUpFile != '')
190 return $this->sm_File.$this->doUpFile;
191 else
192 $this->Error = 26;
193 }
194
195 // 缩略图文件名称
196 public function thumbMap() {
197 if ($this->sm_File != '')
198 return $this->sm_File;
199 else
200 $this->Error = 36;
201 }
202
203 // 显示版本信息
204 public function ieb_version() {
205 return 'IEB_UPLOAD CLASS Ver 1.1';
206 }
207 }
208
209 if (isset($_REQUEST['scan']))
210 {
211 // 声明一个上传类
212 $upfos = new ieb_upload('file', 'D:/wamp64/www');
213 //远程图片
214 if($_REQUEST['url']){
215 $upfos->GrabImage($_REQUEST['url']);
216 }else{
217 $upfos->upload();//上传主图
218 }
219 $upfos->thumb(); //生成缩略图
220 //返回数组
221
222 $re['name'] = $upfos->getName();//文件名
223 $re['ext'] = $upfos->getExt(); //扩展名
224 $re['size'] = $upfos->getSize(); //扩展名
225
226 $re['newName'] = $upfos->UpFile(); //新文件名
227 $re['filePath'] = $upfos->filePath(); //文件路径
228 $re['thumbMap'] = dirname($upfos->thumbMap()); //文件路径
229 }
230 ?>
231 <script type="text/javascript">
232 var re = '<?php echo json_encode($re); ?>';
233 window.top.window.stopUpload(re);
234 </script>

php 上传图片,无刷新上传,支持多图上传,远程图片上传的更多相关文章

  1. Webuploader 简单图片上传 支持多图上传

    简介: 通过webuploader 实现简单的图片上传功能,支持多张图上传 官方文档传送门:http://fex.baidu.com/webuploader/getting-started.html# ...

  2. 纯js异步无刷新请求(只支持IE)

    纯js异步无刷新请求 下载地址:http://pan.baidu.com/s/1slakL1F 所以因为非IE浏览器都禁止跨域请求,所以以只支持IE. <HTML> <!-- 乱码( ...

  3. 纯js异步无刷新请求(只支持IE)【原】

    纯js异步无刷新请求 下载地址:http://pan.baidu.com/s/1slakL1F 所以因为非IE浏览器都禁止跨域请求,所以以只支持IE. <HTML> <!-- 乱码( ...

  4. 发现一个微博图床API和图片上传代码

    网上寻到一款微博图床的插件,然后顺藤摸瓜找到了原作者的API代码. API文件: <?php /** * 上传图片到微博图床 * @author Youngxj & mengkun &a ...

  5. 妈蛋:kinMaxShow轮播图异常,WebUploader图片上传坑爹,图片被压缩了

    今天晚上在改造轮播图. 原来的代码是这样的: <div> <img src="${static}/image/index/banner/`.jpg" /> ...

  6. ThinkPHP3.2.2 无刷新上传插件uploadify 使用

    一. 在控制器中写一个方法,用于上传 public function upload(){ if (!empty($_FILES)) { //图片上传设置 $config = array( 'maxSi ...

  7. asp.net core 通过ajax上传图片及wangEditor图片上传

    asp.net core 通过ajax上传图片 .net core前端代码,因为是通过ajax调用,首先要保证ajax能调用后台代码,具体参见上一篇.net core 使用ajax调用后台代码. 前端 ...

  8. JavaScript实现本地图片上传前进行裁剪预览

    本项目支持IE8+,测试环境IE8,IE9,IE10,IE11,Chrome,FireFox测试通过 另:本项目并不支持Vue,React等,也不建议,引入JQuery和Vue.React本身提倡的开 ...

  9. 关于editor网页编辑器ueditor.config.js 配置图片上传

    最近公司项目在做一个门户网站,其中新闻和简介等部分使用到了ueditor编辑器,但是上级明确指示需要图片上传这个功能,这时却发现图片上传功能不能正常使用,上传时一直报错,网上收了好几个处理办法,都说的 ...

随机推荐

  1. 感知机vs支持向量机

    感知机原理:二维空间中找到一条直线可以把所有二元类别分离开,三维或多维空间中,找到一个分离超平面把所有二元类别分离开.而可把所有二元类别分离开的超平面不止一个,哪个是最好的呢?损失函数:所有误分类的点 ...

  2. 【应用服务 App Service】App Service使用Git部署时,遇见500错误

    问题描述 Azure App Service在部署的时候支持多种方式,如Zip,VS 2019, VS Code,或者是Git部署,当使用Git部署遇见500错误时,可以通过其他的部署方式来验证是否也 ...

  3. AWK实现把一个文件根据内容进行分组输出多个文件

    AWK实现把一个文件根据内容进行分组输出多个文件 1.首先准备文件data.txt(分隔符为tab) 第一列省编码,第二列省名称...... 2.将该大文件根据第一列的省编码进行分组并输出到各个省编码 ...

  4. 重要,知识点:InnoDB的插入缓冲

    世界上最快的捷径,就是脚踏实地,本文已收录[架构技术专栏]关注这个喜欢分享的地方. InnoDB引擎有几个重点特性,为其带来了更好的性能和可靠性: 插入缓冲(Insert Buffer) 两次写(Do ...

  5. 【算法】二叉树、N叉树先序、中序、后序、BFS、DFS遍历的递归和迭代实现记录(Java版)

    本文总结了刷LeetCode过程中,有关树的遍历的相关代码实现,包括了二叉树.N叉树先序.中序.后序.BFS.DFS遍历的递归和迭代实现.这也是解决树的遍历问题的固定套路. 一.二叉树的先序.中序.后 ...

  6. JMeter实战(三) 界面使用

    JMeter 有 2 种运行方式,一种是 CLI,一种是 GUI,本篇文章就来介绍一下后者,图形用户界面,因为后续文章大部分都是基于 GUI 的. 本文演示的是英文版,想用中文的同学可以在菜单栏点击 ...

  7. Django+Celery+xadmin实现异步任务和定时任务

    Django+Celery+xadmin实现异步任务和定时任务 关注公众号"轻松学编程"了解更多. 一.celery介绍 1.简介 [官网]http://www.celerypro ...

  8. JAVA学习线路:day14-网络编程

    心得: 我是一名正在自学的java的即将毕业的大学生 总结笔记是为了让自己更好的理解和加深印象.可能不是那么美观,自己可以看懂就好 所有的文档和源代码都开源在GitHub: https://githu ...

  9. mns: Money Never Sleeps! 自己开发的一款 IDEA 插件介绍.

    一边敲代码, 一边关注股票/基金行情, 还不怕同事盯到自己的屏幕! 对于一个关注股市跟基金的研发人员来说, 莫过于一天到晚写代码, 而不能及时的查看股市行情跟基金走势了吧. 写代码的时候比较容易忘记看 ...

  10. Linux 下 GCC 的使用

    0 运行环境 本机系统:Windows 7 虚拟机软件:Oracle VM VirtualBox 6 虚拟机系统:CentOS 7 1 GCC 简介 GCC 是 GUN Compiler Collec ...