您的位置:首页 > 编程语言 > PHP开发

学习笔记:PHP上传图片代码详解

2013-09-04 21:46 801 查看
想知道上传图片代码怎么写吗,下面我就带大家一起详细分析一下吧。利用PHP,你总是可以有多种方式来完成某个特定的任务。我们就拿文件上传举个例子。当然了,你可以按照传统的方式来使用HTTP文件上传,把文件直接传输到Web服务器磁盘上。
你还可以用更加奇异的方式上传,用FTP协议两步就完成上传:从你的本地硬盘到Web服务器,然后再到FTP服务器。PHP在本机同时支持FTP和HTTP上传,所以你可以根据自己应用程序的设计需要进行最佳的选择。使用PHP的FTP函数进行文件传输几乎与使用传统的FTP客户端相同――你会看到连函数的名字都和标准的FTP命令类似。和大家分享一下PHP上传图片代码的小例子,希望大家多多提意见嘿嘿谢谢了一起学习!!
PHP上传图片代码:
<?phpsession_start();?> <?php $id=mysql_connect('localhost','root','585858'); mysql_select_db("okhwyy",$id); mysql_query("setnamesgb2312"); ?> <html> <head> <metahttp-equivmetahttp-equiv="Content-Type"c> <title>限制上传图片的格式</title> <styletypestyletype="text/css"> <!-- .style1{ font-size:14px; font-family:"华文行楷"; } .style4{font-size:12px;font-weight:bold;} --> </style> </head> <body> <tablewidthtablewidth="406"height="129"border="0"align="center"cellpadding="0"cellspacing="0"background=""> <tr> <tdwidthtdwidth="106"height="40"></td> <tdwidthtdwidth="196"></td> <tdwidthtdwidth="31"></td> </tr> <formnameformname="form1"method="post"action=""enctype="multipart/form-data"> <tr> <tdheighttdheight="32"align="right"><spanclassspanclass="style1">图片路径</span>:</td> <tdvaligntdvalign="middle"><inputnameinputname="images"type="file"id="images2"size="15"> <inputtypeinputtype="hidden"name="MAX_FILE_SIZE"value="30000"></td> <td></td> </tr> <tr> <tdheighttdheight="44"align="right"valign="middle"><spanclassspanclass="style4">图片的格式</span>:</td> <tdvaligntdvalign="middle"><spanclassspanclass="style4">(.jpg)</span><inputtypeinputtype="submit"name="Submit"value="提交"></td> <td></td> </tr> </form> <tr> <tdheighttdheight="10"></td> <td></td> <td></td> </tr> </table> <tablewidthtablewidth="406"height="129"border="1"align="center"cellpadding="0"cellspacing="0"> <?php $query="select*fromtb_image2whereidorderbydatadesclimit2"; $result=mysql_query($query); if($result){ while($row=mysql_fetch_array($result)){ ?> <tr> <tdwidthtdwidth="106"align="center"><?phpecho$row[data];?></td> <tdwidthtdwidth="196"align="center"><imgsrcimgsrc="<?phpecho$row[path];?>"width="200"height="120"></td> </tr> <?php}}?> </table> </body> </html> <?php $Submit=$_POST[Submit]; if($Submit){ $image=$_FILES['images']['name']; $datedate=date("Y-m-d"); $path="upfiles/".$_FILES['images']['name']; $type=strstr($path,"."); $size=$_FILES['images']['size']; if($size>1000000){echo"<script>alert('上传容量超限');history.back();</script>";} elseif($type!=".jpg"){echo"<script>alert('上传类型不对');history.back();</script>";} elseif(move_uploaded_file($_FILES['images']['tmp_name'],$path)){ $query="insertintotb_image2(image_name,path,data)values('$image','$path','$date')"; $result=mysql_query($query)ordie(mysql_error()); if($result){ echo"上传成功!"; echo"<metahttp-equivmetahttp-equiv=\"Refresh\"content=\"3;url=index.php\">"; } else{ echo"上传失败!"; echo"<metahttp-equivmetahttp-equiv=\"Refresh\"content=\"3;url=index.php\">"; }}} ?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: