簡體   English   中英

Php.$_FILE 不起作用。我找不到解決方案

[英]Php.$_FILE is not working .I cant find a solution

這是一個 php 代碼。它不起作用。存儲在圖像的數據庫名稱中,而不是圖像。 我搜索了這個問題,但我找不到解決方案。我試過 file_get_contents() 但沒有用。我不知道是服務器或代碼的問題。

<?php
    $msg="";
    if(isset($_POST['submit'])){
        session_start();

        $target_dir="uploads/";
        $target_file=$target_dir . basename($_FILES["fileToUpload"]["name"]);

        include 'dbh.php';

        $image=$_FILES['fileToUpload']['name'];
        $image_tmp=$_FILES['fileToUpload']['tmp_name'];
        $id=$_SESSION['id'];
        $sql="UPDATE user SET image='$image' WHERE id='$id'";
        mysqli_query($conn,$sql) or Die("ERROR:" .mysqli_error($conn));

        if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
            $msg="Image uploaded successfully";
        }
        else{
            $msg="There was a problem uploading image";
        }

    }

    ?>




<form action="user_photo.php" method="post" enctype="multipart/form-data" target="iframe">
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" name="submit" value="Ngarko Foto">
      </form>

file_get_contents應該可以工作。 但是圖像數據是二進制的,所以你不能把它代入到 SQL 中。 您應該使用准備好的語句。

$image = file_get_contents($_FILES['fileToUpload']['tmp_name']);
$sql = "UPDATE user SET image = ? WHERE id = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "bi", $image, $_SESSION['id']);
mysqli_stmt_execute($stmt) or die(mysqli_error("ERROR:" .$conn));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM