为了账号安全,请及时绑定邮箱和手机立即绑定

图像从文件路径中删除

图像从文件路径中删除

PHP
偶然的你 2022-12-11 09:22:39
我有一个仪表板,用户可以在其中登录并上传自己的个人资料图片,并将其保存到他们的个人资料中。这会将图像移动到正确的文件夹,并将其正确插入到数据库中。直到最近我注意到图像消失时,它一直运行良好。在检查控制台中,我注意到404 not found图像出现错误,所以我检查了文件路径,图像不再存在(因此出现 404)。用户删除图像根本没有脚本,只能上传。配置文件.php:<p><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='profiles/".$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"/></p></form>edit-image.php 的脚本<?php    require 'db.php';    session_start();    $uploadDir = '../profiles/';    // if edit-picture has been clicked on, run this if statement    if (isset($_POST['edit-picture'])) {        $studentID = $_SESSION['studentID'];        // Creating 4 different variables using the $_FILES global variable to get data about the image that        // you can view data about a file by doing: print_r($image);                 $fileName = $_FILES['image']['name'];        $tmpName = $_FILES['image']['tmp_name'];        $fileSize = $_FILES['image']['size'];        $fileType = $_FILES['image']['type'];        $filePath = $uploadDir.$fileName;        // The below doesn't work as it assigns different value to folder and in db for image name        // $filePath = md5($file_name . microtime()) . substr($fileName , -5, 5);        $result = move_uploaded_file($tmpName, $filePath);        if (!$result) {            header("Location: ../profile.php?img=errorFileRelocate");            exit;        }        // Checking file size - working        else if ($_FILES["image"]["size"] > 5000000) {            header("Location: ../profile.php?img=errorFileSizeError");            exit();        }move_uploaded_file有没有人遇到过图像在通过任何帮助或指导移入文件夹后实际上从文件夹中消失的情况,我们将不胜感激。
查看完整描述

1 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

解决图片上传条件,不覆盖已有图片文件:


<?php

require 'db.php';

session_start();

$uploadDir = '../profiles/';


// if edit-picture has been clicked on, run this if statement

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



    $studentID = $_SESSION[ 'studentID' ];


    // Creating 4 different variables using the $_FILES global variable to get data about the image that

    // you can view data about a file by doing: print_r($image);

    $fileName = $_FILES[ 'image' ][ 'name' ];

    $tmpName = $_FILES[ 'image' ][ 'tmp_name' ];

    $fileSize = $_FILES[ 'image' ][ 'size' ];

    $fileType = $_FILES[ 'image' ][ 'type' ];


    $filePath = $uploadDir . $fileName;

    // The below doesn't work as it assigns different value to folder and in db for image name

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


    if (file_exists($filePath)) {

        header("Location: ../profile.php?img=errorFileNameExists");

        exit();


    } // Checking file size - working

    else if ($_FILES[ "image" ][ "size" ] > 5000000) {

        header("Location: ../profile.php?img=errorFileSizeError");

        exit();

    }

    $info = getimagesize($tmpName);

    // empty $info - not known image

    if (empty($info)) {

        header("Location: ../profile.php?img=errorFileTypeError");

        exit();

    } // This is to show any errors that may occur if the connection fails, this helps with error checking.


    $result = move_uploaded_file($tmpName, $filePath);


    if (!$result) {

        header("Location: ../profile.php?img=errorFileRelocate");

        exit;

    }

    else if (mysqli_connect_errno()) {

        printf("Connect failed: %s\n", mysqli_connect_error());

        exit();

    } else {

        $stmt = $conn->prepare("INSERT INTO `profileImage` (`imagePath`, `studentID`)

            VALUES ( ?, ?) ON DUPLICATE KEY UPDATE `imagePath` = VALUES (`imagePath`) ");

        $stmt->bind_param("si", $fileName, $studentID);

        $stmt->execute() or die("Failed to insert image into the database");


        header("Location: ../profile.php?img=successImageUploaded");

        exit();

    }

}

?>


查看完整回答
反对 回复 2022-12-11
  • 1 回答
  • 0 关注
  • 85 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信