php 如何实现无刷新删除图片? 这张图是修改产品内容,当我们把标签文本编辑成df23234343434的时候,再删除下面的图片,如何实现标签文本的值没有变呢?急急急,请各位大虾们多多指教!!!!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 恩,我知道是Ajax,或者用iframe,但是不会做啊。 为图片设置一个标签,取个id用JS.document.getElementById(id).parentNode.removeChild(document.getElementById(id)) 怎么没人给支招啊,如果说嫌我给的分少,可以去八戒网去投标,我已经悬赏100RMB了。真的很急啊,什么smarty之类的,我都不知道,我是半路出家,没人讲解,自己摸索的。谢谢大家啊 没用smarty的话就这个吧,我自己写的,你改改吧,单文件上传的,要是smarty的我也有(包括多文件的)//html中<form action="" method="post" enctype="multipart/form-data" target="frame"> <input type="file" name="file" class="input"> <input type="submit" name="submit_upload" value="上 传"> <input type="submit" name="submit_delete" value="删 除"> <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe></form><img id="picview" height="100" style="display:none;"><script>if("<?php echo $imgpath; ?>"){ parent.document.getElementById("picview").src = "<?php echo $imgpath; ?>"; parent.document.getElementById("picview").style.display = "block";}</script>//php中<?php/*头两天做了个本地图片预览功能,当时是纯js做的,也就是上传之前预览图片,可是谁想到几天后浏览器更新,IE直接就不能用了,后来思考了下,认为还是将图片上传到服务器端之后再进行预览比较安全,要不然哪天浏览器再来下可就完蛋了,不过后来看了下jquery插件做法也是如此的,呵呵,绝没有抄袭,给不会的朋友提供下思路*/if(!isset($_SESSION))session_start();/**2010-6-22 * $data 数组类型 包含以下变量 ------------------------------- * $sub_type submit类型(upload:上传按钮 delete:删除按钮),默认upload * $file 通过表单获取的$_FILES['filename']数组 * $img_tag_id 预览图片的<img>的ID值 * $img_dir 上传图片的目录 * $max_file_size 单位M(兆),默认:1M * $type_array 允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif) **/function upload($data){ if(!$data['file']){ echo '<script>alert("file不能为空!");</script>';exit; } if(!$data['img_tag_id']){ echo '<script>alert("预览图片标签ID不能为空!");</script>';exit; } if(!$data['img_dir']){ echo '<script>alert("图片上传目录不能为空!");</script>';exit; } if(!isset($data['max_file_size'])){ $data['max_file_size'] = 1024 * 1024; }else{ $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024; } if(!isset($data['type_array'])){ $data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif'); } if(!isset($data['sub_type'])){ $data['sub_type'] = 'upload'; } $imgpath = ''; if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){ if(isset($_SESSION['name']) && $_SESSION['name']){ if(is_file($_SESSION['imgpath'])){ $b = unlink($_SESSION['imgpath']); } unset($_SESSION['name'], $_SESSION['imgpath']); if(!isset($_SESSION['name'])){ echo '<script>alert("删除成功!");</script>'; echo '<script>parent.document.getElementById("'.$data['img_tag_id'].'").style.display = "none";</script>'; }else{ echo '<script>alert("删除失败!");</script>'; } }else{ echo '<script>alert("没有稿件!");</script>'; }exit; } if(isset($_SESSION['imgpath']) && $_SESSION['imgpath']){ echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit; } if(!in_array($data['file']['type'], $data['type_array'])){ echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit; } if($data['file']['size'] > $data['max_file_size']){ echo '<script>alert("您上传的稿件过大,请选择2M以下的图片上传!");</script>';exit; } if(!is_dir($data['img_dir'])){ @mkdir($data['img_dir'], 0777, true); } $imgpath = $data['img_dir'].'/'.date('His', time()).rand(100, 999).$data['file']['name']; $isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath); if(!$isupload){ echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit; }else{ echo '<script>alert("稿件上传成功!");</script>'; } $_SESSION['name'] = $data['file']['name']; $_SESSION['imgpath'] = $imgpath; return $imgpath;}/*test_start*/$sub_type = '';if(isset($_POST['submit_upload']))$sub_type = 'upload';if(isset($_POST['submit_delete']))$sub_type = 'delete';if($sub_type){ $data = array( 'sub_type' => $sub_type, 'file' => $_FILES['file'], 'img_tag_id' => 'picview', 'img_dir' => 'upload_img', ); $imgpath = upload($data);}else{ $imgpath = isset($_SESSION['imgpath'])? $_SESSION['imgpath']: '';}?> 上面那贴不要看了,下面这个是html和php同页面的情况下的(注意改提交路径)<?phpif(!isset($_SESSION))session_start();/**2010-6-22 * $data 数组类型 包含以下变量 ------------------------------- * $sub_type submit类型(upload:上传按钮 delete:删除按钮),默认upload * $file 通过表单获取的$_FILES['filename']数组 * $img_tag_id 预览图片的<img>的ID值 * $img_dir 上传图片的目录 * $max_file_size 单位M(兆),默认:1M * $type_array 允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif) **/function upload($data){ if(!$data['file']){ echo '<script>alert("file不能为空!");</script>';exit; } if(!$data['img_tag_id']){ echo '<script>alert("预览图片标签ID不能为空!");</script>';exit; } if(!$data['img_dir']){ echo '<script>alert("图片上传目录不能为空!");</script>';exit; } if(!isset($data['max_file_size'])){ $data['max_file_size'] = 1024 * 1024; }else{ $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024; } if(!isset($data['type_array'])){ $data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif'); } if(!isset($data['sub_type'])){ $data['sub_type'] = 'upload'; } $imgpath = ''; if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){ if(isset($_SESSION['name']) && $_SESSION['name']){ if(is_file($_SESSION['imgpath'])){ $b = unlink($_SESSION['imgpath']); } unset($_SESSION['name'], $_SESSION['imgpath']); if(!isset($_SESSION['name'])){ echo '<script>alert("删除成功!");</script>'; echo '<script>parent.document.getElementById("'.$data['img_tag_id'].'").style.display = "none";</script>'; }else{ echo '<script>alert("删除失败!");</script>'; } }else{ echo '<script>alert("没有稿件!");</script>'; }exit; } if(isset($_SESSION['imgpath']) && $_SESSION['imgpath']){ echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit; } if(!in_array($data['file']['type'], $data['type_array'])){ echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit; } if($data['file']['size'] > $data['max_file_size']){ echo '<script>alert("您上传的稿件过大,请选择2M以下的图片上传!");</script>';exit; } if(!is_dir($data['img_dir'])){ @mkdir($data['img_dir'], 0777, true); } $imgpath = $data['img_dir'].'/'.date('His', time()).rand(100, 999).$data['file']['name']; $isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath); if(!$isupload){ echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit; }else{ echo '<script>alert("稿件上传成功!");</script>'; } $_SESSION['name'] = $data['file']['name']; $_SESSION['imgpath'] = $imgpath; return $imgpath;}/*test_start*/$sub_type = '';if(isset($_POST['submit_upload']))$sub_type = 'upload';if(isset($_POST['submit_delete']))$sub_type = 'delete';if($sub_type){//echo '<script>alert("'.$sub_type.'");</script>';exit; $data = array( 'sub_type' => $sub_type, 'file' => $_FILES['file'], 'img_tag_id' => 'picview', 'img_dir' => 'upload_img', ); $imgpath = upload($data);}else{ $imgpath = isset($_SESSION['imgpath'])? $_SESSION['imgpath']: '';}?><form action="a.php" method="post" enctype="multipart/form-data" target="frame"> <input type="file" name="file" class="input"> <input type="submit" name="submit_upload" value="上 传"> <input type="submit" name="submit_delete" value="删 除"> <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe></form><img id="picview" height="100" style="display:none;"><script>if("<?php echo $imgpath; ?>"){ parent.document.getElementById("picview").src = "<?php echo $imgpath; ?>"; parent.document.getElementById("picview").style.display = "block";}</script> 在来帖smarty版的,共后来的参考//目录结构如下(根目录中):目录cache(缓存目录-本程序中没用到)目录config(配置文件目录-本程序中没用到)目录compile(编译文件目录)目录smarty(smarty模板主文件目录)目录template(模板目录)下面是代码//template目录中的b.html文件中的内容<form action="b.php?act=a" method="post" enctype="multipart/form-data" target="frame" id="form"> <input type="file" name="file" class="input"> <input type="submit" name="submit_upload" value="上 传"> <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe></form><div id="div"><% section name=loop loop=$return_data %><div id="div<% $return_data[loop].num %>"><img src="<% $return_data[loop].imgpath %>" height="100"><a id="a<% $return_data[loop].num %>" onclick="del(<% $return_data[loop].num %>)" style="cursor:pointer;">删除</a></div><% /section %></div><input type="hidden" id="name"><input type="hidden" id="imgpath"><input type="hidden" id="num"><script>var request;function createxmlHttpRequest(){ if(!request){ if(window.XMLHttpRequest){ request = new XMLHttpRequest(); }else{ request = new ActiveXObject("Microsoft.XMLHTTP"); } }}function callback(num){ if (request.readyState == 4){ if (request.status == 200){// alert(request.responseText); parent.document.getElementById("div" + num).innerHTML = ""; parent.document.getElementById("div" + num).style.display = "none"; }else if(request.status == 404){ alert("该路径未找到"); }else if(request.status == 403){ alert("禁止访问"); }else{ alert("status is " + request.status); } }}function del(num){ createxmlHttpRequest(); var url = "b.php"; var send = "act=a&submit_delete=1&num=" + num; request.open("post", url, true); request.onreadystatechange = function(){ callback(num); } request.setRequestHeader("content-type", "application/x-www-form-urlencoded"); request.send(send);}</script>//根目录下的b.php文件中的内容<?php//2010-7-20 17:57//=============================================================================================================================function p($var){echo '<pre>';if((is_array($var) || is_object($var)) && count($var)){print_r($var);}else{var_dump($var);}exit;}//=============================================================================================================================$script = substr(__FILE__, strrpos(__FILE__, '\\')+ 1);define('ROOT_PATH', str_replace('\\', '/', str_replace($script, '' ,__FILE__)));include(ROOT_PATH.'smarty/libs/Smarty.class.php');$s = new Smarty();$s->template_dir = ROOT_PATH.'template';$s->compile_dir = ROOT_PATH.'compile';$s->cache_dir = ROOT_PATH.'cache';$s->config_dir = ROOT_PATH.'config';$s->debugging = false;$s->caching = false;$s->left_delimiter = '<%';$s->right_delimiter = '%>';class Fload{ /*--------------------------------------------------------------- * data 数组,包含元素如下: * ------------------------------------------------------------- * sub_type 提交类型:upload-添加上传图片 delete-删除上传图片 * ------------------------------------------------------------- * file 上传图片的$_FILES['filename'] * ------------------------------------------------------------- * img_dir 图片上传的路径(相对路径) * ------------------------------------------------------------- * num 图片上传/删除的标识 * ------------------------------------------------------------- */ function upload($data){ $num = $data['num']; if(!$data['file'] && $data['sub_type'] != 'delete'){ echo 'file_not_isnull';exit; } if(!$data['img_dir']){ echo 'pic_savepath_isnull';exit; } if(!isset($data['max_file_size'])){ $data['max_file_size'] = 1024 * 1024; }else{ $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024; } if(!isset($data['type_array'])){ $data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif'); } if(!isset($data['sub_type'])){ $data['sub_type'] = 'upload'; } $imgpath = ''; if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){ if(isset($_SESSION['img'][$data['num']]['name']) && $_SESSION['img'][$data['num']]['name']){ if(is_file($_SESSION['img'][$data['num']]['imgpath'])){ $b = unlink($_SESSION['img'][$data['num']]['imgpath']); } unset($_SESSION['img'][$data['num']]); if(!isset($_SESSION['img'][$data['num']]['name'])){ echo 'del_ok'; }else{ echo 'del_no'; } }else{ echo 'no_pic'; }exit; } if(!in_array($data['file']['type'], $data['type_array'])){ echo 'pic_type_no';exit; } if($data['file']['size'] > $data['max_file_size']){ echo 'pic_size_toobig';exit; } if(!is_dir($data['img_dir'])){ mkdir($data['img_dir'], 0777, true); } $imgpath = $data['img_dir'].'/'.date('His', time()).rand(100, 999).$data['file']['name']; $isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath); if(!$isupload){ echo 'pic_upload_no';exit; } $_SESSION['img'][$num]['name'] = $data['file']['name']; $_SESSION['img'][$num]['imgpath'] = $imgpath; $_SESSION['img'][$num]['num'] = $num; $_SESSION['num'] = $num + 1; $script = '<script> var div = parent.document.getElementById("div"); if(div == null){ div = parent.document.createElement("div"); div.id = "div"; parent.document.getElementById("form").appendChild(div); } var child_div = parent.document.getElementById("div" + '.$num.'); if(child_div == null){ child_div = parent.document.createElement("div"); child_div.id = "div" + '.$num.'; } var img = parent.document.createElement("img"); img.src = "'.$imgpath.'"; img.height = 100; var a = parent.document.getElementById("a" + '.$num.'); if(a == null){ a = parent.document.createElement("a"); a.style.cursor = "pointer"; a.id = "a" + '.$num.'; a.innerHTML = "删除"; } child_div.appendChild(img); child_div.appendChild(a); div.appendChild(child_div); </script>'; echo $script; foreach($_SESSION['img'] as $k => $v){ $event = '<script> var a = parent.document.getElementById("a" + '.$v['num'].'); if(parent.document.addEventListener){ a.addEventListener("click", function(){del('.$v['num'].')}, false); }else if(attachEvent){ a.attachEvent("onclick", new Function("del("+'.$v['num'].'+")")); } </script>'; echo $event; $j++; } } //如果浏览器是FF(3.0),请用此函数将返回的数据重新排序 function k_sort($arr){ if(is_array($arr))sort($arr); return $arr; }}//================================================================test=================================================================$fload = new Fload();//$tpl = new Tpl();if(!isset($_SESSION))session_start();$num = isset($_SESSION['num'])? $_SESSION['num']: 0;$sub_type = '';if(isset($_POST['submit_upload']))$sub_type = 'upload';if(isset($_POST['submit_delete']))$sub_type = 'delete';if($sub_type){ $num = isset($_POST['num'])? $_POST['num']: $num;//$_POST['num']为删除的图片数组num,后者的num为上传的图片数组num $data = array('sub_type' => $sub_type, 'file' => $_FILES['file'], 'img_dir' => 'upload_img', 'num' => $num); $return_data = $fload->upload($data);}else{ $return_data = isset($_SESSION['img'])? $_SESSION['img']: '';}$return_data = $fload->k_sort($return_data);//支持FF$s->assign('return_data', $return_data);$s->display('a.html');?> 求一个php的网上实时竞价/拍卖系统? 求教下一个字符串或数组取值的函数 如何找到一个php项目练手 以为部分CSDN的牛人有多强,原来只是建立在自己无知的基础上 php配置问题 求救~~~,php菜鸟问问题了! 如可以把问题解决者,可以得到本人赠送的实体礼物 折线图。 mssql_result取数据库varchar的字段时为何有字数限制? PHP支持的FTP函数ftp_connect()吗? $_SERVER['SCRIPT_NAME'], $_SERVER['REQUEST_URI']问题 类封装验证图如何输出+JS
document.getElementById(id).parentNode.removeChild(document.getElementById(id))
<input type="file" name="file" class="input">
<input type="submit" name="submit_upload" value="上 传">
<input type="submit" name="submit_delete" value="删 除">
<iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
</form><img id="picview" height="100" style="display:none;"><script>
if("<?php echo $imgpath; ?>"){
parent.document.getElementById("picview").src = "<?php echo $imgpath; ?>";
parent.document.getElementById("picview").style.display = "block";
}
</script>
//php中
<?php
/*
头两天做了个本地图片预览功能,当时是纯js做的,也就是上传之前预览图片,可是谁想到几天后浏览器更新,IE直接就不能用了,后来思考了下,认为还是将图片上传到服务器端之后再进行预览比较安全,要不然哪天浏览器再来下可就完蛋了,不过后来看了下jquery插件做法也是如此的,呵呵,绝没有抄袭,给不会的朋友提供下思路
*/
if(!isset($_SESSION))session_start();/**2010-6-22
* $data 数组类型 包含以下变量
-------------------------------
* $sub_type submit类型(upload:上传按钮 delete:删除按钮),默认upload
* $file 通过表单获取的$_FILES['filename']数组
* $img_tag_id 预览图片的<img>的ID值
* $img_dir 上传图片的目录
* $max_file_size 单位M(兆),默认:1M
* $type_array 允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif)
**/
function upload($data){
if(!$data['file']){
echo '<script>alert("file不能为空!");</script>';exit;
}
if(!$data['img_tag_id']){
echo '<script>alert("预览图片标签ID不能为空!");</script>';exit;
}
if(!$data['img_dir']){
echo '<script>alert("图片上传目录不能为空!");</script>';exit;
}
if(!isset($data['max_file_size'])){
$data['max_file_size'] = 1024 * 1024;
}else{
$data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
}
if(!isset($data['type_array'])){
$data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif');
}
if(!isset($data['sub_type'])){
$data['sub_type'] = 'upload';
} $imgpath = '';
if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){
if(isset($_SESSION['name']) && $_SESSION['name']){
if(is_file($_SESSION['imgpath'])){
$b = unlink($_SESSION['imgpath']);
}
unset($_SESSION['name'], $_SESSION['imgpath']);
if(!isset($_SESSION['name'])){
echo '<script>alert("删除成功!");</script>';
echo '<script>parent.document.getElementById("'.$data['img_tag_id'].'").style.display = "none";</script>';
}else{
echo '<script>alert("删除失败!");</script>';
}
}else{
echo '<script>alert("没有稿件!");</script>';
}exit;
} if(isset($_SESSION['imgpath']) && $_SESSION['imgpath']){
echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit;
}
if(!in_array($data['file']['type'], $data['type_array'])){
echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit;
}
if($data['file']['size'] > $data['max_file_size']){
echo '<script>alert("您上传的稿件过大,请选择2M以下的图片上传!");</script>';exit;
} if(!is_dir($data['img_dir'])){
@mkdir($data['img_dir'], 0777, true);
}
$imgpath = $data['img_dir'].'/'.date('His', time()).rand(100, 999).$data['file']['name'];
$isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath);
if(!$isupload){
echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit;
}else{
echo '<script>alert("稿件上传成功!");</script>';
}
$_SESSION['name'] = $data['file']['name'];
$_SESSION['imgpath'] = $imgpath; return $imgpath;
}/*test_start*/
$sub_type = '';
if(isset($_POST['submit_upload']))$sub_type = 'upload';
if(isset($_POST['submit_delete']))$sub_type = 'delete';
if($sub_type){
$data = array( 'sub_type' => $sub_type,
'file' => $_FILES['file'],
'img_tag_id' => 'picview',
'img_dir' => 'upload_img',
);
$imgpath = upload($data);
}else{
$imgpath = isset($_SESSION['imgpath'])? $_SESSION['imgpath']: '';
}
?>
<?phpif(!isset($_SESSION))session_start();/**2010-6-22
* $data 数组类型 包含以下变量
-------------------------------
* $sub_type submit类型(upload:上传按钮 delete:删除按钮),默认upload
* $file 通过表单获取的$_FILES['filename']数组
* $img_tag_id 预览图片的<img>的ID值
* $img_dir 上传图片的目录
* $max_file_size 单位M(兆),默认:1M
* $type_array 允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif)
**/
function upload($data){
if(!$data['file']){
echo '<script>alert("file不能为空!");</script>';exit;
}
if(!$data['img_tag_id']){
echo '<script>alert("预览图片标签ID不能为空!");</script>';exit;
}
if(!$data['img_dir']){
echo '<script>alert("图片上传目录不能为空!");</script>';exit;
}
if(!isset($data['max_file_size'])){
$data['max_file_size'] = 1024 * 1024;
}else{
$data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
}
if(!isset($data['type_array'])){
$data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif');
}
if(!isset($data['sub_type'])){
$data['sub_type'] = 'upload';
} $imgpath = '';
if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){
if(isset($_SESSION['name']) && $_SESSION['name']){
if(is_file($_SESSION['imgpath'])){
$b = unlink($_SESSION['imgpath']);
}
unset($_SESSION['name'], $_SESSION['imgpath']);
if(!isset($_SESSION['name'])){
echo '<script>alert("删除成功!");</script>';
echo '<script>parent.document.getElementById("'.$data['img_tag_id'].'").style.display = "none";</script>';
}else{
echo '<script>alert("删除失败!");</script>';
}
}else{
echo '<script>alert("没有稿件!");</script>';
}exit;
} if(isset($_SESSION['imgpath']) && $_SESSION['imgpath']){
echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit;
}
if(!in_array($data['file']['type'], $data['type_array'])){
echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit;
}
if($data['file']['size'] > $data['max_file_size']){
echo '<script>alert("您上传的稿件过大,请选择2M以下的图片上传!");</script>';exit;
} if(!is_dir($data['img_dir'])){
@mkdir($data['img_dir'], 0777, true);
}
$imgpath = $data['img_dir'].'/'.date('His', time()).rand(100, 999).$data['file']['name'];
$isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath);
if(!$isupload){
echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit;
}else{
echo '<script>alert("稿件上传成功!");</script>';
}
$_SESSION['name'] = $data['file']['name'];
$_SESSION['imgpath'] = $imgpath; return $imgpath;
}/*test_start*/
$sub_type = '';
if(isset($_POST['submit_upload']))$sub_type = 'upload';
if(isset($_POST['submit_delete']))$sub_type = 'delete';
if($sub_type){//echo '<script>alert("'.$sub_type.'");</script>';exit;
$data = array( 'sub_type' => $sub_type,
'file' => $_FILES['file'],
'img_tag_id' => 'picview',
'img_dir' => 'upload_img',
);
$imgpath = upload($data);
}else{
$imgpath = isset($_SESSION['imgpath'])? $_SESSION['imgpath']: '';
}
?><form action="a.php" method="post" enctype="multipart/form-data" target="frame">
<input type="file" name="file" class="input">
<input type="submit" name="submit_upload" value="上 传">
<input type="submit" name="submit_delete" value="删 除">
<iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
</form><img id="picview" height="100" style="display:none;"><script>
if("<?php echo $imgpath; ?>"){
parent.document.getElementById("picview").src = "<?php echo $imgpath; ?>";
parent.document.getElementById("picview").style.display = "block";
}
</script>
//目录结构如下(根目录中):目录cache(缓存目录-本程序中没用到)
目录config(配置文件目录-本程序中没用到)目录compile(编译文件目录)
目录smarty(smarty模板主文件目录)
目录template(模板目录)下面是代码//template目录中的b.html文件中的内容<form action="b.php?act=a" method="post" enctype="multipart/form-data" target="frame" id="form">
<input type="file" name="file" class="input">
<input type="submit" name="submit_upload" value="上 传">
<iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
</form>
<div id="div">
<% section name=loop loop=$return_data %>
<div id="div<% $return_data[loop].num %>"><img src="<% $return_data[loop].imgpath %>" height="100"><a id="a<% $return_data[loop].num %>" onclick="del(<% $return_data[loop].num %>)" style="cursor:pointer;">删除</a></div>
<% /section %>
</div>
<input type="hidden" id="name">
<input type="hidden" id="imgpath">
<input type="hidden" id="num"><script>
var request;
function createxmlHttpRequest(){
if(!request){
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}else{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
}function callback(num){
if (request.readyState == 4){
if (request.status == 200){
// alert(request.responseText);
parent.document.getElementById("div" + num).innerHTML = "";
parent.document.getElementById("div" + num).style.display = "none";
}else if(request.status == 404){
alert("该路径未找到");
}else if(request.status == 403){
alert("禁止访问");
}else{
alert("status is " + request.status);
}
}
}function del(num){
createxmlHttpRequest();
var url = "b.php";
var send = "act=a&submit_delete=1&num=" + num;
request.open("post", url, true);
request.onreadystatechange = function(){
callback(num);
}
request.setRequestHeader("content-type", "application/x-www-form-urlencoded");
request.send(send);
}
</script>
//根目录下的b.php文件中的内容<?php
//2010-7-20 17:57
//=============================================================================================================================
function p($var){echo '<pre>';if((is_array($var) || is_object($var)) && count($var)){print_r($var);}else{var_dump($var);}exit;}
//=============================================================================================================================$script = substr(__FILE__, strrpos(__FILE__, '\\')+ 1);
define('ROOT_PATH', str_replace('\\', '/', str_replace($script, '' ,__FILE__)));
include(ROOT_PATH.'smarty/libs/Smarty.class.php');
$s = new Smarty();$s->template_dir = ROOT_PATH.'template';
$s->compile_dir = ROOT_PATH.'compile';
$s->cache_dir = ROOT_PATH.'cache';
$s->config_dir = ROOT_PATH.'config';$s->debugging = false;
$s->caching = false;$s->left_delimiter = '<%';
$s->right_delimiter = '%>';class Fload{
/*---------------------------------------------------------------
* data 数组,包含元素如下:
* -------------------------------------------------------------
* sub_type 提交类型:upload-添加上传图片 delete-删除上传图片
* -------------------------------------------------------------
* file 上传图片的$_FILES['filename']
* -------------------------------------------------------------
* img_dir 图片上传的路径(相对路径)
* -------------------------------------------------------------
* num 图片上传/删除的标识
* -------------------------------------------------------------
*/
function upload($data){
$num = $data['num'];
if(!$data['file'] && $data['sub_type'] != 'delete'){
echo 'file_not_isnull';exit;
}
if(!$data['img_dir']){
echo 'pic_savepath_isnull';exit;
}
if(!isset($data['max_file_size'])){
$data['max_file_size'] = 1024 * 1024;
}else{
$data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
}
if(!isset($data['type_array'])){
$data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif');
}
if(!isset($data['sub_type'])){
$data['sub_type'] = 'upload';
} $imgpath = '';
if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){
if(isset($_SESSION['img'][$data['num']]['name']) && $_SESSION['img'][$data['num']]['name']){
if(is_file($_SESSION['img'][$data['num']]['imgpath'])){
$b = unlink($_SESSION['img'][$data['num']]['imgpath']);
}
unset($_SESSION['img'][$data['num']]);
if(!isset($_SESSION['img'][$data['num']]['name'])){
echo 'del_ok';
}else{
echo 'del_no';
}
}else{
echo 'no_pic';
}exit;
} if(!in_array($data['file']['type'], $data['type_array'])){
echo 'pic_type_no';exit;
}
if($data['file']['size'] > $data['max_file_size']){
echo 'pic_size_toobig';exit;
} if(!is_dir($data['img_dir'])){
mkdir($data['img_dir'], 0777, true);
}
$imgpath = $data['img_dir'].'/'.date('His', time()).rand(100, 999).$data['file']['name'];
$isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath);
if(!$isupload){
echo 'pic_upload_no';exit;
}
$_SESSION['img'][$num]['name'] = $data['file']['name'];
$_SESSION['img'][$num]['imgpath'] = $imgpath;
$_SESSION['img'][$num]['num'] = $num;
$_SESSION['num'] = $num + 1; $script = '<script>
var div = parent.document.getElementById("div");
if(div == null){
div = parent.document.createElement("div");
div.id = "div";
parent.document.getElementById("form").appendChild(div);
}
var child_div = parent.document.getElementById("div" + '.$num.');
if(child_div == null){
child_div = parent.document.createElement("div");
child_div.id = "div" + '.$num.';
}
var img = parent.document.createElement("img");
img.src = "'.$imgpath.'";
img.height = 100;
var a = parent.document.getElementById("a" + '.$num.');
if(a == null){
a = parent.document.createElement("a");
a.style.cursor = "pointer";
a.id = "a" + '.$num.';
a.innerHTML = "删除";
}
child_div.appendChild(img);
child_div.appendChild(a);
div.appendChild(child_div);
</script>';
echo $script;
foreach($_SESSION['img'] as $k => $v){
$event = '<script>
var a = parent.document.getElementById("a" + '.$v['num'].');
if(parent.document.addEventListener){
a.addEventListener("click", function(){del('.$v['num'].')}, false);
}else if(attachEvent){
a.attachEvent("onclick", new Function("del("+'.$v['num'].'+")"));
}
</script>';
echo $event;
$j++;
}
} //如果浏览器是FF(3.0),请用此函数将返回的数据重新排序
function k_sort($arr){
if(is_array($arr))sort($arr);
return $arr;
}
}
//================================================================test=================================================================
$fload = new Fload();
//$tpl = new Tpl();
if(!isset($_SESSION))session_start();
$num = isset($_SESSION['num'])? $_SESSION['num']: 0;$sub_type = '';
if(isset($_POST['submit_upload']))$sub_type = 'upload';
if(isset($_POST['submit_delete']))$sub_type = 'delete';
if($sub_type){
$num = isset($_POST['num'])? $_POST['num']: $num;//$_POST['num']为删除的图片数组num,后者的num为上传的图片数组num
$data = array('sub_type' => $sub_type,
'file' => $_FILES['file'],
'img_dir' => 'upload_img',
'num' => $num);
$return_data = $fload->upload($data);
}else{
$return_data = isset($_SESSION['img'])? $_SESSION['img']: '';
}
$return_data = $fload->k_sort($return_data);//支持FF$s->assign('return_data', $return_data);
$s->display('a.html');
?>