How To Upload File Using Javascript PHP

How To Upload File Using Javascript PHP

How To Upload File Using Javascript PHP

0 Sales

Free

 

Uploading files is a feature that is most used in the web application. PHP provides an easy way for the client to access the data. With PHP, by writing minimal code, you can upload files or images to the server. We will provide a PHP script in this tutorial to add the folder to the database directory. You can upload all types of files including images to the server in PHP using our PHP file upload script.

Upload Form HTML


 

An HTML form that allows users to select a file they want to upload must be created at first.

Make sure there are the following attributes in the <form> tag.

 

  • Method="post"

  • enctype="multipart/form-data"

 

 Make sure that the <input> tag contains the attribute type="file."

 

<form action="upload.php" method="post" enctype="multipart/form-data">

    Select File to Upload:

    <input type="file" name="file">

    <input type="submit" name="submit" value="Upload">

</form>


 

The form above will be sent to the upload.php file to upload the file to the server.

 

Upload File in PHP (upload.php)


 

PHP offers a move_uploaded_file()function that transfers a file uploaded to a new location. We can upload a file to PHP using the move_uploaded_file() function.

 

To upload the file in PHP, the following code is used. Specify the folder where the downloaded file will be located in the $targetDir variable. Often, describe the types of files you want to upload.


 

<form action="upload.php" method="post" enctype="multipart/form-data">

    Select File to Upload:

    <input type="file" name="file">

    <input type="submit" name="submit" value="Upload">

</form>

The form above will be sent to the upload.php file to upload the file to the server.

Upload File in PHP (upload.php)

PHP provides a function called move_uploaded_file() that moves an uploaded file to a new location. Using move_uploaded_file() function we can upload a file in PHP.

The following code is used to upload file in PHP. Specify the directory in $targetDir variable, where the uploaded file will be placed. Also, define the file types that you want to allow for uploading.

<?php

$statusMsg = '';


//file upload path

$targetDir = "uploads/";

$fileName = basename($_FILES["file"]["name"]);

$targetFilePath = $targetDir . $fileName;

$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);


if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])) {

    //allow certain file formats

    $allowTypes = array('jpg','png','jpeg','gif','pdf');

    if(in_array($fileType, $allowTypes)){

        //upload file to server

        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){

            $statusMsg = "The file ".$fileName. " has been uploaded.";

        }else{

            $statusMsg = "Sorry, there was an error uploading your file.";

        }

    }else{

        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';

    }

}else{

    $statusMsg = 'Please select a file to upload.';

}


//display status message

echo $statusMsg;

?>

LICENSE OF USE

You can use it for personal or commercial projects. You can't resell it partially or in this form.

PRODUCT INFO

Create Date : May 14, 2020

Updated Date : May 14, 2020

Ratings

Comments : 0

Downloads : 0