在PHP中将图像位置保存到MySQL DB

时间:2014-06-13 23:48:25

标签: php mysql

再次疲惫不堪试图看看我做错了什么。我正在尝试将照片上传到我的页面,并使用预准备语句将其位置保存到mysql。我的代码如下

<?php

// First we execute our common code to connection to the database and start the session
require("common.php");

// At the top of the page we check to see whether the email is logged in or not
if(empty($_SESSION['email']))
{
    // If they are not, we redirect them to the login page.
    header("Location: ../index.html");
    die("Redirecting to FrontPage");
}

$email = $_SESSION['email']['email'];

//Profile Image upload script
if(isset($_FILES['profilepic']))
{
    if((($_FILES["profilepic"]["type"]=="image/jpeg") || ($_FILES["profilepic"]["type"]=="image/png") || ($_FILES["profilepic"]["type"]=="image/gif")) && ($_FILES["profilepic"]["size"] < 1048576)) 
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $rand_dir_name = substr(str_shuffle($chars), 0, 15);
        mkdir("userdata/profile_pics/$rand_dir_name");

        if(file_exists("userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]))
        {
            echo $_FILES["profilepic"]["name"]."Already exists";
        }
        else
        {
            move_uploaded_file($_FILES["profilepic"]["tmp_name"], "userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

            $profile_pic_name = $_FILES["profilepic"]["name"];

            $queryProfilePic = "UPDATE db SET profilePic=:profile_Pic WHERE email = :email";

            $query_params = array(':profile_Pic' => $rand_dir_name/$profile_pic_name,':email' => $email);

            $stmt = $db->prepare($queryProfilePic);
            $result = $stmt->execute($query_params);

            //header("Location: profile.php");
        }
    }
    else
    {

    }   
}

?>

图像被上传到服务器上随机生成的文件夹,但是当我尝试将文件的位置保存到数据库时,我得到一个大鹅蛋。

1 个答案:

答案 0 :(得分:1)

您无法划分这些值:

$rand_dir_name/$profile_pic_name

我假设你想连接它们:

$rand_dir_name . '/' . $profile_pic_name

或者您可以使用

"$rand_dir_name/$profile_pic_name"