PHP AJAXFORM提交图片上传并显示图片源代码
PHP dofile.php 文件上传源代码
<?
php
$file_upload = "upload/";
$file_allow_ext='gif|jpg|jpeg|png|gif|zip|rar|ppt|xls|pdf|pptx|xlsx|docx';
$file_allow_size = 5*1024*1024;
if($_POST['submit']=="上传"){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$file_name = $_FILES['file']['name'];
$file_error = $_FILES['file']['error'];
$file_type = $_FILES['file']['type'];
$file_tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_ext = substr($file_name, strrpos($file_name, '.')+1);
switch($file_error){
case 0:
$data['status'] = 0;
$data['msg'] = "文件上传成功!";
break;
case 1:
$data['status'] = 1;
$data['msg'] = "文件上传失败,文件大小".$file_size."超过限制,同意上传大小".sizeFormat($file_allow_size)."!";
break;
case 3:
$data['status'] = 1;
$data['msg'] = "上传失败,文件仅仅有部份上传!";
break;
case 4:
$data['status'] = 1;
$data['msg'] = "上传失败,文件没有被上传!";
break;
case 5:
$data['status'] = 1;
$data['msg'] = "文件上传失败,文件大小为0!";
break;
}
if(stripos($file_allow_ext,$file_ext)===false){
$data['status'] = 1;
$data['msg'] = "该文件扩展名不同意上传";
}
if($file_size>$file_allow_size){
$data['status'] = 1;
$data['msg'] = "文件大小超过限制,仅仅能上传".sizeFormat($file_allow_size)."的文件!";
}
if($data['status']==1){
$data['status'] = 1;
$data['msg'] = $data['msg'];
exit(json_encode($data));
}
if($data['status']==0){
if(file_exists($file_upload)){
$file_new_name = date("YmdHis").'_'.rand(10000,99999).'.'.$file_ext;
$file_save_path = $file_upload.$file_new_name;
$data['status'] = 0;
$data['url'] = $file_save_path;
move_uploaded_file($file_tmp_name,$file_save_path);
exit(json_encode($data));
}else{
exit(json_encode($data));
}
}
}
}
function sizeFormat($size)
{
$sizeStr='';
if($size<1024)
{
return $size."bytes";
}
else if($size<(1024*1024))
{
$size=round($size/1024,1);
return $size."KB";
}
else if($size<(1024*1024*1024))
{
$size=round($size/(1024*1024),1);
return $size."MB";
}
else
{
$size=round($size/(1024*1024*1024),1);
return $size."GB";
}
}
?>
HTML例如以下
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://files.cnblogs.com/files/china-li/jquery.form.js"></script>
<form action="dofile.php" method="post" enctype="multipart/form-data" id="upfileimage">
<input type="hidden" name="image[]" />
<label for="file">文件:</label><input type="file" name="file" id="file" />
<input type="submit" name="submit" value="上传" />
</form>
<script type="text/javascript">
$("#upfileimage").submit(function(){
if($("input[type=file]").val()==""){
alert("请选择要上传的文件");
return false;
}
})
$(function(){
var options = {
type:"POST",
dataType:"json",
resetForm:true,
success:function(o){
if(o.status==1){
alert(o.msg);
}else{
$("body").append(" <img src='"+o.url+"' alt='' width='100' /><input type='hidden' name='image[]' value='"+o.url+"' />");
}
},
error:function(o){
alert(o.message);
}
}
$("#upfileimage").ajaxForm(options).submit(function(){return false;});
})
</script>
PHP AJAXFORM提交图片上传并显示图片源代码的更多相关文章
- django上传并显示图片
环境 python 3.5 django 1.10.6 步骤 创建名为 testupload的项目 django-admin startproject testupload 在项目testupload ...
- input上传文件显示图片缩略图
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- django后台处理前端上传和显示图片
1:项目根目录存放图片的目录 2:settings.py 添加 MEDIA_ROOT = os.path.join(BASE_DIR, "media") 3:url.py 添 ...
- html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器
以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上 ...
- ruby on rails爬坑(三):图片上传及显示
一,问题及思路 最近在用rails + react + mysql基本框架写一个cms + client的项目,里面涉及到了图片的上传及显示,下面简单说说思路,至于这个项目的配置部署,应该会在寒假结束 ...
- 图片上传即时显示javascript代码
这是基于javascript的一种图片上传即时显示方法,测试结果IE6和火狐浏览器可以正常使用.google浏览器不兼容. 这种方法兼容性比较差,仅供参考,建议使用ajax方法来即时显示图片. 1.首 ...
- 图片上传并显示(兼容ie),图片大小判断
图片上传并显示(兼容ie),图片大小判断 HTML <div id="swf" style="margin: 0 auto;text-align: center;& ...
- struts中用kindeditor实现的图片上传并且显示在页面上
做公司网站的时候由于需要在内容属性中加入图片,所以就有了这个问题,本来一开始找几篇文章看都是讲修改kindeditor/jsp/file_manager_json.jsp和upload_json.js ...
- Slog71_选取、上传和显示本地图片GET !(微信小程序之云开发-全栈时代3)
ArthurSlog SLog-71 Year·1 Guangzhou·China Sep 12th 2018 ArthurSlog Page GitHub NPM Package Page 掘金主页 ...
随机推荐
- ffmepg 指定RTSP网络连接模式UDP还是TCP
AVFormatContext *formatCtx = NULL; formatCtx = avformat_alloc_context(); AVDictionary* options = NUL ...
- ffmpeg windows 雪花声解决方法
替换所有文件里的<math.h>为<mathimf.h>即可. 我用ffmpeg-0.6.3版测试时,好像mathimf.h文件和其他文件有冲突,需要修改源码. 和qdm2.c ...
- 反射小应用之DataTable和List<T>互操作
在程序中,往往会遇到一些小情况,就是数据库取出来的时候为了方便直接将数据通过存储在DataSet或DataTable中,这样做的一个后果是在日后的的对数据进行”细“操作时,就发现它可能没有List&l ...
- Ubuntu消息菜单(MessagingMenu)API
应用程序可以注册在消息菜单里显示消息,它也可以使用全局聊天状态项目. 注册 应用程序要在消息菜单里显示消息,必须满足以下条件: $HOME/.config/indicators/messages/ap ...
- Oracle EM 不能访问
zwt2001267 原文 Oracle EM 不能访问 1. cmd控制启动EM: C:\Users\Administrator>emctl start dbconsoleEnvironmen ...
- 白盒测试之gmock入门篇
一.gmock是什么 gmock是google公司推出的一款开源的白盒测试工具.gmock是个很强大的东西,测试一个模块的时候,可能涉及到和其他模块交互,可以将模块之间的接口mock起来,模拟交互过程 ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_label(self, locator)
def get_selected_list_label(self, locator): """Returns the visible label of the selec ...
- LeetCode题解——3Sum
题目: 给定一个数组,找出其中和为0的所有3个数的组合.每个组合的3个数都是非递降的. 解法: 先排序再遍历,设置3个指针,第一个依次遍历,第二三个在第一个指针后面的部分里,左右夹逼查找和为第一个数的 ...
- kobo boot scripts
#!/bin/sh pkill nickel eink_enable_autoupdate rm -rf /debian/tmp/* /debian/tmp/.* 2>/dev/null mou ...
- 利用redis分布式锁的功能来实现定时器的分布式
文章来源于我的 iteye blog http://ak478288.iteye.com/blog/1898190 以前为部门内部开发过一个定时器程序,这个定时器很简单,就是配置quartz,来实现定 ...