哪位高手给我个PHP编写的一个存储照片路径的例子!

wangweitao 2010-10-21 09:39:09
我已经有一个表单了,我想在里面加一个照片的框,当点击这个框的时候出现像microsoft的对话框用来提示查找照片路径,当找到所需照片的时候,点击“确定”后照片在这个框内显示出来,当在这个页面点击“保存”按钮的时候,整个页面包括这个照片的路径也全部被保存在指定的数据库中。请高手给我一个PHP实例,我是一个菜鸟,万分感激!
...全文
120 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kyzy_yy_pm 2010-10-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wangweitao 的回复:]
太麻烦了吧
[/Quote]
麻烦其实也不麻烦,上面的代码多于的部分主要有
1、手写ajax,这个代码多了点,其实就是为了实现ajax请求而已,因为楼主要保存数据库而有不能让页面跳转所就要用ajax比较好,如果楼主将手写的ajax幻城jquery的ajax那么就会取出很多代码(这里是为了让楼主明白所以手写的ajax)
2、楼主说的类似ajax图片(其实是iframe)预览这里,那么楼主可以去除一写功能来一样可以减少很多代码
kyzy_yy_pm 2010-10-22
  • 打赏
  • 举报
回复
大致流程思路:


<?php
//=========================================================================================================================
function p($var){echo '<pre>';if((is_array($var)||is_object($var))&&count($var)){print_r($var);}else{var_dump($var);}exit;}
//=========================================================================================================================
//header("Content-type: text/html; charset=gbk");




if(!isset($_SESSION))session_start();

if(isset($_GET['save'] && isset($_SESSION['imgpath']) && is_file($_SESSION['imgpath']))){
//链接数据库
// $img_path = $_SESSION['imgpath'];
mysql_connect('locahost', 'db_user', 'db_pass');
// mysql_query('set names gbk');
//向数据库里插入就行了
$sql = 'insert into your_table values()';
if(mysql_affected_rows(mysql_query($sql)){
echo 'ok';exit;
}else{
echo 'fail';exit;
}
}
/**
* $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>
<input type="button" value="保存到数据库" onclick="save()">
<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";
}

var request;
function createxmlHttpRequest(){//判断浏览器类型,创建xmlHttpRequest对象
if(!request){
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}else{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
}

function save(){//get发送模式
createxmlHttpRequest();
var url = "a.php?save=1";
request.open("get", url, true);//同步或异步
request.onreadystatechange = callback;
request.send(null);
}

//function post_test(){//post发送模式
// createxmlHttpRequest();
// var url = "abc.php";
// var send = "text=" + encodeURI(document.getElementById("text").value);
// request.open("post", url, true);//false同步 true异步(默认的)
// request.onreadystatechange = callback;
// request.setRequestHeader("content-type", "application/x-www-form-urlencoded");
// request.send(send);
//}

function callback(){//回调函数
if (request.readyState == 4){
if (request.status == 200){
// alert(request.responseText);
if(request.responseText == "ok")alert("保存成功!");
else alert("保存失败!");
}else if(request.status == 404){
alert("该路径未找到");
}else if(request.status == 403){
alert("禁止访问");
}else{
alert("status is " + request.status);
}
}
}
</script>
















































































































































































































































































































































































































































































21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧