Loading…

How can we upload a file in PHP?

For file upload in PHP make sure that the form uses method="post" and attribute: enctype="multipart/form-data". It specifies that which content-type to use when submitting the form.

Example

                                        

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

Suggest An Answer

No suggestions avaliable!

Latest post from PHP interview questions

Ask Question