程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP上傳圖片功能

PHP上傳圖片功能

編輯:關於PHP編程

[html] 
<form name="myform" id="myform" class="registerform" method="post" action="?action=add"> 
 
<input name="pic1" id="pic1" type="text" style="width:293px;"/> 
<input name="" type="button"  class="button02" onClick="window.open('admin_upload.php?formName=myform&editName=pic1&upPath=/attached','','status=no,scrollbars=no,top=100,left=210,width=420,height=165')" /> 
 
</form> 

admin_upfile.php
[php] 
<html> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <?php 
    date_default_timezone_set('PRC'); 
//這裡上傳 $upsize判斷上傳文件的大小 
    $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] : "/attached/";              //文件上傳路徑 
    $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : "myform";          //回傳到上頁面編輯框所在Form的Name 
    $editName = isset($_REQUEST["editName"]) ? $_REQUEST["editName"] : $_REQUEST["editName"];              //回傳到上頁面編輯框的Name 
//轉換根目錄的路徑 
    if (strpos($uppath, "/") == 0) { 
        $i = 0; 
        $thpath = $_SERVER["SCRIPT_NAME"]; 
        $thpath = substr($thpath, 1, strlen($thpath)); 
        while (strripos($thpath, "/") !== false) { 
            $thpath = substr($thpath, strpos($thpath, "/") + 1, strlen($thpath)); 
            $i = ++$i; 
        } 
 
        $pp = ""; 
        for ($j = 0; $j < $i; ++$j) { 
            $pp .="../"; 
        } 
 
        $uppaths = $pp . substr($uppath, 1, strlen($thpath)); 
    } 
    $filename = date("y-m-d"); 
    if (is_dir($uppaths . $filename) != TRUE) 
        mkdir($uppaths . $filename, 0777); 
// if(is_dir($filename."/".$ctime)!=TRUE) mkdir($filename."/".$ctime,0777); 
 
    $f = $_FILES['file1']; 
 
    if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png") { 
        echo "<script>alert('只能上傳圖片格式的文件');window.close()</script>"; 
        //echo $f['type']; 
        return false; 
    } 
 
 
//獲得文件擴展名 
    $temp_arr = explode(".", $f["name"]); 
    $file_ext = array_pop($temp_arr); 
    $file_ext = trim($file_ext); 
    $file_ext = strtolower($file_ext); 
 
//新文件名 
    $new_file_name = md5(date("YmdHis")) . '.' . $file_ext; 
    //$new_file_name = md5(date("YmdHis") . '_' . rand(10000, 99999)) . '.' . $file_ext; 
 
    $dest = $uppaths . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //設置文件名為日期加上文件名避免重復 上傳目錄 
    $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //設置文件名為日期加上文件名避免重復 
    $r = move_uploaded_file($f['tmp_name'], $dest); 
    if ($f['size'] > 0) { 
 
        echo "<script>window.opener.document." . $formName . "." . $editName . ".value='" . $dest1 . "'</script>"; 
        echo "<script>alert('圖片上傳成功');window.close()</script>"; 
    } 
    ?> 
</html> 

admin_upload.php
[php] 
<html> 
    <head> 
        <title>圖片上傳</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
        <?php 
        $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] . "/" : "/attached/";              //文件上傳路徑 
        $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : "myform";          //回傳到上頁面編輯框所在Form的Name 
        $editName = isset($_REQUEST["editName"]) ? $_REQUEST["editName"] : $_REQUEST["editName"];            //回傳到上頁面編輯框的Name 
        ?> 
        <script language="javascript"> 
            <!-- 
            function mysub() 
            { 
                esave.style.visibility="visible"; 
            } 
            --> 
        </script> 
        <style type="text/css"> 
            <!-- 
            .STYLE2 { 
                font-size: 12pt 
            } 
            .STYLE3 { 
                font-size: 14pt 
            } 
            --> 
        </style> 
    </head> 
    <body oncontextmenu='return false' onselectstart="return false" oncopy="return false;" oncut="return false;"> 
        <form name="form1" method="post" action="admin_upfile.php" enctype="multipart/form-data" > 
            <div id="esave" style="position:absolute; top:18px; left:40px; z-index:10; visibility:hidden"> 
                <TABLE WIDTH=340 BORDER=0 CELLSPACING=0 CELLPADDING=0> 
                    <TR> 
                        <td width=20?></td> 
                        <TD bgcolor=#ff0000 width="60%"><TABLE WIDTH=100% height=120 BORDER=0 CELLSPACING=1 CELLPADDING=0> 
                                <TR> 
                                    <td bgcolor=#ffffff align=center><font color=red>正在上傳文件,請稍候...</font></td> 
                                </tr> 
                            </table></td> 
                        <td width=20?></td> 
                    </tr> 
                </table> 
            </div> 
            <table class="Border" width="100%" border="0" height="100%" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 
                <tr> 
                    <td align="center" class="title" height="30"><b><span class="style2 STYLE3">圖 片 上 傳</span> 
                            <input type="hidden" name="upPath" value="<?= $uppath ?>"> 
                            <input type="hidden" name="editName" value="<?= $editName ?>"> 
                            <input type="hidden" name="formName" value="<?= $formName ?>"> 
                        </b> </td> 
                </tr> 
                <tr bgcolor="#E8F1FF"> 
                    <td height="94" align="center" id="upid">選擇文件: 
                        <input type="file" name="file1" size="20" class="tx1"  value=""> 
                        <input type="submit" name="Submit" value="開始上傳" class="button" onClick="mysub();"> 
                        <br></td> 
                </tr> 
            </table> 
        </form> 
    </body> 
</html> 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved