简体   繁体   中英

Show image on html page using parent directory without image name

Is there a way to show an image in html using only the parent directory of the image?

My server is storing images for user reports in folders. Each time a report is filed with an image, the server gets the unique id # of the report using php and creates an folder with that number as the title. It then puts the image in that folder.

For example: the 100th report filed will have its image saved at /var/www/website/uploads/100/image.jpg

For reading saved reports, I want to simply display the associated image on an html page given the report id # which is obtained using php. Something like this using html in a php file:

<img src="/var/www/website/uploads/<?php echo $id ?>/image.jpg">

The problem is that the name of 'image.jpg' is dynamic and unknown, so is there a way to show the image using only its parent directory?

You can change your image upload code to include the $id within the move_uploaded_file() function. Provided you have the $id available at upload, then it would look something like the following:

The following code assumes you have file-ext of your image stored as a variable in your upload file extension check.

$fileNameNew = "image.".$fileExt;
$fileDestination = $id.'/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);

This essentially renames the image to image.jpg or image.png . Being that there is only one image within that folder, you would then use the unique ID folder to show it no matter the unique ID, the image will always be image.png or jpg.

So when you want to show the image, just call the line you referenced...

<img src="/var/www/website/uploads/<?php echo $id ?>/image.jpg">

The following would be an example of a standard file upload code block.

if(isset($_POST['submit'])){
    $file = $_FILES['file'];
    
    //--> associative array of items uploaded via the HTTP POST method
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];

    //--> get the extension using pathinfo() 
    $fileExt = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));

    //--> array of allowed file-extensions
    $allowed = array('jpg', 'jpeg', 'png');

    // check if the file-extension is with the array of allowed extensions using in_array()
    if(in_array($fileExt, $allowed)){

        //--> Make sure there are no errors
        if($fileError === 0){
            //--> Set a file size
            if($fileSize < 1000000){
                //--> Here is where we define the images name as we want it to be
                //--> Since there is only one image within the folder there
                //--> Will be no issue with renaming it `image`
                $fileNameNew = "image.".$fileExt;
                //--> Provided you have the unique $id enter it here as the destination 
                //--> for your file or make the directory
                mkdir($id, 0755, true); //--> optional check if directory is made or exists?
                $fileDestination = $id.'/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                header("Location: index.php?uploadsuccess?id=$id");
            }else{
                echo "Your file is too large!";
            }

        }else{
            echo "There was an error uploading your file!";
        }
    }else{
        echo "You can not upload files of this type!".$fileExt;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM