Home Backend Development PHP Tutorial php code to upload images

php code to upload images

Jul 25, 2016 am 09:04 AM

  1. /*

  2. * Parameter description
  3. * $max_file_size: Upload file size limit, unit BYTE
  4. * $destination_folder: Upload file path
  5. * $watermark: Whether to attach a watermark ( 1 means adding watermark, others means not adding watermark);
  6. * http://bbs.it-home.org
  7. * Instructions for use:
  8. * 1. In front of the line "extension=php_gd2.dll" in the PHP.INI file Remove the ; sign because we need to use the GD library;
  9. * 2. Change extension_dir = to the directory where your php_gd2.dll is located;
  10. */
  11. // Upload file type list
  12. $uptypes = array (
  13. 'image/ jpg',
  14. 'image/png',
  15. 'image/jpeg',
  16. 'image/pjpeg',
  17. 'image/gif',
  18. 'image/bmp',
  19. 'image/x-png'
  20. );
  21. $max_file_size = 20000000; //Upload file size limit, unit BYTE
  22. $destination_folder = 'uploadimg/'; //Upload file path
  23. $watermark = 1; //Whether to attach a watermark (1 means adding a watermark, others means not adding a watermark );
  24. $watertype = 1; //Watermark type (1 is text, 2 is picture)
  25. $waterposition = 1; //Watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner corner, 5 is centered);
  26. $waterstring = "http://bbs.it-home.org/"; //Watermark string
  27. $waterimg = "xplore.gif"; //Watermark image
  28. $imgpreview = 1 ; //Whether to generate a preview image (1 means generate, others do not generate);
  29. $imgpreviewsize = 1 / 2; //Thumbnail ratio
  30. ?>
  31. ZwelL picture upload program
  32. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  33. //Determine whether there is an uploaded file

  34. if (is_uploaded_file($_FILES['upfile']['tmp_name']) ) {

  35. $upfile = $_FILES['upfile'];
  36. print_r($_FILES['upfile']);
  37. $name = $upfilep['name']; //File name
  38. $type = $upfile[' type']; //File type
  39. $size = $upfile['size']; //File size
  40. $tmp_name = $upfile['tmp_name']; //Temporary file
  41. $error = $upfile['error' ]; //Cause of error

  42. if ($max_file_size < $size) { //Determine the size of the file

  43. echo 'The uploaded file is too large';
  44. exit ();
  45. }< /p>
  46. if (!in_arrar($type, $uptypes)) { //Determine the file type

  47. echo 'Uploaded file type does not match' . $type;
  48. exit ();
  49. }
  50. if (!file_exists($destination_folder)) {

  51. mkdir($destination_folder);
  52. }

  53. if (file_exists("upload/" . $_FILES["file" ]["name"])) {

  54. echo $_FILES["file"]["name"] . " already exists. ";
  55. } else {
  56. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
  57. echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  58. }
  59. $pinfo = pathinfo($name);

  60. $ftype = $pinfo['extension'];
  61. $destination = $destination_folder . time() . "." . $ftype;
  62. if (file_exists ($destination) && $overwrite != true) {
  63. echo "The file with the same name already exists";
  64. exit ();
  65. }

  66. if (!move_uploaded_file($tmp_name, $destination )) {

  67. echo "Error moving file";
  68. exit ();
  69. }

  70. $pinfo = pathinfo($destination);

  71. $fname = $pinfo[basename];
  72. echo " 已经成功上传
    文件名: " . $destination_folder . $fname . "
    ";
  73. echo " 宽度:" . $image_size[0];
  74. echo " 长度:" . $image_size[1];
  75. echo "
    大小:" . $file["size"] . " bytes";

  76. if ($watermark == 1) {

  77. $iinfo = getimagesize($destination, $iinfo);
  78. $nimage = imagecreatetruecolor($image_size[0], $image_size[1]);
  79. $white = imagecolorallocate($nimage, 255, 255, 255);
  80. $black = imagecolorallocate($nimage, 0, 0, 0);
  81. $red = imagecolorallocate($nimage, 255, 0, 0);
  82. imagefill($nimage, 0, 0, $white);
  83. switch ($iinfo[2]) {
  84. case 1 :
  85. $simage = imagecreatefromgif($destination);
  86. break;
  87. case 2 :
  88. $simage = imagecreatefromjpeg($destination);
  89. break;
  90. case 3 :
  91. $simage = imagecreatefrompng($destination);
  92. break;
  93. case 6 :
  94. $simage = imagecreatefromwbmp($destination);
  95. break;
  96. default :
  97. die("不支持的文件类型");
  98. exit;
  99. }

  100. imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]);

  101. imagefilledrectangle($nimage, 1, $image_size[1] - 15, 80, $image_size[1], $white);

  102. switch ($watertype) {

  103. case 1 : //加水印字符串

  104. imagestring($nimage, 2, 3, $image_size[1] - 15, $waterstring, $black);

  105. break;
  106. case 2 : //加水印图片

  107. $simage1 = imagecreatefromgif("xplore.gif");

  108. imagecopy($nimage, $simage1, 0, 0, 0, 0, 85, 15);
  109. imagedestroy($simage1);
  110. break;
  111. }

  112. switch ($iinfo[2]) {

  113. case 1 :
  114. //imagegif($nimage, $destination);

  115. imagejpeg($nimage, $destination);

  116. break;
  117. case 2 :
  118. imagejpeg($nimage, $destination);
  119. break;
  120. case 3 :
  121. imagepng($nimage, $destination);
  122. break;
  123. case 6 :
  124. imagewbmp($nimage, $destination);
  125. //imagejpeg($nimage, $destination);
  126. break;
  127. }

  128. //覆盖原上传文件

  129. imagedestroy($nimage);
  130. imagedestroy($simage);
  131. }

  132. if ($imgpreview == 1) {

  133. echo "
    图片预览:
    ";
  134. echo "图片预览:r文件名:";
  135. }
  136. }
  137. }
  138. ?>
复制代码

您可能感兴趣的文章: php 多图片上传的简单例子(图文) php文件上传时遇到的一个问题(move_uploaded_file) php普通表单多文件上传的代码 了解PHP文件上传的原理 php简单文件上传的例子 php判断上传文件的文件类型的几种方法 一个php上传下载文件的源码 一个好用的php文件上传处理类 php上传多个文件的代码 php多文件上传的三种方法 php上传图片功能的实现



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)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

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.

See all articles