0

So i want my page to show the image whose path I am getting from the mysql database and displaying on the same screen. This is my code, I have tried everything, please let me know where I'm going wrong.

while ($row = mysqli_fetch_array($return_data)) {

        echo "ID:".$row['demo_id']."<br>";
        echo "Name: ".$row['demo_name']."<br>";
        echo "Version: ".$row['demo_version']."<br>";
        echo "Details: ".$row['demo_details']."<br>";
        echo "File Link: ".$row['file']."<br>";
        $new = $row['file'];
        echo '<img src = \"$new\"/>';
    }

    mysqli_free_result($return_data);
    echo "Data retrieved successfully!"."<br>";
    ?>

    <img src = "<?php echo $new?>">

echo "File Link: " returns me the whole path of the uploaded file.

How do I render the image at that path in the same page?

neither of the image tags are working. Thanks in advance!

edit

File Link: C:/Apache24/htdocs/demo_webpages_project/neweruploads/footer.jpg

this is the path I get as an output.

Basically this is the folder where I have uploaded the image from another php file

<?php

        //this module is used to temporarily store the uploaded file to the server
        $target_dir = "random/"; //we randomly assign a value to the upload target directory
        $target_file = $target_dir . basename($_FILES["image_file"]["name"]); /*here ["name"] is the original name of the file before it was updated 
                                                                              target file is assigned this name by appending it to the $targer_dir
                                                                              now target_file is the uploaded file name along with the path*/
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);/*this returns various kind of info about the path for eg Directory, Basename
                                                                   in our case it returns extension of the file*/


        //this sub module is to check whether the file is really an image file
        if(isset($_POST["submit"])) { //isset is used to confirm whether the value has actually being submitted 
            $check = getimagesize($_FILES["image_file"]["tmp_name"]);/*here ["tmp_name"] is the location of temporary file on server
                                                                    getimagesize confirms image format by returning dimensions etc*/
            if($check !== false) {
                echo "A file is of an image format<br>";
            }
            else {
                echo "The file is not an image!<br>";
            }
        }


        //Test module to upload files to a destination directory and check whether they have been uploaded or not
        if (is_uploaded_file($_FILES['image_file']['tmp_name']) && $_FILES['image_file']['error']==0) { /*code to check two things: 1. whether the file exists in the                                                                                                   temp memory 2. whether the file has any error*/
            $path = 'C:\Apache24\htdocs\demo_webpages_project\neweruploads\\' . $_FILES['image_file']['name']; /*this sets the destination directory(along with expected file                                                                                               name)*/
            if (!file_exists($path)) {  //if the file does not exists at that path
                if (move_uploaded_file($_FILES['image_file']['tmp_name'], $path)) { //use the php file move function to move it
                    echo "The file was uploaded successfully."; //success
                } 
                else {
                    echo "The file was not uploaded successfully."; //failure
                }
            } 
            else {
                echo "File already exists. Please upload another file."; //detects existence of file with exact same name
            }
        } 
        else {
            echo "The file was not uploaded successfully."; //if any problem with original uploading
            echo "(Error Code:" . $_FILES['image_file']['error'] . ")";
        }

    ?>

Does this help?

edit 2

http://localhost:8080/demo_webpages_project/download.php?project_name=footer&version=&submit=Search

this is my local directory path.

the solution you provided is allowing me to read images which are in the demo_webpages_project folder pointing directly there), not to neweruploads folder

hal9000
  • 201
  • 5
  • 25

1 Answers1

0

If your uploaded files are stored in the neweruploads subdirectory, then replace this code:

$new = $row['file'];
echo '<img src = \"$new\"/>';

By this one :

$new = basename( $row['file'] ); // GET FILE NAME ONLY, GET RID OF PATH.
echo '<img src = \"neweruploads/$new\"/>'; // FILENAME WITH UPLOAD PATH.
                         ▲