nethken nethken - 3 months ago 11
PHP Question

How to get the file extension of file uploaded

How to get the file type of file uploaded? If it's

.ppt
,
.docx
,
.pdf
,
.png
,
.mp4
etc. Can someone help me about this? I'm starting to learn file uploading. Give me some clues please. Just a newbie to php.

here's my code.

<?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'];
$folder="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,size) VALUES('$final_file','$new_size')";
mysql_query($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
}
}
?>

Answer

Try this:

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
Comments