dra87370 2016-06-02 07:46 采纳率: 100%
浏览 48
已采纳

PHP在我的根文件夹中的数据库和文件夹中上传

So I'm trying to upload the file name of the image in the database and the image itself will be save in the folder inside a root folder.

In my codes, the image name is able to be saved in the database but the image itself did not save in the folder. Why is that?

Here's my code:

<?php

include '../session.php';
require_once 'config.php';


if (isset($_POST['submit'])) {

    $img_dir = "updated_photo/";
    $target_file = $img_dir . basename($_FILES["image"]["name"]);
    $imageName = $_FILES["image"]["name"];
    $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
    $imageType = $_FILES["image"]["type"];

        if (substr($imageType, 0,5) == "image") {
            $query = "UPDATE `crew_info` SET `image_name` = ? WHERE `id` = ?";
            $stmt = mysqli_prepare($conn, $query);
            mysqli_stmt_bind_param($stmt, 'si', $imageName, $_POST['id']);
            mysqli_stmt_execute($stmt);
            $id = $_POST['id'];

            header("Location: ../admin/view_all_info.php?id=$id");

        }

        else {

            echo "Image not Uploaded!";

        }

}

?>

What seems to be the problem of my codes?

  • 写回答

3条回答 默认 最新

  • douhu2898 2016-06-02 08:05
    关注

    Here try this, I edited your codes.

    <?php
    
    include '../session.php';
    require_once 'config.php';
    
    
    if (isset($_POST['submit'])) {
    
        $img_dir = "../updated_photo/";
        $target_file = $img_dir . basename($_FILES["image"]["name"]);
        $imageName = $_FILES["image"]["name"];
        $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
        $imageType = $_FILES["image"]["type"];
    
            if (substr($imageType, 0,5) == "image") {
                $query = "UPDATE `crew_info` SET `image_name` = ? WHERE `id` = ?";
                $stmt = mysqli_prepare($conn, $query);
                mysqli_stmt_bind_param($stmt, 'si', $imageName, $_POST['id']);
                mysqli_stmt_execute($stmt);
                file_put_contents($target_file, $imageData);
                $id = $_POST['id'];
                header("Location: ../admin/view_all_info.php?id=$id");
    
            }
    
            else {
    
                echo "Image not Uploaded!";
    
            }
    
    }
    
    ?>
    

    just copy and paste it. tell if it worked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入