php上传图片获取路径及给表单字段赋值的方法
本文实例讲述了php上传图片获取路径及给表单字段赋值的方法。分享给大家供大家参考,具体如下:
1.调用方法例子:
大图路径:
<inputtype="text"name="bigImageURL"id="bigImageURL"value=""> <iframesrc="uppic.php?id=bigImageURL"width="600"height="25"frameborder="0"scrolling="no"></iframe>
小图路径:
<inputtype="text"name="smallImageURL"id="smallImageURL"value=""> <iframesrc="uppic.php?id=smallImageURL"width="600"height="25"frameborder="0"scrolling="no"></iframe>
2.uppic.php
<?php
header("Content-Type:text/html;charset=GB2312");
?>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTMLxmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>图片上传</TITLE>
<METAhttp-equiv=Content-Typecontent="text/html;charset=gb2312">
<METAcontent="MSHTML6.00.3790.4275"name=GENERATOR>
<styletype="text/css">
<!--
input{border-width:1px;border:1pxsolid#bdbcbd;padding:3px03px5px;}
.inputbut{padding-left:3px;padding-right:2px;border:1pxsolid#bdbcbd;background:#FFFurl(../images/inputbut_bg.gif)leftcenterrepeat-x;font-size:12px;height:24px;}
-->
</style>
</HEAD>
<BODYleftmargin=0topmargin=0style="font-size:12px">
<?php
$id=$_GET["id"];
//echo"id==".$id;
switch($_GET["action"])
{
case"up":
upmovie($id);
break;
default:
upinput($id);
break;
}
functionupinput($id){
?>
<SCRIPTlanguage=javascript>
functioncheck()
{
varstrFileName=document.form.strPhoto.value;
if(strFileName=="")
{
alert("请选择要上传的文件");
document.form.strPhoto.focus();
returnfalse;
}
returntrue;
}
</SCRIPT>
<formaction="uppic.php?action=up&id=<?=$id?>"enctype="multipart/form-data"name="form"method="post"onsubmit="if(!check())returnfalse;">
<inputname="strPhoto"type="file"id="strPhoto"size="40">
<inputtype="submit"name="Submit"value="上传"class=inputbut/>
</form>
</BODY>
<?php
}
functionupmovie($id){
global$web_picdir;
$savePath=dirname(__FILE__)."/".$web_picdir;
$str=date('YmdHis');
if($_FILES['strPhoto']['name']!='')
{
$tmp_file=$_FILES['strPhoto']['tmp_name'];
$file_types=explode(".",$_FILES['strPhoto']['name']);
$file_type=$file_types[count($file_types)-1];
if(strtolower($file_type)!="jpg"&strtolower($file_type)!="gif"&strtolower($file_type)!="bmp"&strtolower($file_type)!="png"){
echo"<spanstyle=/"color:red;line-height:25px;/">格式错误请重新上传<ahref=#onclick=history.go(-1);>[返回]</a></span>";
exit;
}
$file_name=$str.".".$file_type;
if(!copy($tmp_file,$savePath.$file_name)){
echo"<spanstyle=/"color:red;line-height:25px;/">上传错误请重试!!<ahref=#onclick=history.go(-1);>[返回]</a></span>";
}else{
//echo"<spanstyle=/"olor:red;line-height:25px;/">上传成功</span><script>parent.document.getElementById(/"bigImageURL/").value=/"".$file_name."/"</script>";
echo"<spanstyle=/"olor:red;line-height:25px;/">上传成功</span><script>parent.document.getElementById(/"{$id}/").value=/"".$file_name."/"</script>";
echo"<ahref=#onclick=history.go(-1);>,若需要修改,请重新上传</a>";
}
}else{
echo"<spanstyle=/"color:red;line-height:25px;/">请选择需要上传的文件<ahref=#onclick=history.go(-1);>[返回]</a></span>";
}
}
?>
注意:不同的文本框需要定义id, iframeurl: uppic.php?id=文本框id
更多关于PHP图片操作相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。