Jump to content

Recommended Posts

I have created a user interface where use can register for an account and create profile...

The issue that I'm having is after use fills out thier profile and tries to upload an image everything works ok

but when the script call to display the image nothing is showing up. I have checked to see that the image is being saved to server and database and it shows the name and file size in database and file is on the server in the upload folder, not sure why it will not show up.

 

If someone can help me out with this I would greatly apperciate it very much, thanks in advance.

 

here is some of my code for you to review.

 

This is members page code

 

<?php 
session_start(); //allows session 
include "config.php"; 
echo "<center>"; 
if(isset($_GET['user'])) { //if there trying to view a profile 
//gets the user name and makes it safe 
$username = addslashes($_GET[user]); 
//querys the db to find the username 
$getuser = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'"); 
//checks see if the username exists in the db  

$usernum = mysql_num_rows($getuser); 
//if it don't exist  
if($usernum == 0)  

{  
//don't exist 

echo ("User Not Found");  

}  
//if it does exist then show there profile 
else{ 
$user = mysql_fetch_array($getuser);  
echo " 
<b>$user[username]'s Profile</b><br><br> 
Email: $user[email]<br> 
Location: $user[location]<br> 
Sex: $user[sex]<br> 
Age: $user[age]<br> 
Photo:  $user[content]<br>
User Description: $user[userDescription]<br>
Classification: $user[classification]<br>
Graduate Class: $user[graduateClass]<br>
Major: $user[major]<br>
Minor: $user[minor]<br>
School: $user[school]<br>
Residents: $user[reside]<br>
Phone: $user[phone]<br>
AIM: $user[aim]<br>
MSN: $user[msn]<br>
My Space: $user[mySpace]
";
} 
}else{ 
//gets all the members from the database 
$getusers = mysql_query("SELECT * FROM `members` ORDER BY `id` ASC") or die(mysql_error());  
//loops there name out 
while ($user = mysql_fetch_array($getusers)) {  
echo "<a href='members.php?user=$user[username]'>$user[username]</a><br>";  
} 
} 
echo "<center>"; 
?> 

 

This is editprofile code

 

<?php 
session_start(); //allows session 
include "config.php"; 
echo "<center>"; 
//checks see if there logged in 
if($logged[id]) {  
if(isset($_GET['update'])) { 
$email = addslashes(htmlspecialchars($_POST[email]));  
$location = addslashes(htmlspecialchars($_POST[location]));  
$age = (int)addslashes(htmlspecialchars($_POST[age]));  
$sex = addslashes(htmlspecialchars($_POST[sex])); 
$userDescription = addslashes(htmlspecialchars($_POST[userDescription])); 
$classification = addslashes(htmlspecialchars($_POST[classification]));
$graduateClass = addslashes(htmlspecialchars($_POST[graduateClass]));
$major = addslashes(htmlspecialchars($_POST[major]));
$minor = addslashes(htmlspecialchars($_POST[minor]));
$school = addslashes(htmlspecialchars($_POST[school]));
$reside = addslashes(htmlspecialchars($_POST[reside]));
$phone = addslashes(htmlspecialchars($_POST[phone]));
$aim = addslashes(htmlspecialchars($_POST[aim]));
$msn = addslashes(htmlspecialchars($_POST[msn]));
$mySpace = addslashes(htmlspecialchars($_POST[mySpace]));
$file = addslashes(htmlspecialchars($_POST[file]));
//checks the sex if its ok 
if(($sex == "Male") || ($sex == "Female")) { 
//updates there profile in the db 
include("file_array.inc");
include "upload_file.inc";
$update = mysql_query("UPDATE `members` SET `email` = '$email', `sex` = '$sex', `age` = '$age', `location` = '$location' , `userDescription` = '$userDescription', `classification` = '$classification', `graduateClass` = '$graduateClass', `major` = '$major', `minor` = '$minor', `school` = '$school', `reside` = '$reside', `phone` = '$phone', `aim` = '$aim', `msn` = '$msn', `mySpace` = '$mySpace', `name` = '$fileName', `TYPE` = '$fileType', `size` = '$fileSize', `content` = '$content' WHERE `username` = '$logged[username]'"); 
echo "Profile updated!"; 
} 
//if the sex is invalid 
else{ 
echo "Invalid sex input!"; 
} 
}else{ 
$getuser = mysql_query("SELECT * FROM `members` WHERE `username` = '$logged[username]'"); 
$user = mysql_fetch_array($getuser);  
echo "<form action='editprofile.php?update' method='post' enctype='multipart/form-data'> 
Email: <input type='text' name='email' size='30' maxlength='55' value='$user[email]'><br> 
Location: <input type='text' name='location' size='30' maxlength='40' value='$user[location]'><br> 
Age: <input type='text' name='age' size='3' maxlength='3' value='$user[age]'><br> 
Sex: <select size='1' name='sex' value='$user[sex]'> 
<br>
<option value='Male' "; if($user[sex] == Male) {  
echo "selected"; }  
echo ">Male</option>  
<option value='Female' "; if($user[sex] == Female) {  
echo "selected"; }  
echo ">Female</option> 
</select><br> 
Description: <textarea name='userDescription' value='$user[userDescription]' cols='45' rows='5'></textarea><br>
Upload photo: <input type='file' name='file' id='file' value='$user[file]' /><br>
<input type='hidden' name='MAXFILE_SIZE' value='2000000'>
Classification: <input type='text' name='classification' size='30' maxlength='64' value='$user[classification]'><br> 
Graduate Class: <input type='text' name='graduateClass' size='30' maxlength='64' value='$user[graduateClass]'><br>
Major: <input type='text' name='major' size='30' maxlength='64' value='$user[major]'><br>
Minor: <input type='text' name='minor' size='30' maxlength='64' value='$user[minor]'><br>
School: <input type='text' name='school' size='30' maxlength='64' value='$user[school]'><br>
Residents: <input type='text' name='reside' size='30' maxlength='64' value='$user[reside]'><br>
Phone: <input type='text' name='phone' size='30' maxlength='13' value='$user[phone]'><br>
AIM: <input type='text' name='aim' size='30' maxlength='30' value='$user[aim]'><br>
MSN: <input type='text' name='msn' size='30' maxlength='30' value='$user[msn]'><br>
My Space: <input type='text' name='mySpace' size='30' maxlength='30' value='$user[mySpace]'><br>
<br> 
<input type='submit' value='Update'> 
</form>"; 
} 
}else{ 
echo "You are not logged in."; 
} 
echo "<center>"; 
?> 

 

upload file code

 

<?PHP
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/tif")
|| ($_FILES["file"]["type"] == "image/psd")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/pct")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

 

File array code

 

<?php
$fileName = $_FILES['file']['name'];
$tmpName  = $_FILES['file']['tmpName'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
?>

 

the mysql database for file upload is stored in same table as user information and is like

 

(name VARCHAR(30) NOT NULL,type VARCHAR(30) NOT NULL,size INT NOT NULL,content MEDIUMBLOB NOT NULL,PRIMARY KEY(id));

 

Can anyone tell from my code what is going wrong....and why image will not display it self....

 

Thanks!!!

Link to comment
https://forums.phpfreaks.com/topic/108883-problem-displaying-image/
Share on other sites

I have created a user interface where use can register for an account and create profile...

The issue that I'm having is after use fills out thier profile and tries to upload an image everything works ok

but when the script call to display the image nothing is showing up. I have checked to see that the image is being saved to server and database and it shows the name and file size in database and file is on the server in the upload folder, not sure why it will not show up.

 

If someone can help me out with this I would greatly apperciate it very much, thanks in advance.

 

here is some of my code for you to review.

 

This is members page code

 

<?php 
session_start(); //allows session 
include "config.php"; 
echo "<center>"; 
if(isset($_GET['user'])) { //if there trying to view a profile 
//gets the user name and makes it safe 
$username = addslashes($_GET[user]); 
//querys the db to find the username 
$getuser = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'"); 
//checks see if the username exists in the db  

$usernum = mysql_num_rows($getuser); 
//if it don't exist  
if($usernum == 0)  

{  
//don't exist 

echo ("User Not Found");  

}  
//if it does exist then show there profile 
else{ 
$user = mysql_fetch_array($getuser);  
echo " 
<b>$user[username]'s Profile</b><br><br> 
Email: $user[email]<br> 
Location: $user[location]<br> 
Sex: $user[sex]<br> 
Age: $user[age]<br> 
Photo:  $user[content]<br>    /// THIS IS HOW I TRIED TO DISPLAY IMAGE FROM DATABASE
User Description: $user[userDescription]<br>
Classification: $user[classification]<br>
Graduate Class: $user[graduateClass]<br>
Major: $user[major]<br>
Minor: $user[minor]<br>
School: $user[school]<br>
Residents: $user[reside]<br>
Phone: $user[phone]<br>
AIM: $user[aim]<br>
MSN: $user[msn]<br>
My Space: $user[mySpace]
";
} 
}else{ 
//gets all the members from the database 
$getusers = mysql_query("SELECT * FROM `members` ORDER BY `id` ASC") or die(mysql_error());  
//loops there name out 
while ($user = mysql_fetch_array($getusers)) {  
echo "<a href='members.php?user=$user[username]'>$user[username]</a><br>";  
} 
} 
echo "<center>"; 
?> 

 

This is editprofile code

 

<?php 
session_start(); //allows session 
include "config.php"; 
echo "<center>"; 
//checks see if there logged in 
if($logged[id]) {  
if(isset($_GET['update'])) { 
$email = addslashes(htmlspecialchars($_POST[email]));  
$location = addslashes(htmlspecialchars($_POST[location]));  
$age = (int)addslashes(htmlspecialchars($_POST[age]));  
$sex = addslashes(htmlspecialchars($_POST[sex])); 
$userDescription = addslashes(htmlspecialchars($_POST[userDescription])); 
$classification = addslashes(htmlspecialchars($_POST[classification]));
$graduateClass = addslashes(htmlspecialchars($_POST[graduateClass]));
$major = addslashes(htmlspecialchars($_POST[major]));
$minor = addslashes(htmlspecialchars($_POST[minor]));
$school = addslashes(htmlspecialchars($_POST[school]));
$reside = addslashes(htmlspecialchars($_POST[reside]));
$phone = addslashes(htmlspecialchars($_POST[phone]));
$aim = addslashes(htmlspecialchars($_POST[aim]));
$msn = addslashes(htmlspecialchars($_POST[msn]));
$mySpace = addslashes(htmlspecialchars($_POST[mySpace]));
$file = addslashes(htmlspecialchars($_POST[file]));
//checks the sex if its ok 
if(($sex == "Male") || ($sex == "Female")) { 
//updates there profile in the db 
include("file_array.inc");
include "upload_file.inc";
$update = mysql_query("UPDATE `members` SET `email` = '$email', `sex` = '$sex', `age` = '$age', `location` = '$location' , `userDescription` = '$userDescription', `classification` = '$classification', `graduateClass` = '$graduateClass', `major` = '$major', `minor` = '$minor', `school` = '$school', `reside` = '$reside', `phone` = '$phone', `aim` = '$aim', `msn` = '$msn', `mySpace` = '$mySpace', `name` = '$fileName', `TYPE` = '$fileType', `size` = '$fileSize', `content` = '$content' WHERE `username` = '$logged[username]'"); 
echo "Profile updated!"; 
} 
//if the sex is invalid 
else{ 
echo "Invalid sex input!"; 
} 
}else{ 
$getuser = mysql_query("SELECT * FROM `members` WHERE `username` = '$logged[username]'"); 
$user = mysql_fetch_array($getuser);  
echo "<form action='editprofile.php?update' method='post' enctype='multipart/form-data'> 
Email: <input type='text' name='email' size='30' maxlength='55' value='$user[email]'><br> 
Location: <input type='text' name='location' size='30' maxlength='40' value='$user[location]'><br> 
Age: <input type='text' name='age' size='3' maxlength='3' value='$user[age]'><br> 
Sex: <select size='1' name='sex' value='$user[sex]'> 
<br>
<option value='Male' "; if($user[sex] == Male) {  
echo "selected"; }  
echo ">Male</option>  
<option value='Female' "; if($user[sex] == Female) {  
echo "selected"; }  
echo ">Female</option> 
</select><br> 
Description: <textarea name='userDescription' value='$user[userDescription]' cols='45' rows='5'></textarea><br>
Upload photo: <input type='file' name='file' id='file' value='$user[file]' /><br>  /// THIS IS THE CODE FOR IMAGE UPLOAD
<input type='hidden' name='MAXFILE_SIZE' value='2000000'> /// THIS IS MAX FILE SIZE
Classification: <input type='text' name='classification' size='30' maxlength='64' value='$user[classification]'><br> 
Graduate Class: <input type='text' name='graduateClass' size='30' maxlength='64' value='$user[graduateClass]'><br>
Major: <input type='text' name='major' size='30' maxlength='64' value='$user[major]'><br>
Minor: <input type='text' name='minor' size='30' maxlength='64' value='$user[minor]'><br>
School: <input type='text' name='school' size='30' maxlength='64' value='$user[school]'><br>
Residents: <input type='text' name='reside' size='30' maxlength='64' value='$user[reside]'><br>
Phone: <input type='text' name='phone' size='30' maxlength='13' value='$user[phone]'><br>
AIM: <input type='text' name='aim' size='30' maxlength='30' value='$user[aim]'><br>
MSN: <input type='text' name='msn' size='30' maxlength='30' value='$user[msn]'><br>
My Space: <input type='text' name='mySpace' size='30' maxlength='30' value='$user[mySpace]'><br>
<br> 
<input type='submit' value='Update'> 
</form>"; 
} 
}else{ 
echo "You are not logged in."; 
} 
echo "<center>"; 
?> 

 

upload file code

 

<?PHP
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/tif")
|| ($_FILES["file"]["type"] == "image/psd")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/pct")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

 

File array code

 

<?php
$fileName = $_FILES['file']['name'];
$tmpName  = $_FILES['file']['tmpName'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
?>

 

the mysql database for file upload is stored in same table as user information and is like

 

(name VARCHAR(30) NOT NULL,type VARCHAR(30) NOT NULL,size INT NOT NULL,content MEDIUMBLOB NOT NULL,PRIMARY KEY(id));

 

Can anyone tell from my code what is going wrong....and why image will not display it self....

 

Thanks!!!

Well here I try to call for the image

 

echo " 
<b>$user[username]'s Profile</b><br><br> 
Email: $user[email]<br> 
Location: $user[location]<br> 
Sex: $user[sex]<br> 
Age: $user[age]<br> 
Photo:  $user[content]<br>                             /// THIS IS HOW I TRIED TO DISPLAY IMAGE FROM DATABASE
User Description: $user[userDescription]<br>
Classification: $user[classification]<br>
Graduate Class: $user[graduateClass]<br>
Major: $user[major]<br>
Minor: $user[minor]<br>
School: $user[school]<br>
Residents: $user[reside]<br>
Phone: $user[phone]<br>
AIM: $user[aim]<br>
MSN: $user[msn]<br>
My Space: $user[mySpace]
";

how to verify image has been added to the database and not just the image type, size and name??

 

cause now the name, size and type are in the database but content BLOB is showing up like  [bLOB - 0 B] /// Should this not read somthing and not 0 B

 

is this right or did it not take the image in the content BLOB??

 

 

Please help

Do you see anything wrong with

 

$update = mysql_query("UPDATE `members` SET `email` = '$email', `sex` = '$sex', `age` = '$age', `location` = '$location' , `userDescription` = '$userDescription', `classification` = '$classification', `graduateClass` = '$graduateClass', `major` = '$major', `minor` = '$minor', `school` = '$school', `reside` = '$reside', `phone` = '$phone', `aim` = '$aim', `msn` = '$msn', `mySpace` = '$mySpace', `name` = '$fileName', `TYPE` = '$fileType', `size` = '$fileSize', `content` = '$content' WHERE `username` = '$logged[username]'");

 

The image name, type, size and content should be going to database NO??

 

File array is

$fileName = $_FILES['file']['name'];

$tmpName  = $_FILES['file']['tmpName'];

$fileSize = $_FILES['file']['size'];

$fileType = $_FILES['file']['type'];

 

Any ideas??

I don't think the file is getting uploaded. try this..

 

$filepath = $_SERVER['DOCUMENT_ROOT'] ."/images/" . $_FILES['file01']['name'];

$upload = $_FILES['file01']['tmp_name'];

$err = move_uploaded_file($upload, $filepath);

 

then make your DB connection and save it in $user[content]

what you need to do is store the name of the file in the DB and store the actual image in your image directory. The following code will grab a <input type='file' name='file01'> file and save it in your 'images' directory. A connection to your database will grab the image name, width and height - no need for a type here as the filename ie: img01.jpg already holds that info.

-------------

if(isset($_FILES['file01'])) {

$fil = $_FILES['file01']['name'];

 

if(file_exists($_FILES['file01']['tmp_name'])) {

$upload = $_FILES['file01']['tmp_name'];

$filepath = $_SERVER['DOCUMENT_ROOT'] ."/images/" . $_FILES['file01']['name'];

 

try {

 

$err = move_uploaded_file($upload, $filepath);

}

catch(EXCEPTION $e) {

 

echo $e;

}

}

 

}

else {

echo "upload failed";

}

------------------

now the file is saved in a directory on your server

-------------------------------------

now connect to your DB and grab the file name ie: $user[content]

-----------------------------

then display the image

--------------------

$img = $user[content];

$w =  $user[width];

$h =  $user[height];

echo "Photo: " . "<img src='/images/" . $img . "' width='" . $w . "' height='" . $h . "'>";

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.