简体   繁体   中英

php image upload cant copy the actual file to the destination folder

I am having a problem when trying to upload an image to my mysql database which has 3 fields: id(auto increment), username and image location. The code is only inserting the image location to the database but not copying the actual image to the "profile_pics" directory. Please assist.

This is my code:

<?php

$connection=mysql_connect("localhost", "root", "");
$choose_db=mysql_select_db("ninjacity", $connection) or die (mysql_error());

if ($_SESSION['user'] && $_POST['submit'])
{
    function check() // to check if the pic already exists
    {
        $username=$_SESSION['user'];
        $choose_db;
        $sql_check="SELECT * FROM profile_pics WHERE username='$username'";
        $res_check=mysql_query($sql_check) or die (mysql_error());
        return mysql_num_rows($res_check);
    }

    $name=$_FILES['image']['name'];
    $tmp_name=$_FILES['image']['tmp_name'];


        $check=check();
        if ($check==1)
        {
            echo "You already have a profile pic. IF you wish to change it, please use the 'Edit my Profile' section";
        }
        else
        {
            $name;
            $tmp_name;
            $username=$_SESSION['user'];
            $location="profile_pics/$name";
            move_uploaded_file($tmp_name, "profile_pics/".$name);

            $choose_db;
            $sql="INSERT INTO profile_pics VALUES ('$_POST[id]', '$username', '$location')";
            $res=mysql_query($sql) or die (mysql_error());
            print "Image successfully uploaded";
        }

}
else
{
    echo "Please fill all fields";
}


?>

Have you checked the permission of the profile_pics directory . It should be 777.

Or the issue related to the path (destination path of your directory )you have used in move_uploaded_file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM