天天看點

php上傳圖檔得到路徑給表單字段指派的通用方法

1. 調用方法例子:

大圖路徑:<input type="text" name="bigimageurl" id="bigimageurl"  value="">

<iframe src="uppic.php?id=bigimageurl" width="600" height="25" frameborder="0" scrolling="no"></iframe>

小圖路徑:

<input type="text" name="smallimageurl" id="smallimageurl" value="">

<iframe src="uppic.php?id=smallimageurl" width="600" height="25" frameborder="0" scrolling="no"></iframe>

2. uppic.php

<?php

header("content-type:text/html;charset=gb2312");

?>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3c.org/tr/1999/rec-html401-19991224/loose.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>圖檔上傳</title>

<meta http-equiv=content-type content="text/html; charset=gb2312">

<meta content="mshtml 6.00.3790.4275" name=generator>

<style type="text/css">

<!--

input{border-width:1px;border:1px solid #bdbcbd;padding:3px 0 3px 5px;}

.inputbut{padding-left:3px;padding-right:2px;border:1px solid #bdbcbd;background:#fff url(../images/inputbut_bg.gif) left center repeat-x;font-size:12px;height:24px;}

-->

</style>

</head>

<body leftmargin=0 topmargin=0 style="font-size:12px">

$id=$_get["id"];

//echo "id==".$id;

switch($_get["action"])

{

case "up":

 upmovie($id);

 break;

default:

 upinput($id);

}

function upinput($id){

<script language=javascript>

function check()

 var strfilename=document.form.strphoto.value;

 if (strfilename=="")

 {

     alert("請選擇要上傳的檔案");

  document.form.strphoto.focus();

     return false;

   }

 return true;

</script>

<form action="uppic.php?action=up&id=<?=$id?>" enctype="multipart/form-data" name="form" method="post" onsubmit="if (!check()) return false;">

<input name="strphoto" type="file" id="strphoto" size="40">

<input type="submit" name="submit" value="上 傳" class=inputbut />

</form>

</body>

function upmovie($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 "<span style=/"color:red;line-height: 25px;/">格式錯誤請重新上傳<a href=# onclick=history.go(-1);>[傳回]</a></span>";

      exit;

   }

   $file_name=$str.".".$file_type;

   if(!copy($tmp_file,$savepath.$file_name)){

    echo "<span style=/"color:red;line-height: 25px;/">上傳錯誤請重試!!<a href=# onclick=history.go(-1);>[傳回]</a></span>";

   }else{

    //echo "<span style=/"olor:red;line-height: 25px;/">上傳成功</span><script>parent.document.getelementbyid(/"bigimageurl/").value=/"".$file_name."/"</script>";

    echo "<span style=/"olor:red;line-height: 25px;/">上傳成功</span><script>parent.document.getelementbyid(/"{$id}/").value=/"".$file_name."/"</script>";

    echo "<a href=# onclick=history.go(-1);>,若需要修改,請重新上傳</a>";

 }else{

  echo "<span style=/"color:red;line-height: 25px;/">請選擇需要上傳的檔案<a href=# onclick=history.go(-1);>[傳回]</a></span>";

 }

注意: 不同的文本框 需要定義id,  iframe url :   uppic.php?id=文本框id