I've been wondering why is it that i got no error when i try to upload file with size more than 2mb?
I already read this How to limit file upload type file size in PHP? but still not working. (Note: i'm not working on client side validation here like what it did using javascript).
Here's my code:
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="title" class="form-control" placeholder="Document Title" required autofocus><br>
<textarea name="description" class="form-control" placeholder="Description" rows="10" cols="10"></textarea><br>
<input type="file" name="documento" required><br>
<button type="upload" name="upload" class="btn btn-default"><span class="glyphicon glyphicon-open"> </span> Upload</button>
</form>
Then
if (isset($_POST['upload'])) {
$ddd_t = htmlspecialchars($_POST['title']);
$ddd_d = htmlspecialchars($_POST['description']);
date_default_timezone_set('Asia/Manila');
$date_time = date('Y-m-d H:i:s') ;
$filename = strtolower($_FILES['documento']['name']);
$target = "../downloads/files/";
$rand = rand(1,10000);
$target = $target . $rand . "_" . $filename;
$maxsize = 2097152;
$mime_type = array(
'application/pdf',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'text/plain',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/msword'
);
if (empty($ddd_t)) {
$errorize1 = "Required Title!";
}
else{
if (empty($filename)) {
$errorize1 = "Required File!";
}
}
if (($_FILES['documento']['size'] > $maxsize) || ($_FILES['documento']['size'] == 0)){
$errorize = "Max size is 2mb";
}
else{
if (!in_array($_FILES['documento']['type'], $mime_type)){
$errorize = "Invalid File. Only powerpoint, excel, pdf, word, plain-txt accepted!";
}
}
if (isset($errorize1) || isset($errorize))
{
}
else{
$_FILES['documento']['name'] = $rand . "_" . $filename;
$upload_file = ($_FILES['documento']['name']);
$query = "INSERT INTO files(docu_title, description, link, date) VALUES(:ddd_t, :ddd_d, :document, :date_time)";
$data = $conn->prepare($query);
$result = $data->execute(array(':ddd_t' => $ddd_t, ':ddd_d' => $ddd_d, ':document' => $upload_file, ':date_time' => $date_time));
$move = move_uploaded_file($_FILES['documento']['tmp_name'], $target);
if ($result && $move) {
$upload_img = header("location:?success=true&file=".$_FILES['documento']['name']);
}
else{
echo "error!";
}
}
}
And
<?php
if (isset($errorize)) {
echo '<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>'. $errorize .'</div>';
}
if (isset($errorize1)) {
echo '<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>'. $errorize1 .'</div>';
}
?>
Only the invalid mime type here is working.. I don't know what's wrong with the file size error when i try to upload more than 2mb.
$upload_img = header("location:?success=true&file=".$_FILES['document']['name']);should probably be$upload_img = header("location:?success=true&file=".$_FILES['documento']['name']);yet it won't fix your problem, just pointing something out..htaccessif you can't modify yourphp.inifile. That's usually the fix. @user3258603