PHP 上传图片精简版

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>   
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <html>    
    <head>    
    <title>图片上传</title>     
    </head>    
<body>   
<?php    
  $uptypes=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png');    
  $addtime=date("Ym",time());      
  $testdir="./upload/".$addtime."/";   
  if(file_exists($testdir)):   
  else:   
  mkdir($testdir,0777);   
  endif;
  $max_file_size=2097152;     //上传文件大小限制, 单位BYTE    
  $destination_folder=$testdir; //上传文件路径    
  $imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);    
  $imgpreviewsize=1/2;    //缩略图比例    
?> 
<form enctype="multipart/form-data" method="POST" name="upform">    
上传文件:    
<input name="upfile" type="file">    
<input type="submit" value="上传"><br>    
允许上传的文件类型为:<?=implode(',',$uptypes)?>    
</form>    
    <?php    
    if ($_SERVER['REQUEST_METHOD'] == 'POST')    
    {
        if (!is_uploaded_file($_FILES["upfile"]["tmp_name"]))    
        //是否存在文件    
        {
             echo "图片不存在!";    
             exit;    
        }
        $file = $_FILES["upfile"];    
        if($max_file_size < $file["size"])    
        //检查文件大小    
        {
            echo "文件太大!";    
            exit;    
        }
        if(!in_array($file["type"], $uptypes))    
        //检查文件类型    
        {    
            echo "文件类型不符!".$file["type"];    
            exit;    
        }
        if(!file_exists($destination_folder))    
        {
			mkdir($destination_folder);    
        }
        $filename=$file["tmp_name"];    
        $image_size=getimagesize($filename);    
        $pinfo=pathinfo($file["name"]);    
        $ftype=$pinfo['extension'];    
        $destination = $destination_folder.time().".".$ftype;    
        if (file_exists($destination) && $overwrite != true)    
        {    
            echo "同名文件已经存在了";    
            exit;    
        }    
        
        if(!move_uploaded_file ($filename, $destination))    
        {    
            echo "移动文件出错";    
            exit;    
        }
        $pinfo=pathinfo($destination);    
        $fname=$pinfo["basename"];    
        echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>";    
        echo " 宽度:".$image_size[0];    
        echo " 长度:".$image_size[1];    
        echo "<br> 大小:".$file["size"]." bytes";    
        if($imgpreview==1)    
        {
        echo "<br>图片预览:<br>";    
        echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);    
        echo " title=\"图片预览:\r文件名:".$destination."\r上传时间:\">";    
        }    
    }
    ?>    
</body>    
</html> 

 

效果图:


 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
精简版,在ASP、PHP、JSP、ASP.NET四个平台通用 web访问http://localhost/xxx/view.asp 其中xxx可以是任何的目录,例如: 1、xxx=web--------->http://localhost/web/view.asp 2、xxx=pc/web--------->http://localhost/pc/web/view.asp 本地目录:在某目录xx下有如下结构, ┎─[.\] ┠─────view.asp ┃ ┠────┰[ewebeditor] ┃    ┃ ┃    ┖─ewebeditor.asp ┃ ┠────┰[admin] ┃    ┃ ┃    ┖─edit.asp ┃ ┠─────[uploadfile] 目录下有显示文件view.asp、编辑目录ewebeditor、管理目录admin、存放图片的目录uploadfile admin目录下有管理的edit.asp,用于编辑要显示的内容。 ewebeditor相对路径设置如下: -----设置-------------说明 【路径模式】:相对路径 【上传路径】:../uploadfile/ ;该路径的设置是相对于ewebeditor.asp的路径 【显示路径】:/xxx/ ;该路径是设置upload文件的位置路径,其设置是相对于http://localhost的路径 【内容路径】:uploadfile/ ;该路径的设置是相对于view.asp的路径 ======================== 花费了好多的时间才搞定的。^_^ 至于当服务器的xxx文件目录变化后,只有ewebeditor、uploadfile和admin的位置不变,只要修改【显示路径】就可以了。 如果[uploadfile]目录改变,对不起了,只有修改数据库中所有存放图片的位置,不过也简单,一条命令一个函数搞定:replace("uploadfile/","xxx/uploadfile/")循环一下咯。不过记得备份数据库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值