herghost Posted April 11, 2009 Share Posted April 11, 2009 Hi all, When the below code runs, how can I store the result (image) as a session? <?php session_start(); include('include/database.php'); // Make sure the user actually // selected and uploaded a file if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; $userid = $_SESSION['SESS_USERID']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); // Create the query and insert // into our database. $sql = mysql_query("SELECT * FROM bandpic WHERE userid = '$userid'"); if(mysql_num_rows($sql) == 0) { $query = "INSERT INTO bandpic (userid, image) values ('$userid','$data')"; } else $query = "UPDATE bandpic SET userid = '$userid', image = '$data'"; } $result = mysql_query($query); session_regenerate_id(); $bandpic = mysql_fetch_assoc($result); $_SESSION["BANDPIC"] = $bandpic['image']; session_write_close(); //Check whether the query was successful or not if($result) { header("location: member_home.php"); exit(); }else { die(mysql_error()); } // Close our MySQL Link mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/153580-add-as-session/ Share on other sites More sharing options...
Goldeneye Posted April 11, 2009 Share Posted April 11, 2009 You have to use a SELECT query to pull the image out of the database. Either that, or use the variable $data as opposed $bandpic['image']. $bandpic is null because the query you assigned it to is not retrieving information, it's inserting information. (Hopefully that's clear.) Link to comment https://forums.phpfreaks.com/topic/153580-add-as-session/#findComment-807014 Share on other sites More sharing options...
herghost Posted April 11, 2009 Author Share Posted April 11, 2009 I think so, basically I can not store it as a session as its a file and not a string? Link to comment https://forums.phpfreaks.com/topic/153580-add-as-session/#findComment-807038 Share on other sites More sharing options...
xtopolis Posted April 11, 2009 Share Posted April 11, 2009 What are you trying to accomplish..? It looks like you upload the file... open the temp version of it.. read it.. add slashes to it (why?) Then you store the data in mysql. Why do you need it in a session? Link to comment https://forums.phpfreaks.com/topic/153580-add-as-session/#findComment-807044 Share on other sites More sharing options...
Goldeneye Posted April 11, 2009 Share Posted April 11, 2009 <?php session_start(); include('include/database.php'); // Make sure the user actually // selected and uploaded a file if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; $userid = $_SESSION['SESS_USERID']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); fclose($fp); // Create the query and insert // into our database. $sql = mysql_query("SELECT * FROM bandpic WHERE userid = '$userid'"); if(mysql_num_rows($sql) == 0) { $query = "INSERT INTO bandpic (userid, image) values ('$userid','$data')"; } else $query = "UPDATE bandpic SET userid = '$userid', image = '$data'"; } //Check whether the query was successful or not if($result) { session_regenerate_id(); $_SESSION["BANDPIC"] = $data; session_write_close(); header("location: member_home.php"); exit(); }else { die(mysql_error()); } // Close our MySQL Link mysql_close($con); ?> Try this and see if it works. Also, as xtopolis implied, there's no conceivable reason to pass your image through the addslashes() function. Link to comment https://forums.phpfreaks.com/topic/153580-add-as-session/#findComment-807046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.