首页  >  文章  >  后端开发  >  PHP上传图片功能_PHP教程

PHP上传图片功能_PHP教程

WBOY
WBOY原创
2016-07-14 10:06:501110浏览

[html] 

 
 
 
 
 
 

admin_upfile.php
[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             $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(&#39;只能上传图片格式的文件&#39;);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=&#39;" . $dest1 . "&#39;</script>"; 
        echo "<script>alert(&#39;图片上传成功&#39;);window.close()</script>"; 
    } 
    ?> 
 

admin_upload.php
[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 
        ?> 
         
         
     
     
       
 
             
             
                 
                     
                 
                 
                     
                 
           
图 片 上 传 
                             
                             
                             
                       
选择文件: 
                         
                         
                       
 
       
 
     
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477916.htmlTechArticle[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 onC...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn