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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles