图像从文件路径中删除

我有一个仪表板,用户可以在其中登录并上传自己的个人资料图片,并将其保存到他们的个人资料中。这会将图像移动到正确的文件夹,并将其正确插入到数据库中。


直到最近我注意到图像消失时,它一直运行良好。在检查控制台中,我注意到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有没有人遇到过图像在通过任何帮助或指导移入文件夹后实际上从文件夹中消失的情况,我们将不胜感激。


偶然的你
浏览 86回答 1
1回答

呼如林

解决图片上传条件,不覆盖已有图片文件:<?phprequire 'db.php';session_start();$uploadDir = '../profiles/';// if edit-picture has been clicked on, run this if statementif (isset($_POST[ 'edit-picture' ])) {&nbsp; &nbsp; $studentID = $_SESSION[ 'studentID' ];&nbsp; &nbsp; // Creating 4 different variables using the $_FILES global variable to get data about the image that&nbsp; &nbsp; // you can view data about a file by doing: print_r($image);&nbsp; &nbsp; $fileName = $_FILES[ 'image' ][ 'name' ];&nbsp; &nbsp; $tmpName = $_FILES[ 'image' ][ 'tmp_name' ];&nbsp; &nbsp; $fileSize = $_FILES[ 'image' ][ 'size' ];&nbsp; &nbsp; $fileType = $_FILES[ 'image' ][ 'type' ];&nbsp; &nbsp; $filePath = $uploadDir . $fileName;&nbsp; &nbsp; // The below doesn't work as it assigns different value to folder and in db for image name&nbsp; &nbsp; // $filePath = md5($file_name . microtime()) . substr($fileName , -5, 5);&nbsp; &nbsp; if (file_exists($filePath)) {&nbsp; &nbsp; &nbsp; &nbsp; header("Location: ../profile.php?img=errorFileNameExists");&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; } // Checking file size - working&nbsp; &nbsp; else if ($_FILES[ "image" ][ "size" ] > 5000000) {&nbsp; &nbsp; &nbsp; &nbsp; header("Location: ../profile.php?img=errorFileSizeError");&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }&nbsp; &nbsp; $info = getimagesize($tmpName);&nbsp; &nbsp; // empty $info - not known image&nbsp; &nbsp; if (empty($info)) {&nbsp; &nbsp; &nbsp; &nbsp; header("Location: ../profile.php?img=errorFileTypeError");&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; } // This is to show any errors that may occur if the connection fails, this helps with error checking.&nbsp; &nbsp; $result = move_uploaded_file($tmpName, $filePath);&nbsp; &nbsp; if (!$result) {&nbsp; &nbsp; &nbsp; &nbsp; header("Location: ../profile.php?img=errorFileRelocate");&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp; else if (mysqli_connect_errno()) {&nbsp; &nbsp; &nbsp; &nbsp; printf("Connect failed: %s\n", mysqli_connect_error());&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $stmt = $conn->prepare("INSERT INTO `profileImage` (`imagePath`, `studentID`)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VALUES ( ?, ?) ON DUPLICATE KEY UPDATE `imagePath` = VALUES (`imagePath`) ");&nbsp; &nbsp; &nbsp; &nbsp; $stmt->bind_param("si", $fileName, $studentID);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute() or die("Failed to insert image into the database");&nbsp; &nbsp; &nbsp; &nbsp; header("Location: ../profile.php?img=successImageUploaded");&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP