PHP File Upload
Mục lục bài viết
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:
PHP Online Test
Liked this article? Share it!