Get the App
SLTechnology News&Howtos  ›  Network Security  › 

How to upload pictures with PHP

Shulou Source: shulou.com Published: 2022-06-01 01:24:42 10月04日 Update

This article mainly explains "how to upload pictures in PHP". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to upload pictures on PHP".

First of all, we add the upload attachment # file and the upload button # imgbut to the form form, and remember to add multipart/form-data to the form form, otherwise you won't be able to upload the image.

Judgment of various restrictions before upload:

If ($_ FILES ["img"] ["error"] > 0) {switch ($_ FILES ["img"] ["error"]) {case 1: echo "alert ('the uploaded file exceeds the upload_max_filesize option limit in php.ini

'); history.back (); "; break; case 2: echo" alert (' the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form'); history.back (); break; case 3: echo "alert ('only part of the file is uploaded'); history.back ();" Break; case 4: echo "alert ('no files were uploaded'); history.back ();"; break; default: echo "alert ('unknown error'); history.back ();";} exit;} $maxsize = 1000000 / / step 2 uses $_ FILES ["pic"] ["size"] to limit the size in bytes 2M=2000000 if ($_ FILES ["img"] ["size"] > $maxsize) {echo "alert ('the uploaded file is too large to exceed {$maxsize} bytes'); history.back ();"; exit } / / step 3 uses $_ FILES ["pic"] ["type"] or file extension restriction type MIME image/gif image/png gif png jpg $allowtype = array ("png", "gif", "jpg", "jpeg"); $arr = explode (".", $_ FILES ["img"] ["name"]); $hz = $arr [count ($arr)-1] If (! in_array ($hz, $allowtype)) {echo "alert ('this is a type not allowed'); history.back ();"; exit;}

Upload pictures:

$filepath = "uploads/"; / / to match the path of UBB $randname = date ("Y"). Date ("m") Date ("d") Date ("H") Date ("I"). Date ("s"). Rand (100999). "." $hz; / / move the files in the temporary location to the specified directory to if (is_uploaded_file ($_ FILES ["img"] ["tmp_name"]) {if (move_uploaded_file ($_ FILES ["img"] ["tmp_name"], $filepath. $randname) {echo "alert ('upload successful'); history.back ();"; session_start (); $_ SESSION ['images'] = $filepath. $randname; / / echo $_ SESSION ['images'];} else {echo "alert (' upload failed'); history.back ();";}} else {echo "alert ('not an upload file'); history.back ();" } at this point, I believe you have a deeper understanding of "how to upload pictures on PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File picture limit function form content size byte type learning practical deeper success location interest unit only practical practical extension Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS Docker Huawei MariaDB