Skip to content

Instantly share code, notes, and snippets.

@ErMandeep
Created February 27, 2019 13:40
Show Gist options
  • Save ErMandeep/929e783ba9ab12598a5ffcb841d85999 to your computer and use it in GitHub Desktop.
Save ErMandeep/929e783ba9ab12598a5ffcb841d85999 to your computer and use it in GitHub Desktop.
upload file into folder simple method and show error if any
<?php
if(isset($_POST['submit'])){
echo "<pre>";
print_r($_FILES['file_upload']);
echo "</pre>";
$upload_error = array(
UPLOAD_ERR_OK => "There is no error",
UPLOAD_ERR_INI_SIZE =>"The uploaded file exceed the upload file size limit",
UPLOAD_ERR_FORM_SIZE =>"The uploaded file exceed the MAX_FILE_SIZE directively",
UPLOAD_ERR_PARTIAL =>"The uploaded file was only partialy uploaded. ",
UPLOAD_ERR_NO_FILE =>"No file was upload",
UPLOAD_ERR_NO_TMP_DIR =>"Missing a tempary folder.",
UPLOAD_ERR_CANT_WRITE =>"Failed to write file to disk",
UPLOAD_ERR_EXTENSION =>"A php extension stopped the file upload"
);
$temp_name = $_FILES['file_upload']['tmp_name'];
$the_file = $_FILES['file_upload']['name'];
$directory = "uploads";
if(move_uploaded_file($temp_name, $directory."/".$the_file)){
$the_message = "File upload Successfully.";
}else{
$the_error = $_FILES['file_upload']['error'];
$the_message = $upload_error[$the_error];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<h2>
<?php
if(!empty($upload_error)){
echo $the_message;
}
?>
</h2>
<input type="file" name="file_upload"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment