search
HomeBackend DevelopmentPHP TutorialPHP single file and multiple file upload examples_php examples

File upload and multi-file upload in PHP are like a nightmare for PHP beginners who are just getting started. This article gives you a detailed analysis of PHP Code to implement single file upload and multiple file upload as well as problem solutions. If you don’t understand PHP file upload, please be optimistic! !

$_FILES When is an empty array?

When the form submission enctype is not equal to multipart/form-data, in php.iniConfiguration file, file_uploads = Off The size of the uploaded file> is specified in the php.ini configuration file When the maximum upload size is configured

As long as $_FILES is an empty array, the above problem may occur and must be fixed!

If you click the "Upload Button" immediately without selecting any file, $_FILES will be an array with elements, and each attribute in the element will be empty String, error attribute For 4

##Single file upload

$_FILES data structure

array(
  'filename' => array(
    'name' => 'xxx.png',
    'type' => 'image/png',
    'size' => 2548863,
    'tmp_name' => '/img/sdsdsd.png',
    'error' => 0
  )
)

Whether it is a

single file Or Multiple file upload, there will be 5 fixed attributes: name / size / type / tmp_name / error

Multiple file upload

Compared with single file upload, multiple

file upload processing is much more complicated. The two forms of multi-file upload on the front end

//name相同
<form method="post" enctype="multipart/form-data">
  <input type="file" name="wt[]"/>
  <input type="file" name="wt[]"/>
  <input type="submit" value="提交"/>
</form>

//name不同(简单点)
<form method="post" enctype="multipart/form-data">
  <input type="file" name="wt"/>
  <input type="file" name="mmt"/>
  <input type="submit" value="提交"/>
</form>

The back end

$_FILES The corresponding data structures are different

//name相同
array (size=1)
 &#39;wt&#39; => 
  array (size=5)
   &#39;name&#39; => 
    array (size=2)
     0 => string &#39;新建文本文档 (2).txt&#39; (length=26)
     1 => string &#39;新建文本文档.txt&#39; (length=22)
   &#39;type&#39; => 
    array (size=2)
     0 => string &#39;text/plain&#39; (length=10)
     1 => string &#39;text/plain&#39; (length=10)
   &#39;tmp_name&#39; => 
    array (size=2)
     0 => string &#39;C:\Windows\php1D64.tmp&#39; (length=22)
     1 => string &#39;C:\Windows\php1D65.tmp&#39; (length=22)
   &#39;error&#39; => 
    array (size=2)
     0 => int 0
     1 => int 0
   &#39;size&#39; => 
    array (size=2)
     0 => int 0
     1 => int 1820

//name不同(简单点)
array (size=2)
 &#39;wt&#39; => 
  array (size=5)
   &#39;name&#39; => string &#39;新建文本文档 (2).txt&#39; (length=26)
   &#39;type&#39; => string &#39;text/plain&#39; (length=10)
   &#39;tmp_name&#39; => string &#39;C:\Windows\php39C7.tmp&#39; (length=22)
   &#39;error&#39; => int 0
   &#39;size&#39; => int 0
 &#39;mmt&#39; => 
  array (size=5)
   &#39;name&#39; => string &#39;新建文本文档.txt&#39; (length=22)
   &#39;type&#39; => string &#39;text/plain&#39; (length=10)
   &#39;tmp_name&#39; => string &#39;C:\Windows\php39D8.tmp&#39; (length=22)
   &#39;error&#39; => int 0
   &#39;size&#39; => int 1820

Field Error purpose

Value: 1 The uploaded file exceeds the value limited by the upload_max_filesize option in php.ini.

Value: 2 The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.

Value: 3 The file was only partially uploaded.

Value: 4 No files were uploaded. Value: 5 The uploaded file size is 0.

The above is all the content of

PHP file upload, I hope it will be helpful to everyone! !

Related recommendations:

Perfect solution to PHP’s inability to upload large files

Example analysis of PHP single file and multiple file upload

A PHP file upload class sharing_php example

The above is the detailed content of PHP single file and multiple file upload examples_php examples. For more information, please follow other related articles on the PHP Chinese website!

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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

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 Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

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 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor