首页 >php教程 >PHP源码 >图片上传管理

图片上传管理

PHP中文网
PHP中文网原创
2016-05-25 17:11:511470浏览

1.图片上传

一开始页面
 
<script type="text/javascript">
/*
打开window oper
*/
function dopic(url,height,width)
{
    return window.open(url,&#39;图片上传管理&#39;,"height="+height+",width="+height+",top=50,left=100,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no");
    }
 
//上传图片JS获取图片信息
function dianji(v)
{
    document.getElementById(&#39;pic&#39;).value=v;
    document.getElementById("show").innerHTML=&#39;<img src=\"&#39; + v + &#39;\") onload="javascript:if(this.width>200)this.width=200;">&#39;;
}
</script>
 
<input name="pic" id="pic" type="text" value="" style="width:300px;"/>
        <input name="uploadpic" type="button" id="imgFile" value="上 传" onclick="return dopic(&#39;upload.php&#39;,500,400);"/>
        <div id="show"></div>

2.upload.php

<?php
session_start();
if(!isset($_SESSION[&#39;username&#39;])){echo "<script>alert(&#39;Please Login !&#39;);location.href=&#39;login.php&#39;;</script>";}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#mydiv{ width:460px; height:360px;}
ul{
    width:450px;}
li{
    list-style-type:none; float:left; padding:4px;
    }
li img{
     cursor:pointer; border:2px #CCC groove}
li img:hover{
    cursor:pointer; border:2px #F00 groove}
.del{
    padding-left:30px;} 
.del a{
    text-decoration:none;
    color:#999;
    font-size:13px;}
.del a:hover{
    text-align:center;
    text-decoration:underline;
    background:#F00}
     
#changpage{
    text-align:center; margin-top:10px;}
#changpage a{
    color:#000; text-decoration:none}
#changpage a:hover{
    color:#F00; text-decoration:underline}
#up{
    text-align:center;
    background:#CCC;
    margin-top:10px;}
.top{
    color:#F00;
    font-size:12px;
    font-weight:bold;}
</style>
<script type="text/javascript">
function qd(v)
{
    window.opener.dianji(v);
    window.close();
    }
</script>
<script type="text/javascript" src="base/scripts/erp.js"></script>
<?php
if($_GET[&#39;del&#39;])
{
    $dir = &#39;upload/&#39;;
    if($fileopen = opendir($dir))
    {
        @unlink($dir.$_GET[&#39;del&#39;]);
        }
    }
?>
<div class="top">如果要使用以经上传的图片,请【单击】你需要的图片</div>
<div id="mydiv"><ul> 
<?php 
$resPath = &#39;upload/&#39;;
if ($dh = opendir($resPath))
{
 while (($file = readdir($dh)) !== false)
 {
  if($file !=&#39;.&#39; and $file !=&#39;..&#39; and end(explode(&#39;.&#39;,$file))!=&#39;db&#39;)
  {
   echo "<li><img src=".$resPath.$file." width=\"120px\" height=\"140px\" title=&#39;左击调用图片&#39; onclick=\"return qd(&#39;".$resPath.$file."&#39;);\"><br><span class=\"del\"><a onclick=\"return del(&#39;".$file."&#39;);\" href=\"upload.php?del=".$file."\" >【删除】</a></span></li>";
  }
 }
 closedir($dh);
}
?></ul>
</div> 
<div style="clear:both"></div>
<div id="changpage"></div> 
<div id="up">
<form id="upForm" action="upload_send.php" method="post" enctype="multipart/form-data" onsubmit="return startUpload();"> 
<input type="file" name="myfile" id="myfile" /> <input type="submit" name="submitBtn" value="确定上传" /> 
</form>
</div>
<script> 
var obj,j; 
var page=0; 
var nowPage=0;//当前页 
var listNum=6;//每页显示<ul>数 
var PagesLen;//总页数 
var PageNum=4;//分页链接接数(5个) 
onload=function(){ 
obj=document.getElementById("mydiv").getElementsByTagName("li"); 
j=obj.length 
PagesLen=Math.ceil(j/listNum); 
upPage(0) 
} 
function upPage(p){ 
nowPage=p 
//内容变换 
for (var i=0;i<j;i++){ 
obj[i].style.display="none"
} 
for (var i=p*listNum;i<(p+1)*listNum;i++){ 
if(obj[i])obj[i].style.display="block"
} 
//分页链接变换 
strS=&#39;<a href="###" onclick="upPage(0)">首页</a>  &#39;
var PageNum_2=PageNum%2==0?Math.ceil(PageNum/2)+1:Math.ceil(PageNum/2) 
var PageNum_3=PageNum%2==0?Math.ceil(PageNum/2):Math.ceil(PageNum/2)+1 
var strC="",startPage,endPage; 
if (PageNum>=PagesLen) {startPage=0;endPage=PagesLen-1} 
else if (nowPage<PageNum_2){startPage=0;endPage=PagesLen-1>PageNum?PageNum:PagesLen-1}//首页 
else {startPage=nowPage+PageNum_3>=PagesLen?PagesLen-PageNum-1: nowPage-PageNum_2+1;var t=startPage+PageNum;endPage=t>PagesLen?PagesLen-1:t} 
for (var i=startPage;i<=endPage;i++){ 
if (i==nowPage)strC+=&#39;<a href="###" style="color:red;font-weight:700;" onclick="upPage(&#39;+i+&#39;)">&#39;+(i+1)+&#39;</a> &#39;
else strC+=&#39;<a href="###" onclick="upPage(&#39;+i+&#39;)">&#39;+(i+1)+&#39;</a> &#39;
} 
strE=&#39; <a href="###" onclick="upPage(&#39;+(PagesLen-1)+&#39;)">尾页</a>  &#39;
strE2=nowPage+1+"/"+PagesLen+"页"+"  共"+j+"条"
document.getElementById("changpage").innerHTML=strS+strC+strE+strE2 
} 
</script>

3.upload_send.php

<?php
session_start();
if(!isset($_SESSION[&#39;username&#39;])){echo "<script>alert(&#39;Please Login !&#39;);location.href=&#39;login.php&#39;;</script>";}
if($_FILES[&#39;myfile&#39;]){
 
function uploadFile($file)
 
{     //上传路径 
    $destinationPath = "upload/";  
    if (!file_exists($destinationPath)){mkdir($destinationPath,0777);}  
     
    //命名后面部份随机;
    $str = &#39;0123456789abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLOMNOPQRSTUVWXYZ&#39;;
    $a = strlen($str);
        for($i=1;$i<6;$i++)
        {
            $a = rand(&#39;0&#39;,$a-1);
            $string.=substr($str,$a,1); 
        }
     
    //分割原来图片实名
    $name = end(explode(&#39;.&#39;,iconv(&#39;utf-8&#39;,&#39;gb2312&#39;,basename($file[&#39;name&#39;]))));
     
    //重命名,实名iconv(&#39;utf-8&#39;,&#39;gb2312&#39;,basename($file[&#39;name&#39;]))   
    $fileName = date(&#39;YmdHis&#39;).$string.".".$name;
     
    if (@move_uploaded_file($file[&#39;tmp_name&#39;],$destinationPath.$fileName)){ 
        return iconv(&#39;gb2312&#39;,&#39;utf-8&#39;,$fileName);
        }
    }
     
    function readFromFile($target_path)
     
        //读取文件内容 
        {
        $file = fopen($target_path,&#39;rb&#39;) or die("unable to open file");
        $fileContent = &#39;&#39;;
            while(!feof($file)){ 
                $str = fgets($file);
                $fileContent .= $str; 
            }fclose($file); 
       return $fileContent;}
     
    if($_FILES[&#39;myfile&#39;][&#39;name&#39;] == &#39;&#39;){echo "<script>alert(&#39;Don\&#39;t NULL&#39;);history.back();</script>";exit();}
     
    $file = $_FILES[&#39;myfile&#39;];
    $type = $_FILES[&#39;myfile&#39;][&#39;type&#39;];
    $size = $_FILES[&#39;myfile&#39;][&#39;size&#39;];
     
    if($size/1000 > 100){echo "<script>alert(&#39;Image Too Big&#39;);history.back();</script>";exit();}
     
    if($type != "image/jpeg" || $type != "image/png" || $type != "image/gif")
        {
            echo "<script>alert(&#39;Image Format Error&#39;);history.back();</script>";
            exit;
        }
         
    $pic = &#39;upload/&#39;;
    $fileName = uploadFile($file);
    $result = readFromFile($pic.$fileName);
    $img = "<img src=".$pic.$fileName.">";
    echo "<script>window.opener.dianji(&#39;".$pic.$fileName."&#39;);window.close();</script>";
}

6.jpg6.jpg

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn