php 图片上传代码例子
下面来为你免费提供一款php 图片上传代码,如果你正在找文件上传的图片代码就进来看看吧,这是一款只支持jpg,gif,png,swf文件上传的php实例代码.
PHP实例代码如下:
- <?php
- if($_FILES['file']){
-
-
- $maxsize=10002400;
- $alltype=array(".jpg",".JPG",".GIF",".gif",".png",".swf");
- $imgtype=array(".jpg",".JPG",".GIF",".gif",".png",".swf");
-
-
- if($_FILES['file']['size']>$maxsize) {
- echo "您上传的资料大于10000K";
- exit;
- }
-
-
- $type=strstr($_FILES['file']['name'],".");
- if(!in_array($type,$alltype)){
- echo "不允许上传该类型的文件";
- exit;
- }
- $updir="uploaddir";
- $time=date("Ymd-His",time());
- $fn=$time.$type;
- $destination=$updir."/".$fn;
- if(@move_uploaded_file($_FILES['file']['tmp_name'], $destination)){
- @chmod($destination, 0777);
- $fileurl=$updir."/".$destination;
- $fileurl="".$destination;
-
- }else{
- echo "上传失败!";
- echo "<script>location.href=history.back()</script>";
- }
-
- }
- if($back=="no"):
- echo "ok";
- exit;
- endif;
- ?>
html代码如下:
- <html>
- <head>
- <title>添加图片</title>
- <script language="JavaScript">
- function isnull()
- {
- if(document.all.fileurl.value!="")insert();
- }
- function insert()
- {
- images='<img style="cursor:hand" src="'+insertpic.fileurl.value+'">';
- edit = window.opener.document.mode_iframe.document.selection.createRange();
- edit.pasteHTML (images);
- window.close();
- }
- </script>
- <style>
- BODY { FONT-SIZE: 12px; FONT-FAMILY: tahoma,宋体;}
- TABLE { empty-cells: show;}
- TD { FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: tahoma,宋体;margin: 5}
- </style>
- </head>
- <body leftMargin=12 topMargin=12 marginwidth=2 marginheight=2 bgcolor='#D6D3CE' style="border:0px" onload=isnull()>
- <form action=<?=$_SERVER['PHP_SELF']?> name=insertpic enctype=multipart/form-data method=post>
- <table>
- <tr>
- <td></td>
- <td width=60>上传图片</td>
- <td>
- <input class=mmcinb type="file" name="file" size="14">
- <input class=mmcinb type="submit" value="上传"></td>
- </tr>
- <tr>
- <td></td>
- <td>图片URL</td>
- <td><input class=mmcinb type="text" name="fileurl" value="<?=$fileurl?>" size="26">
- <button onclick=insert()>插入</button></td>
- </tr>
- <tr>
- <td></td>
-
- <td colspan=2 align=left><font color=red>可以直接输入图片的URL,点击插入即可(不能有中文)<br>
- </font></td>
- </tr>
- </table>
- </form>
- <script language="JavaScript">
- <!--
- //picurl.focus();
- //-->
- </script>
- </body>
- </html>