Reputation: 740
My below code is all about uploading file and viewing it and downloading it. I am able to upload the files. I am storing the file name, file size, file type to database. So my problem is when I try to upload .jmx file and .zip files, It will be uploaded to the required directory but the thing is it does not store the file details to the database. Can anyone help me out in this regards. Thanks in advance.
upload.php
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="C:\wamp64\www\uploads/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
//$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysqli_query($GLOBALS['db'],$sql);
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>
Upvotes: 1
Views: 30
Reputation: 740
I got to know that there is a fault in using $_FILES['file']['type'] to find to file extension.
The below code helped me in finding the file extension. And My question is answered now.
$file_type = pathinfo($file, PATHINFO_EXTENSION);
Upvotes: 1