search
HomeBackend DevelopmentPHP TutorialPHP file upload and error code summary_PHP tutorial

PHP file upload and error code summary_PHP tutorial

Jul 13, 2016 pm 04:56 PM
phpuploadintroducecodeaboutSummarizedocumentarticlehaveofmistake

This article summarizes the introduction to PHP file upload and error codes. Friends who need to learn can refer to this article.

Upload operation code

The code is as follows Copy code









If($_FILES['myfile']['error'] > 0) { //Determine whether the file can be successfully uploaded to the server, 0 means the upload is successful
echo 'Error: ';

switch ( $_FILES['myfile']['error'] ) {
case UPLOAD_ERR_OK:
                         $response = 'There is no error, the file uploaded with success.';
break;
case UPLOAD_ERR_INI_SIZE:
                          $response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
break;
case UPLOAD_ERR_FORM_SIZE:
                        $response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
break;
case UPLOAD_ERR_PARTIAL:
                         $response = 'The uploaded file was only partially uploaded.';
break;
case UPLOAD_ERR_NO_FILE:
                        $response = 'No file was uploaded.';
break;
case UPLOAD_ERR_NO_TMP_DIR:
                        $response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
break;
case UPLOAD_ERR_CANT_WRITE:
                            $response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
break;
case UPLOAD_ERR_EXTENSION:
                             $response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
break;
                   default:
                     $response = 'Unknown error';
break;
          }
echo $response;
                                                                                                                                                       Exit; //If $_FILES['myfile']['error'] is greater than 0, there is an error, output the error message and exit the program
}
                     
//Get the main type and subtype of the MIME type of the uploaded file
List($maintype,$subtype)=explode("/",$_FILES['myfile']['type']);
                     
If ($maintype=="text") { //Text files cannot be uploaded through main type restrictions, such as .txt.html.php and other files
                     
echo 'Problem: Unable to upload text file. ';
exit; //Exit the program if the user uploads a text file
                                                                            }

$upfile = './uploads/'.time().$_FILES['myfile']['name']; //Define the location and new file name after uploading
                     
If ( is_uploaded_file($_FILES['myfile']['tmp_name']) ) { //Determine whether it is an uploaded file
                     
If ( !move_uploaded_file($_FILES['myfile']['tmp_name'], $upfile) ) { //Move files from
echo 'Problem: The file cannot be moved to the specified directory. ';
exit;
          }
                                                                            }else{
                                                                            echo 'Problem: The uploaded file is not a legal file: ';
echo $_FILES['myfile']['name'];
exit;
                                                                            }

echo 'File: '.$upfile.' Uploaded successfully, size: '.$_FILES['myfile']['size'].'!
'; //If the file is uploaded successfully, output
?>





html
The code is as follows
Copy code

table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7">  代码如下 复制代码

br />       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 
 


 

         
          选择文件:
         
     


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              ​​​​Select file:               

Some common error codes when uploading files

0 | UPLOAD_ERR_OK | File uploaded successfully
1 | UPLOAD_ERR_INI_SIZE | Size exceeds upload_max_filesize in php.ini.
2 | UPLOAD_ERR_FORM_SIZE | Size exceeds MAX_FILE_SIZE specified in HTML form.
3 | UPLOAD_ERR_PARTIAL | The file is not completely uploaded
4 | UPLOAD_ERR_NO_FILE | No file uploaded
5 | UPLOAD_ERROR_E | As expliained by @Progman, removed in rev. 81792
6 | UPLOAD_ERR_NO_TMP_DIR | Temporary folder not found
7 | UPLOAD_ERR_CANT_WRITE | Disk cannot be written
8 | UPLOAD_ERR_EXTENSION | File upload stopped by extension.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631586.htmlTechArticleThis article summarizes the introduction to PHP file upload and error codes. Friends who need to learn can refer to this article. . Upload operation code The code is as follows Copy code !DOCTYPE html PUBL...
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
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

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

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function