亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

圖像從文件路徑中刪除

圖像從文件路徑中刪除

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 關注
  • 113 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號