user74091 user74091 - 3 months ago 10
PHP Question

images not uploading to database but saving to folder

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?

Answer

i have since resolved this problem myself and would like to share for anyone of similar predicament... there were several distinct problems. well, i the errors i was receiving were seemingly erroneous. the errors were with respect to the portion of the script that was responsible for uploading the images from the client to the hdd of the server. the script was properly uploading and saving the images, however the errors i was getting were fixed when i removed uneccessary portions of the path.

the main problem was with the query, uploading the image url to the table, that wasnt happening at all. this was all corrected when i re-wrote the query. see all code below.

<?php



  $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 to hard disk

   if (isset($_FILES['profilepic'])) {
   if ((@$_FILES["profilepic"]["type"]=="image/jpeg")) {

   $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"]);

  //update table with url for photo

 $profile_pic_loc = "$rand_dir_name/".@$_FILES["profilepic"]["name"];


 $profile_pic_query = mysqli_query($connection, "UPDATE users SET profile_pic='$profile_pic_loc' WHERE username='$username'"); 


  if ($profile_pic_query) {

   header("Location: profile.php");
     }

   else 
   {
        die('failure');
     }
   }
  }
   }


   echo    " 

<div>
<p>UPLOAD PROFILE PHOTO:</p>
<form action='profile.php' 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>
</div>
";


?>

changing;

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"]))

and;

move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

to;

mkdir("userdata/profile_pics/$rand_dir_name");

   if (file_exists("userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
   {

and;

move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

corrected all the erroneous errors about 'filed to open file stream' and what not... although i still dont know what that means.

also correcting the query to;

$profile_pic_loc = "$rand_dir_name/".@$_FILES["profilepic"]["name"];


 $profile_pic_query = mysqli_query($connection, "UPDATE users SET profile_pic='$profile_pic_loc' WHERE username='$username'"); 


  if ($profile_pic_query) {

   header("Location: profile.php");
     }

   else 
   {
        die('failure');
     }

successfully inserted the image URL into the table. works like a champ now.