php – Upload image file-ThrowExceptions

Exception or error:
<?php
include('security.php');

if(isset($_POST['save_albums'])) {
    $name = $_POST['album_name'];
    $file  = $_FILES['album_image'];

    $fileName = $_FILES['album_image']['name'];
    $fileTmpName = $_FILES['album_image']['tmp_name'];
    $fileSize = $_FILES['album_image']['size'];
    $fileError = $_FILES['album_image']['error'];
    $fileType = $_FILES['album_image']['type'];


    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png');

    //Check if the extension is allowed to be uploaded
    if(in_array($fileActualExt, $allowed)) {
        //Check if there is an error
        if($fileError === 0) {
            //Check file is too big
            if($fileSize < 1000000) {
                //make sure when upload file get proper name EX: someone upload a file with 
                //name test.jpeg And another file is uploaded with the same name inside the
                //upload folder it will overwrite so to prevent thsi we do:
                $fileNameNew = uniqid('',true).".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                $query = "INSERT INTO albums ('name','images') VALUES ('$name','$fileNameNew')";
                $query_run = mysqli_query($connection,$query);
                $_SESSION['success'] = "Your File Is Uploaded Successfuly!!!";
                header('Location: albums.php');
            } else {
                $_SESSION['status'] = "Your File Is Too Big!!!";
                header('Location: albums.php');
            }
        } else {
            $_SESSION['status'] = "There Was An Error Uploading You File!!!";
            header('Location: albums.php');
        }
    } else {
        $_SESSION['status'] = "You Cannot Upload Files Of This Types!!!";
        header('Location: albums.php');
    }
} 
?>


<?php
    include('security.php');
    include('includes/header.php');
    include('includes/navbar.php');
?>

html

<!-- modal medium -->
<div class="modal fade" id="albumsModal" tabindex="-1" role="dialog" aria-labelledby="mediumModalLabel"
    aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="mediumModalLabel">Albums</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="login-logo">
                    <a href="#">
                        <img src="images/icon/logo.png" alt="CoolAdmin">
                    </a>
                </div>
                <div class="login-form">
                    <form action="albums_code.php" method="post" enctype="multipart/form-data">
                        <div class="form-group">
                            <label>Name</label>
                            <input class="au-input au-input--full" type="text" name="album_name" placeholder="Enter Name" required>
                        </div>
                        <div class="form-group">
                            <label>Upload Image</label>
                            <input class="au-input au-input--full" type="file" name="album_image" id="album_image"
                             height="38px" required multiple="">
                        </div>
                        <button class="au-btn au-btn--block au-btn--green m-b-20" name="save_albums"
                            type="submit">Save</button>
                        <button type="button" class="au-btn au-btn--block au-btn--red m-b-20"
                            data-dismiss="modal">Cancel</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>




<!-- MAIN CONTENT-->
<div class="main-content">
    <div class="section__content section__content--p30">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">
                            <i class="mr-2 fa fa-align-justify"></i>
                            <strong class="card-title" v-if="headerText">Albums</strong>
                        </div>
                        <div class="card-body">
                            <button type="button" class="btn btn-secondary mb-1" data-toggle="modal"
                                data-target="#albumsModal">
                                ADD
                            </button>
                            <?php
                                if(isset($_SESSION['success']) && $_SESSION['success'] != "")
                                {
                                    echo '<h4 class = "alert alert-success text-center">' .$_SESSION['success'] .'</h4>';
                                    unset($_SESSION['success']);
                                }

                                if(isset($_SESSION['status']) && $_SESSION['status'] != "")
                                {
                                    echo '<h4 class = "alert alert-danger text-center ">' .$_SESSION['status'] .'</h4>';
                                    unset($_SESSION['status']);
                                }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
            <!-- end modal medium -->
            <div class="row">
                <div class="col-lg-12">
                    <div class="table-responsive table--no-card m-b-30">

                        <!-- Retreive Data From Database And Display In Table -->
                        <table class="table table-borderless table-striped table-earning">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Name</th>
                                    <th>Designation</th>
                                    <th>Image</th>
                                    <th>Edit</th>
                                    <th>Delete</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>
                                        <button type="submit" class="btn btn-success"><i
                                                class="fas fa-edit pr-2"></i>Edit</button>
                                    </td>
                                    <td>
                                        <button type="submit" class="btn btn-danger"><i
                                                class="fas fa-trash-alt pr-2"></i>Delete</button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
    include('includes/scripts.php');
    include('includes/footer.php');
?>

i am trying to insert the query in my code to upload the image to database the code is running fine the image is moving to the directory uploads but not to the database any suggestion and the php doesnt report any error

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *