W. Jackson W. Jackson - 6 months ago 41
PHP Question

How to validate an upload form by allowing one extension file only and avoiding duplicate entry in PHP?

My code work's fine but i want it to allow xls or xlsx only to be uploaded. Can you please help me on the validation in my php? The

":type" => $_FILES['upload-file']['tmp_name']
in my code on the backend.php is working also that is the file extension of the file, but i don't have any idea on how to use it in the validation.You can look to my sample array below:

My sample array

Array (
[upload-file] => Array
[name] => Jellyfish.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php222D.tmp
[error] => 0
[size] => 775702


My code in the backend.php
case 'upload-file':
$folder = time();

$file = "path".DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$_FILES['upload-file']['name'];

move_uploaded_file($_FILES['upload-file']['tmp_name'], $file);

$arr = [
":userid" => $_SESSION['loggedIn_PH'][0]['user_id'],
":filename" => $_FILES['upload-file']['name'],
":filelink" => $_FILES['upload-file']['tmp_name'],
":type" => $_FILES['upload-file']['tmp_name']

$query = "INSERT INTO file_rec_tbl ( `file_name`, `file_datetime`,`file_link`, `user_id` )
VALUES (:filename, '".date('Y-m-d H:i:s')."',:filelink,:userid)";

$stmt = $con -> prepare( $query );
$stmt -> execute( $arr );

// exit(print_r($stmt, true));


$allowed =  array('gif','png' ,'jpg');
$filename = $_FILES['upload-file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
    echo 'error';

try this code...