Here In this tutorial “Simple file upload in php to Server” we are going to learn how to upload any files on remote server using Simple HTML form and PHP. We can upload any type of file like doc, image, videos, sql and etc.
Crating a HTML form to upload the file
Here we will create a simple HTML form which can be used to upload files.
index.php
<html> <head> <title>Techjunkgigs</title> </head> <body> <form method="post" action="upload.php" enctype="multipart/form-data" > Browse File: <input type="file" name="file" id="file" > <input type="submit" id="submit" name="submit" value="Upload the File"> </form> </body> </html>
Process to upload the file
Here’s the complete code of our “upload.php” file. From here It will store the uploaded file in a created folder on permanent basis. Through this code we restrict user for different uploading files. Here user can only upload particular type of file and its size which is define by the developer. To ensure that users upload the correct file type and within the allowed limit.
Upload.php
<?php if(isset($_POST['submit'])){ $file = $_FILES['file']; $Name = $_FILES['file']['name']; $TempName = $_FILES['file']['tmp_name']; $Size = $_FILES['file']['size']; $Error = $_FILES['file']['error']; $type = $_FILES['file']['type']; $ext = explode('.',$Name); $actualext= strtolower(end($ext)); $allowed= array('jpg','jpeg','png','pdf','doc','xls','mp4','mp3','ppt','rar','sql','zip'); if(in_array($actualext,$allowed)) { if ($Error === 0) { if($Size < 1000000) { $newname= uniqid('',true).".".$actualext; $fileDestination= 'uploads/'.$newname; move_uploaded_file($TempName,$fileDestination); echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('File Uploaded Successfully Successfully') window.location.href='index.php?AddedSuccessfully'; </SCRIPT>"); }else { echo "Your File size is too big!"; } } else { echo "Error in uploading the file"; } }else { echo "Please Check The file type"; } } ?>
Explanation of Code
When the user clicks to the upload button then the form gets submitted. the information about the uploaded file can be accessed through PHP super global array which is called $_FILES.
Like here, our upload form contains a file select field called file (i.e. name=”file”), if any user uploads a file using this field, we can easily obtains its every details like its name, size, type, temporary name or if any error occurred while attempting the upload through the $_FILES[“file “] associative array, like this:
- $_FILES[“file”][“name”] — This array value defines the original name of the file, including its file extension.
- $_FILES[“file “][“type”] — This array value defines the MIME type of the file.
- $_FILES[“file “][“size”] — This array value defines the file size.
- $_FILES[“file “][“tmp_name”] — This array value defines the temporary file name including full path which is assigned to the every file once it has been uploaded to server.
- $_FILES[“file “][“error”] — This array value defines error code associated with the file upload.
The PHP code in the following example simply displays the details of the uploaded file and stores it in a temporary directory on the server.
I hope this tutorial helped you to learn How to upload file to server using php . To get the latest news and updates follow us on twitter & facebook, subscribe to our YouTube channel. And If you have any query then please let us know by using comment form.
Leave a Reply