PHP page
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php // include 'templates/init.php'; //什么东西 //初始化act $_REQUEST['act']=$_REQUEST['act']?$_REQUEST['act']:''; if($_REQUEST['act']=='add'){ include 'templates/tupian_add.php'; }elseif($_REQUEST['act']=='insert'){ //文件上传 $brand=$_FILES['logo']; if($brand['error']==0){ //判断用户提交的图片格式是否是我要求的格式 $allow_type=array('image/gif','image/jpeg','image/png','image/pjpeg'); if(in_array($brand['type'],$allow_type)){ //符合要求 //在判断提交的大小 $max_size=20000000; if($brand['size']<=$max_size){ //容许上传到服务器 $new_file_name=time().mt_rand(10000,99999).strrchr($brand['name'],'.');//新文件夹 move_uploaded_file($brand['tmp_name'],'file_photos/'.$new_file_name); //var_dump($new_file_name); $brand_logo='file_photos/'.$new_file_name; //var_dump($title,$content,$time); include 'db.class.php'; $db=new db('localhost','root','123','fanyi'); $sql="insert into tupian values(null,'$brand_logo')"; if(mysql_query($sql)){ echo "图片添加成功"; //header("Refresh: 2; url=http://localhost/admin/brand.php?act=list"); } } } } }
-------------------------------- -------------------------------------------------- ----------------------------------
HTML page
<form method="post" action="tupian.php" name="theForm" enctype="multipart/form-data" onsubmit="return validate()"> <table cellspacing="1" cellpadding="3" width="100%"> <tr> <td class="label"><a href="JavaScript:showNotice('warn_brandlogo');" title="点击此处查看提示信息"> <img src="images/notice.gif" width="16" height="16" border="0" alt="点击此处查看提示信息"></a>图片LOGO</td> <td><input type="file" name="logo" id="logo" size="45"> <br /><span class="notice-span" style="display:block" id="warn_brandlogo"> 请上传图片 </span> </td> </tr> <tr> <td colspan="2" align="center"><br /> <input type="submit" class="button" value=" 确定 " /> <input type="reset" class="button" value=" 重置 " /> </td> </tr> </table> </form>
The above is the detailed content of PHP sample code sharing for uploading images from page to database. For more information, please follow other related articles on the PHP Chinese website!