search
HomeBackend DevelopmentPHP Tutorialphp code to upload images

  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 echo 'The uploaded file is too large';

  43. exit ();
  44. }
  45. if (!in_arrar($type, $uptypes)) { //Determine the file type

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

  50. mkdir($destination_folder);
  51. }
  52. if (file_exists("upload/" . $_FILES["file" ]["name"])) {

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

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

  66. echo "Error moving file";
  67. exit ();
  68. }
  69. $pinfo = pathinfo($destination);

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

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

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

  102. case 1 : //加水印字符串
  103. imagestring($nimage, 2, 3, $image_size[1] - 15, $waterstring, $black);

  104. break;
  105. case 2 : //加水印图片
  106. $simage1 = imagecreatefromgif("xplore.gif");

  107. imagecopy($nimage, $simage1, 0, 0, 0, 0, 85, 15);
  108. imagedestroy($simage1);
  109. break;
  110. }
  111. switch ($iinfo[2]) {

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

  115. break;
  116. case 2 :
  117. imagejpeg($nimage, $destination);
  118. break;
  119. case 3 :
  120. imagepng($nimage, $destination);
  121. break;
  122. case 6 :
  123. imagewbmp($nimage, $destination);
  124. //imagejpeg($nimage, $destination);
  125. break;
  126. }
  127. //覆盖原上传文件

  128. imagedestroy($nimage);
  129. imagedestroy($simage);
  130. }
  131. if ($imgpreview == 1) {

  132. echo "
    图片预览:
    ";
  133. echo "图片预览:r文件名:";
  134. }
  135. }
  136. }
  137. ?>
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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 Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use