谁能给我一个纯PHP编写的照片路径存储的实例! 各位大虾们,我是一个PHP菜鸟,我现在想在我的表单中加入一个照片框,当点击这个框的时候出现选择照片的对话框,选定照片后整个照片要完整的显示在这个框内,当点击“保存”按钮的时候,所有表单内的数据包括照片的路径也存储于数据库中,这种编程的方法怎么实现,请高手给些实例,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?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> //以下代码全部在a.php文件中(实现iframe仿ajax进行图片上传预览,路径存在了session中,楼主可以将路径也存在数据库中),楼主建立个a.php,然后将代码放进去运行下<?phpif(!isset($_SESSION))session_start();/** * $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> 有没有一款编辑器可以查看文件的逻辑层次? 我现在安装个WIN7,为什么安装一个appserv2,不能连接SQL 2008数据库呢 请问大家都用什么版本的LINUX! 关于DZ的Authcode函数转JS版的问题。 请问网站用哪中架构的服务器平台好相对好呢? 哪些个人主页比较优秀,请推荐一观 出现一个很古怪的问题,请高手帮忙! 如何判断输入是不是整数! 那里有免费的红旗linux服务器版下载,试用版也可以!!! php 导出execl 本地测试正常 到服务器上就直接错误 url的参数的问题 如何实现select的选择不同,隐藏域的值也不同?
//=========================================================================================================================
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>
if(!isset($_SESSION))session_start();/**
* $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>