Home > Backend Development > PHP Tutorial > How to obtain the path for uploading images in PHP and assign values ​​to form fields

How to obtain the path for uploading images in PHP and assign values ​​to form fields

WBOY
Release: 2016-07-29 09:05:12
Original
1163 people have browsed it

The example in this article describes how to obtain the path for uploading images in PHP and assign values ​​to form fields. Share it with everyone for your reference, as follows:

1. Example of calling method:

Large image path:

<input type="text" name="bigImageURL" value="">
<iframe src="uppic.php&#63;id=bigImageURL" width="600" height="25" frameborder="0" scrolling="no"></iframe>
Copy after login

Small image path:

<input type="text" name="smallImageURL" value="">
<iframe src="uppic.php&#63;id=smallImageURL" width="600" height="25" frameborder="0" scrolling="no"></iframe>
Copy after login

2. uppic.php

<&#63;php
header("Content-Type:text/html;charset=GB2312");
&#63;>
<!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 c 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>
<&#63;php
$id=$_GET["id"];
//echo "id==".$id;
switch($_GET["action"])
{
case "up":
 upmovie($id);
 break;
default:
 upinput($id);
 break;
}
function upinput($id){
&#63;>
<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&#63;action=up&id=<&#63;=$id&#63;>" enctype="multipart/form-data" name="form" method="post"  type="file" size="40">
<input type="submit" name="Submit" value="上 传" class=inputbut />
</form>
</BODY>
<&#63;php
}
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=# /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=# /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=# /a>";
  }
 }else{
 echo "<span style=/"color:red;line-height: 25px;/">请选择需要上传的文件<a href=# /a></span>";
 }
}
?>

Copy after login

Note: Different text boxes need to define ids, iframe url: uppic.php?id=text box id

Readers who are interested in more content related to PHP image operations can check out this site's special topic: "Summary of PHP graphics and image operation skills" 》

I hope this article will be helpful to everyone in PHP programming.

The above introduces the method of obtaining the path for uploading images in PHP and assigning values ​​to form fields, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template