如何通过从MySQL数据库动态获取同一页面上返回的路径来在php页面上呈现图像

时间:2016-09-19 20:11:45

标签: php html

所以我想让我的页面显示我从mysql数据库获取并在同一屏幕上显示的路径的图像。这是我的代码,我已经尝试了一切,请让我知道我哪里出错。

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“文件链接:”返回上传文件的完整路径。

如何在同一页面中的那条路径上渲染图像?

这两个图片代码都没有效果。提前谢谢!

编辑

文件链接:C:/Apache24/htdocs/demo_webpages_project/neweruploads/footer.jpg

这是我作为输出获得的路径。

基本上这是我从另一个php文件上传图像的文件夹

<?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'] . ")";
        }

    ?>

这有帮助吗?

编辑2

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

这是我的本地目录路径。

您提供的解决方案允许我读取直接指向demo_webpages_project文件夹中的图像,而不是读取neweruploads文件夹

1 个答案:

答案 0 :(得分:0)

如果您上传的文件存储在neweruploads子目录中,请替换此代码:

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

通过这个:

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