In: Computer Science
You are required to develop a utility for uploading a file in .rar, .zip and .pdf format using the reusable code of PHP from any online resources. To do this assignment you need to use the FILE element of html forms and on submission the next page should include the php code for completing the operation of file upload. You can use file directories, databases for maintaining your files at your web server along with your web application.
// Front side code
<html>
<head>
<title>File Form</title>
</head>
<body>
<form action="done.php" method="post" enctype="multipart/form-data">
<h3>Upload Any Format</h3>
<label for="fileSelect">Filename:</label>
<input type="file" name="file" id="fileSelect">
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
// done.php
<?php
if(isset($_FILES['file'])){
$errors= array();
$file_name = $_FILES['file']['name'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$extensions= array("rar","zip","pdf");
if(in_array($file_ext,$extensions)=== false)
{
$errors[]="extension not allowed, please choose a rar, zip or pdf file.";
}
if(empty($errors)==true)
{
move_uploaded_file($file_tmp,"files/".$file_name);
echo "Success";
}
else
{
print_r($errors);
}
}
?>