my form upload image user. images stored in folder, , path supposed stored in db, not. is, image being uploaded folder, path not being saved db.
ive tried 2 totally different queries, neither has worked. also, referenced both of these questions; how upload images mysql database using php code , php image not uploading database, following code in question.
<?php // load current profile photo script $username=''; $check_pic=''; $check_pic = mysqli_query($connection,"select profile_pic users 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 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"]." exists"; } else { //moves images 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 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 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 $profile_pic_query
being part of if statement, seen above, , without if statement. tried $profile_pic_name = file_get_contents(@$_files["profilepic"]["name"]);
, without file_get_contents
, no difference. other format of query tried didnt different;
//saves image 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 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' username='$username'");
and session stuff...
<? php session_start(); if (isset($_session['user_login'])) { $username = $_session["user_login"]; } else { $username = ""; } ?>
so there wrong session, query, or did make syntax error im not being warned reason?
****update**
following suggestion below, comment out the;
header(location: 'profile.php');
and apparently pretty popular error:
file_get_contents(.jpg): failed open stream: no such file or directory
common suggestions seem revolve around ensuring proper tmp_file have been created, doesn't seem case here?
you have several problems.
the first query store location wrong, "insert ... where..." form isn't correct. should use "insert ... values ..." explained in mysql documentation.
second, shouldn't use relative paths in functions such move or mkdir. can learn why here : php - failed open stream : no such file or directory
third, cannot use "header()" after echo. header() sends headers of http response. cannot done once have started echoing, since headers have been sent.
Comments
Post a Comment