Home php教程 php手册 php上传图片---初级版

php上传图片---初级版

Jun 06, 2016 pm 07:50 PM
php upload primary picture

没有样式,没有淘宝的那种放大截取大小的效果,只是实现了图片上传的功能。 图片超过100k,会出现内部错误服务器错误,需要手动更改配置文件里的MaxRequestLen属性。 下面粘上代码: 1 ? php 2 /* ********************************************************

没有样式,没有淘宝的那种放大截取大小的效果,只是实现了图片上传的功能。

图片超过100k,会出现内部错误服务器错误,需要手动更改配置文件里的MaxRequestLen属性。

下面粘上代码:

php上传图片---初级版php上传图片---初级版

<span>  1</span> <span>php
</span><span>  2</span> <span>/*</span><span>*****************************************************************************
</span><span>  3</span> 
<span>  4</span> <span>参数说明:
</span><span>  5</span> <span>$max_file_size  : 上传文件大小限制, 单位BYTE
</span><span>  6</span> <span>$destination_folder : 上传文件路径
</span><span>  7</span> <span>$watermark   : 是否附加水印(1为加水印,其他为不加水印);
</span><span>  8</span> 
<span>  9</span> <span>*****************************************************************************</span><span>*/</span>
<span> 10</span> 
<span> 11</span> <span>//</span><span>上传文件类型列表</span>
<span> 12</span> <span>$uptypes</span>=<span>array</span><span>(
</span><span> 13</span>     'image/jpg',
<span> 14</span>     'image/jpeg',
<span> 15</span>     'image/png',
<span> 16</span>     'image/pjpeg',
<span> 17</span>     'image/gif',
<span> 18</span>     'image/bmp',
<span> 19</span>     'image/x-png'
<span> 20</span> <span>);
</span><span> 21</span> 
<span> 22</span> <span>$max_file_size</span>=102400;     <span>//</span><span>上传文件大小限制, 单位BYTE</span>
<span> 23</span> <span>$destination_folder</span>="uploadimg/"; <span>//</span><span>上传文件路径</span>
<span> 24</span> <span>$watermark</span>=1;      <span>//</span><span>是否附加水印(1为加水印,其他为不加水印);</span>
<span> 25</span> <span>$watertype</span>=1;      <span>//</span><span>水印类型(1为文字,2为图片)</span>
<span> 26</span> <span>$waterposition</span>=1;     <span>//</span><span>水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);</span>
<span> 27</span> <span>$waterstring</span>="http://www.xplore.cn/";  <span>//</span><span>水印字符串</span>
<span> 28</span> <span>$waterimg</span>="xplore.gif";    <span>//</span><span>水印图片</span>
<span> 29</span> <span>$imgpreview</span>=1;      <span>//</span><span>是否生成预览图(1为生成,其他为不生成);</span>
<span> 30</span> <span>$imgpreviewsize</span>=1/2;    <span>//</span><span>缩略图比例</span>
<span> 31</span> ?>
<span> 32</span> 
<span> 33</span> 
<span> 34</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 35</span> <title>php上传图片</title>
<span> 36</span> 
<span> 37</span> 
<span> 38</span> 
<span> 39</span> 
<span> 40</span> 
Copy after login
41

上传文件: 42 43

44 45

允许上传的文件类型为:=implode(', ',$uptypes)?>

46
47 48 php 49 if ($_SERVER['REQUEST_METHOD'] == 'POST') 50 { 51 $file = $_FILES["upfile"]; 52 if($max_file_size >= $file["size"] && in_array($file["type"], $uptypes) && file_exists($destination_folder) && is_uploaded_file($_FILES["upfile"]['tmp_name'])) 53 { 54 55 $filename=$file["tmp_name"];//图片的临时名称 56 $image_size = getimagesize($filename);//图片大小 57 $pinfo=pathinfo($file["name"]);//返回包含dirname,basename 和 extension的url 58 $ftype=$pinfo['extension'];//图片类型,后缀名 59 $destination = $destination_folder.time().".".$ftype;//目标图片的名称 60 if (file_exists($destination) && $overwrite != true) 61 { 62 echo "同名文件已经存在了"; 63 exit; 64 } 65 66 if(!move_uploaded_file ($filename, $destination)) 67 { 68 echo "移动文件出错"; 69 exit; 70 } 71 72 $pinfo=pathinfo($destination);//返回包含dirname,basename 和 extension的url 73 $fname=$pinfo['basename'];//目标图片的名称包含后缀名 74 echo " 已经成功上传
文件名: ".$destination_folder.$fname."
"; 75 echo " 宽度:".$image_size[0];// 76 echo " 长度:".$image_size[1];// 77 echo "
大小:".$file["size"]." bytes";//大小 78 79 if($watermark==1) 80 { 81 $iinfo=getimagesize($destination,$iinfo); 82 $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);//新建一个真彩色图像 83 $white=imagecolorallocate($nimage,255,255,255);//为一幅图像分配颜色,黑色 84 $black=imagecolorallocate($nimage,0,0,0);//为一幅图像分配颜色,白色 85 $red=imagecolorallocate($nimage,255,0,0);//为一幅图像分配颜色 86 imagefill($nimage,0,0,$white);//填充,0,0代表坐标 87 switch ($iinfo[2]) 88 { 89 case 1: 90 $simage =imagecreatefromgif($destination);//由文件或URL创建一个新gif图象 91 break; 92 case 2: 93 $simage =imagecreatefromjpeg($destination);//由文件或URL创建一个新jpeg图象 94 break; 95 case 3: 96 $simage =imagecreatefrompng($destination);//由文件或URL创建一个新png图象 97 break; 98 case 6: 99 $simage =imagecreatefromwbmp($destination);//由文件或URL创建一个新bmp图象 100 break; 101 default: 102 die("不支持的文件类型"); 103 exit; 104 } 105 106 imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); 107 imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); 108 109 switch($watertype) 110 { 111 case 1: //加水印字符串 112 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);//col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处 113 break; 114 case 2: //加水印图片 115 $simage1 =imagecreatefromgif("xplore.gif");//由文件或URL创建一个新图象 116 imagecopy($nimage,$simage1,0,0,0,0,85,15);//拷贝图像的一部分 117 imagedestroy($simage1);//销毁一图像 118 break; 119 } 120 121 switch ($iinfo[2]) 122 { 123 //输出图象到浏览器或文件 124 case 1: 125 //imagegif($nimage, $destination); 126 imagejpeg($nimage, $destination); 127 break; 128 case 2: 129 imagejpeg($nimage, $destination); 130 break; 131 case 3: 132 imagepng($nimage, $destination); 133 break; 134 case 6: 135 imagewbmp($nimage, $destination); 136 //imagejpeg($nimage, $destination); 137 break; 138 } 139 140 //覆盖原上传文件 141 imagedestroy($nimage); 142 imagedestroy($simage); 143 } 144 145 if($imgpreview==1) 146 { 147 echo "
图片预览:
"; 148 echo "php上传图片---初级版$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize); 149 echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">"; 150 } 151 } 152 else 153 { 154 if (!is_uploaded_file($_FILES["upfile"]['tmp_name'])) 155 { 156 //是否存在文件 157 echo "图片不存在!"; 158 exit; 159 } 160 if($max_file_size $file["size"]) 161 { 162 //检查文件大小 163 echo "文件太大!"; 164 exit; 165 } 166 167 if(!in_array($file["type"], $uptypes)) 168 { 169 //检查文件类型 170 echo "文件类型不符!".$file["type"]; 171 exit; 172 } 173 174 if(!file_exists($destination_folder)) 175 { 176 mkdir($destination_folder);//尝试新建一个由 $destination_folder 指定的目录 177 } 178 } 179 } 180 ?> 181 182 View Code

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles