【问题标题】:How to render an image on a php page by dynamically taking the path returned on the same page from MySQL database如何通过从 MySQL 数据库动态获取同一页面上返回的路径来在 php 页面上呈现图像
【发布时间】:2016-09-19 20:11:45
【问题描述】:

所以我希望我的页面显示我从 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 "File Link:" 返回上传文件的完整路径。

如何在同一页面中呈现该路径的图像?

两个图像标签都不起作用。提前致谢!

编辑

文件链接: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 文件夹

【问题讨论】:

  • 试试这个echo "&lt;img src='".$new."' /&gt;";
  • whole path?如/home/sites/example.com/html/uploads/kittens.jpg?那永远行不通。您必须输出 URL,而不是文件系统路径。你需要&lt;img src="/uploads/kittens.jpg"&gt;
  • @JoseManuelAbarcaRodríguez 我已添加编辑,请检查。
  • @MarcB 我明白了!有什么办法可以提取网址?

标签: php html


【解决方案1】:

如果您上传的文件存储在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.
                         ▲

【讨论】:

  • @siddparkar,你也可以试试echo '&lt;img src = \"/neweruploads/$new\"/&gt;';(在“neweruploads”前加一个斜线)。
  • 遗憾的是,这也不起作用。它只从根目录中获取图像文件。
  • 即使这样也不会带我进入 neweruploads 页面 echo '';
  • @siddparkar,图片正在上传,您只需要找到存储图片的目录的正确URL路径即可。能不能写下那个目录的localhost URL,比如http://localhost/neweruploads
  • 您好,我使用了 .$_SERVER['SERVER_NAME'] 并且它现在可以工作了,因为 localhost 路径设置正确。但是现在我在友好的 URL 方面遇到了更大的麻烦。为相同创建另一个线程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 2023-02-05
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多