2

Lets start from the begining. I am a student and have recently had a course in webdevelopement with databases (Im sure the course has different name in english) and I am creating a dynamic website. You can add, edit and remove sites/articles.

I wanted to be able to upload you own logo on your navbar neside your "tabs" but I have come to a dead end. I followed a guy on youtube that explained how to upload files to my server into a specific folder and limiting it to be only certain file types and a specific filesize.

But now i need to retrieve the filepath of that image so i can display it as a logo on my navbar on the website.

The way i started thinking was that i need to somehow get the latest modified file and then somehow get its location/filepath and then save it to a variable.

The current code i have for uploading an image is this: its in the root folder and called "upload.php"

<?php
if (isset($_POST['upload'])) {
$file = $_FILES['file'];

/* $_FILES gives you an array of info of an file */
/* below i give each variable some info from my file */

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

 /* Ext = extension.*/
 /* i only want .jpg and. png files on my site */
 /* Here i check if it has .jpg or png at the end of the file name */
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));

/* Creating an array with accepted file endings */
$allowed = array('jpg', 'jpeg', 'png');

if (in_array($fileActualExt, $allowed)) {
    if ($fileError === 0) {
        if ($fileSize < 1000000) {

            /* newimages get uniq names inside database */
            /* in this case it uses milliseconds */
            $fileNameNew = uniqid('', true).".".$fileActualExt;

            /* set file destination */

            $fileDestination = 'images/'.$fileName;

            move_uploaded_file($fileTmpName, $fileDestination);
            header('Location: index.php?uploadsuccess');

        }else {
            echo "Your file was to big! Make sure it's less than 1MB!";
        }
    }else {
        echo "There was an error uploading your file! Please try again!";
    }
}else {
    echo "You cannot Upload files of this type!";
}
}

And then i need to put the filepath into a variable and then add it to:

<img src="images/file_name.jpg>" class="navbar-logo" alt="">

Then replace the file_name.jpg with my variable.

I dont understand how i can achieve this. I dont have the knowledge for it and i hope that turning to stackoverflow i can get some help and learn something new on the way.

I have searched and tried out this code: (written inside of the "upload.php" file at the bottom, outside of the "if" statement.

/* get latest image name */ 
$path = "images"; 

$latest_ctime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}

Maybe i can't access the variable from the file im trying to get it from? As mentioned, this file ("upload.php") is inside the root folder. Im trying to use the variable $latest_filename in the following place: root/views/master.php

I dont know what more to add, i tried making this as transparent as possible.

Nidllah
  • 21
  • 3
  • you can try to save `$fileDestination` into the session, or access the folder where you store images and retrieve the latest image by looking at their timestamps – Wreigh Mar 21 '18 at 01:08

0 Answers0