网页上查看上传图片
1.在form中,必须有 enctype 提交方法必须是POST
<form class="formAjax" enctype="multipart/form-data" method="POST" id="modelform">
2.上传图片,可以在网页中查看
<tr rowspan='4'>
<?php if ($url_method =='create'): ?>
<img src="/images/photo-empty.jpg" alt=""
style="width:110px;height:139px;float:left;padding-left:210px;" id="photopath">
<?php else: ?>
<img src="" alt=""
style="width:110px;height:139px;float:left;padding-left:210px;" id="photopath">
<?php endif; ?>
</tr>
<tr>
<p class="short-input ue-clear">
<input type="file" name="addfile" id="proimg" style="position: relative;left: 145px;"
onchange="Preview(this,'photopath','divphotopath');">
</p>
</tr>
js代码
function Preview(obj, obj1, obj2) {
if (document.all) {
$("#" + obj1).css("width", "0px");
$("#" + obj1).css("height", "0px");
$("#" + obj2).css("display", "block");
document.getElementById('' + obj2 + '').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = obj.value;
} else {
$('#' + obj1).show();
var file = obj, objectURL = window.URL.createObjectURL(file.files[0]);
$('#' + obj1).attr("src", objectURL);
}
}
3.不能用ajax传递,必须用ajaxSubmit进行传递
$("#modelform").ajaxSubmit({
})
4.php后端代码
$config['upload_path'] = FCPATH . 'uploads/userphoto/';
$config['allowed_types'] = 'png|jpg|jpeg';
$config['max_size'] = '2000000';
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$CI =& get_instance();
$CI->load->library('upload', $config);
if ($_FILES['addfile']['name'] != '') {
if ($CI->upload->do_upload('addfile')) {
$str = $CI->upload->data();
$file = realpath(FCPATH . 'uploads/userphoto/');
if (!is_dir($file)) mkdir($file);
$filename = $datainfo[$this->mydb->getKeyId()] . date("YmdHis") . $str['file_ext'];
$file = $file . '/' . $filename;
if (is_file($file)) unlink($file);
copy($str['full_path'], $file);
unlink($str['full_path']);
$file = "http://" . $_SERVER["HTTP_HOST"] . '/uploads/userphoto/' . $filename;
$datainfo ['photopath'] = $file;
} else {
$result = Ousuclass::getJsonResult();
$result['success'] = FALSE;
$result['msg'] = '上传文件错误!';
echo json_encode($result);
exit;
}
}
原文:http://www.cnblogs.com/lqw4/p/4929528.html
posted on 2017-02-08 22:44 donaldbase 阅读(99) 评论(0) 收藏 举报

浙公网安备 33010602011771号