PHP getting started tutorial-detailed description of File Upload instances,
This document describes how to upload files in PHP. We will share this with you for your reference. The details are as follows:
Demo1.php
<Form enctype = "multipart/form-data" action = "Demo2.php" method = "post"> <input type = "hidden" name = "MAX_FILE_SIZE" value = "2000000 "/> upload files: <input type = "file" name = "userfile"/> <input type = "submit" value = "Upload"/> </form>
Demo2.php
<? Php // accept the uploaded file // $ _ FILES; // The file already exists, however, the null value // [userfile] [name] indicates the uploaded file name // [userfile] [type] indicates the file type. For example, the jpg file type is: image/jpeg // [userfile] [tmp_name] indicates the temporary storage location of the uploaded file C: \ WINDOWS \ temp \ php28.tmp // [userfile] [error] indicates the error type, indicates no error. // [Userfile] [size] indicates the size of the uploaded file print_r ($ _ FILES); echo '<br/> '; // is_uploaded_file -- determines whether the file is uploaded through http post // After the file is uploaded through http post, the file will be stored in the Temporary Folder $ fileMimes = array ('image/jpeg ', 'image/pjpeg ', 'image/gif', 'image/png', 'image/x-png '); // determine whether the type is an if (is_array ($ fileMimes) {if (! In_array ($ _ FILES ['userfile'] ['type'], $ fileMimes) {echo "<script> alert ('the site only allows jpg, png, gif image '); history. back (); </script> "; exit ;}// create a constant define ('url', dirname (_ FILE __). '\ uploads'); echo URL; // determine whether the directory exists if (! Is_dir (URL) {mkdir (URL, 0777); // maximum permission 0777, meaning if this directory is not available, create} define ('max _ size ', 2000000); if ($ _ FILES ['userfile'] ['SIZE']> MAX_SIZE) {echo "<script> alert ('upload cannot exceed 2 m'); history. back (); </script> "; exit;} // there are two other questions to verify // The second question, only jpg files are allowed // if ($ _ FILES ['userfile'] ['type']! = 'Image/jpeg '& $ _ FILES ['userfile'] ['type']! = 'Image/pjpeg ') {// echo "<script> alert ('only JPG images are allowed on this site'); history. back (); </script> "; // exit; //} // switch ($ _ FILES ['userfile'] ['type']) {// case 'image/jpeg ': // Firefox // break; // case 'image/pjpeg': // break; // case 'image/gif ': // break; // case 'image/png ': // Firefox // break; // case 'image/x-png': // IE // break; // default: echo "<script> alert ('only jpg, png, and gif image' are allowed on this site); history. back (); </script> "; // exit; //} // The first question, What if an upload error occurs? if ($ _ FILES ['userfile'] ['error']> 0) {switch ($ _ FILES ['userfile'] ['error']) {case 1: echo "<script> alert ('upload file exceeds the agreed value of 1'); history. back (); </script> "; break; case 2: echo" <script> alert ('upload file exceeds the agreed value 2'); history. back (); </script> "; break; case 3: echo" <script> alert ('partially upload'); history. back (); </script> "; break; case 4: echo" <script> alert ('not uploaded '); history. back (); </script> "; break;} exit;} if (is_uploaded _ File ($ _ FILES ['userfile'] ['tmp _ name']) {// move it here // move_uploaded_file -- move the uploaded file to the new location // The first parameter, write the address of the temporary file, // The second parameter, the second parameter should contain the address you want to exist. // first, judge whether the directory exists. // if you want to block the warning, add @ if (! Move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], URL. '/'. $ _ FILES ['userfile'] ['name']) {// if the operation fails, echo 'failed to move '; exit ;}} else {echo "<script> alert ('file uploaded cannot be found in the Temporary Folder '); history. back (); </script> "; exit ;} // upload is successful after all passes. // you must pass a value to the Demo3.php // File Upload address echo "<script> alert ('file uploaded successfully'); location. href = 'demo3. php? Url = ". $ _ FILES ['userfile'] ['name']." '; </script> ";?>
Demo3.php
<?php $url = $_GET['url']; echo "