用ThinkPHP框架做的测试
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传图片带标题</title>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<style type="text/css">
.center{width: 450px;margin: auto;text-align: center;border: 1px solid #ccc;}
input[type="button"]{width: 30px;height: 25px;}
input[type="file"]{width: 150px;border: 1px solid #ccc;}
</style>
<script type="text/javascript">
$(function () {
$('.but_add_cla').click(function () {
var template_html = $('#template_append_info').html();
$(this).parents('.center').append(template_html);
});
$('.but_add_cla_two').click(function () {
var template_html_two = $('#template_append_info_two').html();
$(this).parents('.center').append(template_html_two);
});
});
function remove_element(obj,oldimg,formids) {
if (oldimg){
var formidsval = formids ? formids : 'FormIds' ;
$('#'+formidsval).append('<input type="hidden" name="oldimg[]" value="'+oldimg+'">');
}
$(obj).parent().remove();
}
</script>
</head>
<body>
<form id="FormIds" action="{:U('Index/index')}" method="post" enctype="multipart/form-data">
<div class="center">
<p class="pval">
<input type="text" name="title[]" value="{$imgarr[0]['title']}">
<input type="file" name="img[]">
<input type="hidden" name="imgval[]" value="{$imgarr[0]['imgval']}">
<input type="button" class="but_add_cla" value="+">
<if condition="$imgarr[0]['imgval']">
<img src="{$imgarr[0]['imgval']}" width="20">
</if>
</p>
<volist name='imgarr' id='val'>
<if condition='$key gt 0'>
<p class="pval">
<input type="text" name="title[]" value="{$val.title}">
<input type="file" name="img[]">
<input type="hidden" name="imgval[]" value="{$val.imgval}">
<input type="button" value="-" onclick="remove_element(this,'{$val.imgval}');">
<img src="{$val.imgval}" width="20">
</p>
</if>
</volist>
</div>
<p style="text-align: center;margin-bottom: 100px;"><input type="submit" value="提交"></p>
</form>
<div id="template_append_info" style="display: none;">
<p class="pval">
<input type="text" name="title[]">
<input type="file" name="img[]">
<input type="button" value="-" onclick="remove_element(this);">
</p>
</div>
<form id="FormIdsTwo" action="{:U('Index/index')}" method="post" enctype="multipart/form-data">
<div class="center">
<p class="pval">
<input type="text" name="info[title][]" value="{$imgarrtwo[0]['title']}">
<input type="file" name="info[img][]">
<input type="hidden" name="info[imgval][]" value="{$imgarrtwo[0]['imgval']}">
<input type="button" class="but_add_cla_two" value="+">
<if condition="$imgarrtwo[0]['imgval']">
<img src="{$imgarrtwo[0]['imgval']}" width="20">
</if>
</p>
<volist name='imgarrtwo' id='val'>
<if condition='$key gt 0'>
<p class="pval">
<input type="text" name="info[title][]" value="{$val.title}">
<input type="file" name="info[img][]">
<input type="hidden" name="info[imgval][]" value="{$val.imgval}">
<input type="button" value="-" onclick="remove_element(this,'{$val.imgval}','FormIdsTwo');">
<img src="{$val.imgval}" width="20">
</p>
</if>
</volist>
</div>
<p style="text-align: center;margin-bottom: 100px;"><input type="submit" value="提交"></p>
</form>
<div id="template_append_info_two" style="display: none;">
<p class="pval">
<input type="text" name="info[title][]">
<input type="file" name="info[img][]">
<input type="button" value="-" onclick="remove_element(this);">
</p>
</div>
</body>
</html>
PHP:
/**
* 上传图片带标题/上传相册
*/
if (!empty($_FILES)){
$istype = array('jpg','jpeg','png','gif');
$path = 'uploads/testimg/';
$file_size = 10485760; //10M
$error = array('uptype'=>'上传文件类型不允许','uperror'=>'文件上传保存错误','upsize'=>'上传文件大小不能大于10M或文件大小不符合');
if (!is_dir($path)){
mkdir(iconv("UTF-8", "GBK", $path),0777,true);
}
if ($_FILES['img']['name']){
foreach ($_FILES['img']['name'] as $isk=>$isv){
if ($isv){
$ext = pathinfo($isv);
if (!in_array(strtolower($ext['extension']),$istype)){
$this->error($error['uptype']);die;
}
$size = $_FILES['img']['size'][$isk];
if (($size > $file_size) || (-1 == $file_size)){
$this->error($error['upsize']);die;
}
}
}
$arr_data = array();
foreach ($_FILES['img']['name'] as $key=>$value){
if ($value){
$type = pathinfo($value);
$tmp_name = $_FILES['img']['tmp_name'][$key];
$ImgName = time().mt_rand(0,9999);
$filename = $path.$ImgName.'.'.$type['extension'];
if (!move_uploaded_file($tmp_name,$filename)){
$this->error($error['uperror']);die;
}
$imgval = $filename;
if (file_exists($_POST['imgval'][$key])){
unlink($_POST['imgval'][$key]);
}
}else{
$imgval = $_POST['imgval'][$key];
}
if ($_POST['title'][$key] || $_POST['imgval'][$key] || $value){ /* 防止空数据 */
$arr_data[$key]['title'] = $_POST['title'][$key];
$arr_data[$key]['imgval'] = $imgval;
}
}
foreach ($_POST['oldimg'] as $kk=>$vv){
if ($vv && file_exists($vv)){
unlink($vv);
}
}
$img_data = addslashes(json_encode($arr_data));
$infoval['info'] = $img_data;
M('test')->where("id = 1")->save($infoval);
print_r($arr_data);
}
if ($_FILES['info']['name']){
foreach ($_FILES['info']['name']['img'] as $isk=>$isv){
if ($isv){
$ext = pathinfo($isv);
if (!in_array(strtolower($ext['extension']),$istype)){
$this->error($error['uptype']);die;
}
$size = $_FILES['info']['size']['img'][$isk];
if (($size > $file_size) || (-1 == $file_size)){
$this->error($error['upsize']);die;
}
}
}
$arr_data = array();
foreach ($_FILES['info']['name']['img'] as $key=>$value){
if ($value){
$type = pathinfo($value);
$tmp_name = $_FILES['info']['tmp_name']['img'][$key];
$ImgName = time().mt_rand(0,9999);
$filename = $path.$ImgName.'.'.$type['extension'];
if (!move_uploaded_file($tmp_name,$filename)){
$this->error($error['uperror']);die;
}
$imgval = $filename;
if (file_exists($_POST['info']['imgval'][$key])){
unlink($_POST['info']['imgval'][$key]);
}
}else{
$imgval = $_POST['info']['imgval'][$key];
}
if ($_POST['info']['title'][$key] || $_POST['info']['imgval'][$key] || $value){ /* 防止空数据 */
$arr_data[$key]['title'] = $_POST['info']['title'][$key];
$arr_data[$key]['imgval'] = $imgval;
}
}
foreach ($_POST['oldimg'] as $kk=>$vv){
if ($vv && file_exists($vv)){
unlink($vv);
}
}
$img_data = addslashes(json_encode($arr_data));
$infoval['info'] = $img_data;
M('test')->where("id = 2")->save($infoval);
print_r($arr_data);
}
}
欢迎大家一起来探讨!