my form is to upload an image for a user. the images are stored in a folder, and the path is supposed to be stored in the db, but it is not. that is, the image is properly being uploaded to folder, but the path is not being saved to db.
ive tried two totally different queries, but neither has worked. also, i referenced both of these questions; How to upload images into MySQL database using PHP code and php image not uploading to database, the following is the code in question.
<?php
// load current profile photo script
$username='';
$check_pic='';
$check_pic = mysqli_query($connection,"SELECT profile_pic FROM users WHERE username='$username'");
$get_pic_row = mysqli_fetch_assoc($check_pic);
$profile_pic_db = $get_pic_row['profile_pic'];
if ($profile_pic_db == "") {
$profile_pic = "images/default_pic.jpg";
}
else
{
$profile_pic = "userdata/profile_pics/".$profile_pic_db;
}
//script for uploading profile photo
if (isset($_FILES['profilepic'])) {
if ((@$_FILES["profilepic"]["type"]=="image/jpeg")) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name");
if (file_exists("C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
{
echo @$_FILES["profilepic"]["name"]." Already exists";
}
else
{
//moves images to folder userdata/profile_pics...
move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
//saves image url to table...
$profile_pic_name = (@$_FILES["profilepic"]["name"]);
$profile_pic_loc = "C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/$profile_pic_name'";
if($profile_pic_query = mysqli_query($connection, "INSERT INTO users (profile_pic) VALUES ('$profile_pic_loc')")){
echo "successful upload";
}
else {
echo "unsuccessful upload";
}
header("Location: profile.php");
}
}
else
{
echo "unsuccessful";
}
}
echo"
<p>UPLOAD PROFILE PHOTO:</p>
<form action='' method='POST' enctype='multipart/form-data'>
<img src='$profile_pic' width='70' />
<input type='file' name='profilepic' /><br />
<input type='submit' name='uploadpic' value='Upload Image'>
</form>
";
?>
i tried the $profile_pic_query
being part of the if statement, as seen above, and without the if statement. i also tried $profile_pic_name = file_get_contents(@$_FILES["profilepic"]["name"]);
with and without file_get_contents
, with no difference.
this was the other format of my query i tried that didnt do anything different;
//saves image to folder userdata/profile_pics...
move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
//saves image url to table...
$profile_pic_name = @$_FILES["profilepic"]["name"];
$profile_pic_query = mysqli_query($connection, "UPDATE users SET profile_pic='C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/$profile_pic_name' WHERE username='$username'");
and this is the session stuff...
<? php
session_start();
if (isset($_SESSION['user_login'])) {
$username = $_SESSION["user_login"];
}
else {
$username = "";
}
?>
so is there something wrong with the session, the query, or did i make a syntax error that im not being warned about for some reason?
****UPDATE**
Following suggestion below, I comment out the;
header(location: 'profile.php');
And I get apparently pretty popular error:
file_get_contents(.jpg): failed to open stream: no such file or directory
Common suggestions seem to revolve around ensuring that a proper tmp_file have been created, but that doesn't seem the case here?