uploading file in php
create a html form that contain the file upload input, Like the one one below.
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Select image to upload:<label><br>
<input type="file" name="fileName" id="">
<input type="submit" value="Upload File" name="upload">
</form>
</body>
</html>
note: add enctype="multipart/form-data" to the form attribute, to specify the content-type
Create upload.php file and add the PHP code below to it.
<?php
if(isset($_POST["upload"])) {
$image_dir = "image/"; //folder to store the file
$image_file = $image_dir . basename($_FILES["fileName"]["name"]); //directory of the file with the file name
if (($_FILES["fileName"]["size"] < 500000)&&($_FILES["fileName"]["size"] >0)) { //check if file is between 1kb and 50GB size
if (move_uploaded_file($_FILES["fileName"]["tmp_name"], $image _file)) { // store the file on the server
echo "file upload successful";
} else {
echo "there was an error uploading your file.";
}
} else {
echo "file size is too small/big";
}
}
?>
feel free to leave comment
Top comments (0)