Avatar of the student
the student
 asked on

I am trying this from last 5 days please help me on this

I want to update logo in header using php, i have three different pages see below

----------------------------------------------- logo upload form --------------------------------------------
<?php ob_start();
// auth check
require_once('authorization.php');
$pageTitle = 'Change the Logo';
require_once ('header.php');


    ?>
    <main class="container">
    <form method="post" action="save-logo.php" enctype="multipart/form-data">
    <fieldset class="form-group">
        <label for="logo_image" class="col-sm-1">LOGO:</label>
        <input name="logo_image" id="logo_image" type="file" required/>
    </fieldset>

    <input name="logo_id" id="logo_id" type="hidden" />
    <button class="btn btn-success col-sm-offset-1">Save</button>
    </form>
    </main>

<?php
require_once('footer.php');
ob_flush(); ?>

Open in new window

------------------------------------------------------- save logo -------------------------------------------
<?php ob_start();

require_once ('authorization.php');
require_once ('header.php');
?>



<?php

try {
    $logo_image = null;
    $ok = true;

    if (!empty($_FILES['logo_image']['name'])) {
        $image_title = $_FILES['logo_image']['name'];
    if(empty($image_title)) {
        echo '<h2 class="alert-warning" style="text-align: center">Please select a file</h2>';

    }
        $array = end(explode('.', $image_title));

        $lower = strtolower($array);


        $file_type = ['png', 'gif', 'svg'];

        if (!in_array($lower, $file_type)) {
            echo '<h2 class="alert-warning" style="text-align: center">Logo must be only three types:<br /> .png or .gif or .svg<br/></h2>';
            $ok = false;
        }

        // size check
        $file_size = $_FILES['logo_image']['size'];
        if ($file_size > 5242880) {
            echo '<h2 class="alert-warning" style="text-align: center">Please select Logo less than 5 MB<br /></h2>>';
            $ok = false;
        }

        // rename to unique file name
        $logo_image = uniqid("") . "%$image_title";

        // copy to /covers folder
        $temporary_name = $_FILES['logo_image']['tmp_name'];
        move_uploaded_file($temporary_name, "logos/$logo_image");

    }
    if ($ok) {

        require_once('database-connect.php');


        if (empty($logo_id)) {
            $sql = "INSERT INTO update_logo (logo_image) VALUES (:logo_image);";
        } else {
            $sql = "UPDATE albums SET logo_image = :logo_image WHERE logo_id = :logo_id";
        }


        $cmd = $conn->prepare($sql);
        $cmd->bindParam(':logo_image', $logo_image, PDO::PARAM_STR, 255);


        if (!empty($logo_id)) {
            $cmd->bindParam(':logo_id', $logo_id, PDO::PARAM_INT);
        }

        $cmd->execute();

        $conn = null;


        header('location:console.php');
    }


    }
    catch (exception $e) {
    header('location:error.php');
}
require_once ('footer.php');
    ?>


<?php ob_flush(); ?>
}

Open in new window

---------------------------------------------------------Header(need to display logo here) -----------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="css/custom.css">
</head>
<body>
<?php
try {
$logo_id = null;
$logo_image=null;



if (!empty($_GET['logo_id'])) {
    if (is_numeric($_GET['logo_id'])) {

        $logo_id = $_GET['logo_id'];
        // connect
        require_once('database-connect.php');
        $sql = "SELECT logo_image FROM update_logo";
        $cmd = $conn->prepare($sql);
        $cmd->bindParam(':logo_id', $logo_id, PDO::PARAM_INT);
        $cmd->execute();
        $logo = $cmd->fetch();


        $logo_image = $logo['logo_image'];

        $conn = null;

    }

}
?>
<nav class="navbar navbar-default">
    <ul class="nav nav-pills">
        <li><a href="index.php"
               class="navbar-brand">
                <?php
                if (!empty($logo['logo_image'])) {
                    echo '<img src="logos/' . $logo['logo_image'] . '" class="thumb" />';
                }
                ?>
            </a>
        </li>


        <?php
        session_start();

        if (empty($_SESSION['user_id'])) {

            echo '<li><a href="sign-up.php">Sign Up</a></li>
                <li><a href="sign-in.php">Sign in</a></li>';
        } else {

            echo '<li><a href="information.php">Manage Accounts</a></li>
                  <li><a href="console.php">Console</a></li>
                  <li><a href="sign-out.php">Sign Out</a></li>';
        }
        ?>
    </ul>

    <?php
    if (!empty($_SESSION['user_id'])) {
        echo '<div class="navbar-text pull-right">' . $_SESSION['login_name'] . '</div>';
    }

    ?>
</nav>
<?php
}
catch (exception $e) {
    header('location:error.php');
}
require_once('footer.php');
ob_flush(); ?>

Open in new window

Web DevelopmentPHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ray Paseur

stale question
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy