图像配置文件无法正确显示-PHP MySQL

问题描述 投票:1回答:1

我在正确显示我的用户个人资料图像时遇到问题。我下面的脚本确实将图像移动到正确的文件夹中,并且也正确插入了数据库。在用户个人资料上显示该问题时会出现问题。

[我认为这与我的文件夹结构有关,但是当我从PHP脚本的文件路径中的../之前的$uploadDir中删除profiles时,没有任何效果。任何指导将不胜感激。当我检查图像标签上的元素时,您可以看到../也被回显,这意味着它找不到图像,因为它应该仅为profiles/

我的文件夹结构:

>profiles
    >image1.jpg
    >image2.jpg
>scripts
    >edit-picture.php
>profile.php
>index.php

profile.php:

<b>Profile Picture: </b>
    <?php 
        $picture = $row['imagePath'];
        if (empty($picture)){
            echo "<img src='profiles/no-image.png' width='100' height='100' >";
        } else {
            echo "<img src='".$row['imagePath']."' width='100' height='100' >";    
        };
    ?>
<form action="scripts/edit-picture.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="edit-picture" value="Upload"/>

scripts / edit-picture.php:

<?php
    require 'db.php';
    $uploadDir = '../profiles/';

    if (isset($_POST['edit-picture'])) {

        session_start();
        $studentID = $_SESSION['studentID'];

        $fileName = $_FILES['image']['name'];
        $tmpName = $_FILES['image']['tmp_name'];
        $fileSize = $_FILES['image']['size'];
        $fileType = $_FILES['image']['type'];

        $filePath = $uploadDir.$fileName;

        $result = move_uploaded_file($tmpName, $filePath);
        if (!$result) {
            echo "Error uploading file";
            exit;
        }
        else{
            // If image uploads ok, return to this page
            header("Location: ../profile.php?imageuploaded");
        }
        if(mysqli_connect_errno()){
              printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
        if(!get_magic_quotes_gpc()){
            $fileName = addslashes($fileName);
               $filePath = addslashes($filePath);
        } 
        $stmt = $conn->prepare ("INSERT INTO `profileImage` (`imageID`, `imagePath`, `studentID`) VALUES (NULL, ?, ?) ");
        $stmt->bind_param("si", $filePath, $studentID); 
        $stmt->execute() or die("Failed to insert image into the database");
    }
?>

这是表中的数据(第二行,您可以在文件夹和图像名称之前的../中查看错误的位置。db

php mysql image prepared-statement
1个回答
1
投票

将目录地址保存在数据库中不是一个好习惯。对于图像,您只需要保存一个名称,例如abc.jpg,而不是'directory / abc.jpg'。背后的逻辑很明确,您可能需要链接到网站中不同页面上的图像。如果将目录添加到数据库中,将很难做到,尽管链接到图像并显示它们不是很难的。因此,请尝试从名称中消除“ ../profiles/”。然后,无论何时显示图像,都可以轻松地从网站的任何位置链接它。因此,只需省略以下代码段:

    $uploadDir = '../profiles/';

并如下更改您的$ filepath

$filePath = md5($file_name . microtime()) . substr($fileName , -5, 5);

这有两个好处。文件名将永远不会被覆盖,然后,出于简单的安全保护,将无法通过文件名来识别学生。

然后,当您想要链接到edit-picture.php中的图像时,您将编写

"<img src='../profiles/".$row['imagePath']."' width='100' height='100' >"

并且当您想从profile.php链接到图像时,您将编写:

"<img src='profiles/".$row['imagePath']."' width='100' height='100' >" 

顺便说一句,在您的php代码中,您正在使用get_magic_quotes_gpc。首先,这不是一个好习惯,然后尝试使您的php版本始终保持更新。php官方网站,关于get_magic_quotes_gpc说:

警告从PHP 7.4.0开始,此功能已被弃用。不鼓励使用此功能。

尝试使用其他php安全功能。祝你好运。

© www.soinside.com 2019 - 2024. All rights reserved.