Need to use a variable (which is equal to a function) that is from another php file

105 Views Asked by At

I'm trying to make a website that allows users to add a profile picture. Hence, I have this upload.php:

<?php

if (!isset($_SESSION)) {
    session_start();}

include_once 'includes/dbh.inc.php';
$id = $_SESSION['key'];

if (isset($_POST['submitFile'])) {
    $file = $_FILES['file'];

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

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

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

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError == 0) {
            if ($fileSize < 1000000) {
                $fileNameNew = "profile".$id.".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                $sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
                $resultImg = mysqli_query($conn, $sql);
                header("Location: home.php?upload=success");
            }
            else { echo "Sorry, your file size is too big.";}
        }
        else { echo "Oops, an error occurred!";}
    }
    else { echo "Please upload png and jpg files only.";}

}

I need to use the $fileActualExt in my home.php. Here's the code:

    $sqlImg = "SELECT * FROM profileimg WHERE userid='$id'";
    $resultImg = mysqli_query($conn, $sqlImg);
    $rowImg = mysqli_fetch_assoc($resultImg);

    if ($rowImg['status'] == 0) {
        echo "<img src='uploads/profile".$id.".$fileActualExt'".mt_rand()." height='200' width='200'>";}
    else{
        echo "<img src='uploads/defaultprofilepicture.jpg' height='200' width='200'>";
    }
    echo $username;

I need the file extension which is in the upload.php to be inserted the the img src which is in my home.php.

At the top of of my home.php, I have session_start() and include 'upload.php'

When I load my website, it shows the profile picture, and I thought it wouldn't. There's an error that says "Undefined variable: fileActualExt"

0

There are 0 best solutions below