- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP File Upload
You are free to upload files such as images, audios, videos, etc, either directly in your database or in your particular directory folder just by using PHP.
What to do Before File Upload in PHP
Before uploading any files using PHP either in database or in folder, you have to open php.ini file (php configuration file) and search for file_uploads and turn it as On like.
file_uploads = On
You can change the temporary directory where the file firstly uploded. From this temporary directory, you can copy or move the file in another folder using your PHP script.
In XAMPP, the default uploading file temporary directory is C:\xampp\tmp\. You can check by opening your php.ini file and then search for upload_tmp_dir. You will see this:
upload_tmp_dir="C:\xampp\tmp"
And you can also increase or decrease the maximum size of uploading file just by searching upload_max_filesize in your php.ini file. Here is the sample configuration related to upload file size you will see.
upload_max_filesize=10M
Here M indicates MB.
Create Your File Uploading Folder
Before going to upload any file using PHP, first create a folder say uploaded-file inside the directory C:\xampp\htdocs\.
After creating the folder named uploaded-file as said in the above line. You directory will looks like this:

As you can see, the directory contains the folder you have created named uploaded-file.
Now go to the example given below to understand all about how to upload file using PHP.
Upload File using PHP Example
To upload any file using PHP, first create a HTML form, asking to upload any file from the user as shown in the example given below:
<html> <head> <title>PHP File Uploader Form Example - codescracker</title> </head> <body> <h2>Upload Your File</h2> Select your file to upload:<br/> <form action="file-upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="myfile"><br/><br/> <input type="submit" value="Upload File"> </form> </body> </html>
Type the above file uploading form code in your text editor such as Notepad or Notepad++ or any other. You can also do copy and paste the above code in your text editor and save the above code inside the directory C:\xampp\htdocs\ with name codescracker.php.
After creating the file codescracker.php that contains file uploading form, your directory will looks like this, now contains the file codescracker.php.

Here is the sample output you will see when you type localhost/codescracker.php in your browser:

You have created a file uploading form, now you have to create a file uploading script using PHP that will be used to process the file and then upload the processed file inside the directory C:\xampp\htdocs\uploaded-file\ or in folder uploaded-file.
Here is the file uploading script using PHP.
<?php $filename = $_FILES["myfile"]["name"]; $filetype = $_FILES["myfile"]["type"]; $filesize = $_FILES["myfile"]["size"]; $tempfile = $_FILES["myfile"]["tmp_name"]; $filenameWithDirectory = "uploaded-file/".$filename; ?> <html> <head> <title>PHP File Uploading Script Example - codescracker</title> </head> <body> <?php if(move_uploaded_file($tempfile, $filenameWithDirectory)) { echo "<h2>File Uploaded</h2>"; echo "<p>You file is uploaded successfully.</p>"; echo "<p>File name = <b>$filename</b></p>"; echo "<p>File type = <b>$filetype</b></p>"; echo "<p>File size = <b>$filesize</b></p>"; } else { echo "Error occurred during file upload!"; } ?> </body> </html>
Type the above code in your text editor and save the above file with name file-upload.php inside the same directory.
After creating and saving the above file uploading script inside the same directory, your directory now looks like:

Now choose any file say image file as shown here.

After selecting an image file or any other file, click on the Upload File button. You will see the following output after performing this:

Here the file size is represented in bits value, if you double times divide with 1024 then you will get the value in MB.
Now, after performing all the above steps, when you will open the folder named uploaded-file, then you will see that the file codescracker.jpg will be there as you have upload the file codescracker.jpg using the above example.
Here is the screenshot of the folder uploaded-file after uploading the file:

« Previous Tutorial Next Tutorial »