您的位置:首页 > 移动开发 > 微信开发

php单张图片上传插件免刷新,兼容手机,可实现类似微信图片上传的体验

2017-10-02 15:14 761 查看
本上传功能.是用于整合到一个php,ajax聊天室上的,具体可下载php聊天室http://download.csdn.net/download/viqecel/9832763

因为是聊天室,需要实现类似微信的图片上传效果,就是点击上传按钮后,自动直接弹出相册,选择好一张图片后,自动上传到聊天室并入库.所以.通过upload()函数,可以不用显示上传框了,直接激活子页面中的上传动作.另外,onchange事件则可以自动提交上传,不必用户点击上传按钮了.三步并做一步

<!-- 父页面中的上传按钮 -->
<img src="ui/images/myimg.png" class="link" onclick="upload()" />

<iframe src="upload.php"id="box_paint_container" ></iframe>
upload.php 文件中有一个表单,注意,通过下面的 window.parent,可以把值传回父页面中的一个表单上,从而可以实时浏览上传的图片,比如类这样
js代码: $('#showImg').attr('src','图片新地址');
<img src="" alt="" id="showImg"width="100%"onclick="return close_l();" style="">
<form enctype="multipart/form-data" action="upload.php"id="forms" method="post" name="upform" style="position: relative; ">
<input name="upfile" type="file"id="file" onchange="document.getElementById('forms').submit();
window.parent.document.getElementById('box_paint_container').style.display='none';;">
</form>

下面是父页面中的一个函数
<script type="text/javascript">

function upload(){
var a = document.getElementById('box_paint_container').contentWindow.document.getElementById("file");
a.click();

}
</script>


upload.php中还有一个php上传功能,兼容手机版,可上传单张图片. 一并分享下

//上传文件类型列表
require_once 'config.php';
require_once '../system/config/database.inc.php';
//require_once '../system/funcs/global.fun.php';
require_once 'incl/main.inc';

dbconnect(); $settings=get_settings(0); $options=get_options(); $lang=get_language();

if(isset($_COOKIE['uid'])){
$uid=intval(_encrypt(_getcookie("uid"),'DECODE'));
$shopid=$_COOKIE['shopid'];
$query='SELECT * FROM '.$dbss['prfx']."_member WHERE uid=$uid";

$result=neutral_query($query);
if(neutral_num_rows($result)>0){
$ext_user=neutral_fetch_array($result);

$user=array();
$uname=$ext_user['username'];
$heading=$ext_user['headimg']==''?'/statics/uploads/'.$ext_user['img']:$ext_user['headimg'];
if($uname==''){
$uname=substr_replace($ext_user['mobile'],"****",5,2);

}
}
}

if(!isset($uid)){
//权限
redirect('/index.php/mobile/user/login.php');die();

}

$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/x-png'
);

$max_file_size=2000000;     //上传文件大小限制, 单位BYTE
$destination_folder="uploadimg/"; //上传文件路径
$watermark=0;      //是否附加水印(1为加水印,其他为不加水印);
$watertype=1;      //水印类型(1为文字,2为图片)
$waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring="http://www.xplore.cn/";  //水印字符串
$waterimg="xplore.gif";    //水印图片
$imgpreview=0;      //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2;    //缩略图比例

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

if (!is_uploaded_file($_FILES["upfile"]['tmp_name']))
//是否存在文件
{
echo "图片不存在!";
exit;
}

$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "文件太大!";
exit;
}

if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "文件类型不符!".$file["type"];
exit;
}

if(!file_exists($destination_folder))
{
mkdir($destination_folder);  //上传到的位置
}

$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo['extension'];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) && $overwrite != true)
{
echo "同名文件已经存在了";
exit;
}

if(!move_uploaded_file ($filename, $destination))
{
echo "移动文件出错";
exit;
}

$pinfo=pathinfo($destination);  //文件名
$fname=$pinfo['basename'];
$line='<img onclick="imgbig(this)"src="'.$destination_folder.$fname.'"  style="width:100px;" />';
$query='INSERT INTO '.$dbss['prfx']."_lines VALUES(NULL,$uid,'$uname',$timestamp,'$line','$shopid','$heading','',0)";
neutral_query($query);
//print_r($line);exit;
//echo " <font color=red>上传中....</font><br>";
// echo " 宽度:".$image_size[0];
// echo " 长度:".$image_size[1];
// echo "<br> 大小:".$file["size"]." bytes";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息