0

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"

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Satellite Sage
  • 440
  • 1
  • 4
  • 13
  • It's undefined because it was never declared. Also, you can opt out with isset check for the session and just go ahead and call `session_start()`. If you want the value for `fileActualExt` to be accessed on a different page then add it on a session variable – magicianiam May 19 '18 at 13:27
  • you should save `$fileNameNew` in the table instead of using session to get the file extension. Because if you don't then how do you expect the system to fetch the session when I logout of the system? – magicianiam May 19 '18 at 13:29
  • @magicianiam, I have now added it on a session variable by $_SESSION['ext'] = $fileActualExt; I then placed $ext = $_SESSION['ext'] at the top of my home.php and replaced the img src with $ext instead of $fileActualExt. It's still undefined. To be exact, it now says, "Undefined index: ext" Btw, I still have my session_star(); at the top of the home.php. – Satellite Sage May 19 '18 at 13:43
  • I would still suggest you save `$fileNameNew ` on `profileimg` instead. As your code will not work if you don't re-upload an image. – magicianiam May 19 '18 at 13:48
  • @magicianiam, ohhhh. Okay, I get it now. Thaaank you so muuuch! :) – Satellite Sage May 19 '18 at 13:58

0 Answers0