php 圖片上傳代碼例子

來源:互聯網
上載者:User

<?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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.