hi every one i have create a table with name upload
id int notnull
name varchar notnull
type varchar notnull
size varchar notnull
content varchar not null

and my code is the follwing but instead of iamge it show garbage value

<form method="post" enctype="multipart/form-data" action="image.php">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">

<td>Name:</td><td><input name="username" type="text" size"20"></input></td>
<tr>
<td width="246">

<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>

</tr>
</table>
</form>





<?php
$name = $_POST['username']; 
if($_POST['upload']){
if($name!=''){
$servername="localhost";
    $username="root";
    $conn=  mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("test",$conn);
    $sql = "Insert into upload(username) values('$name')";
$result=mysql_query($sql,$conn) or die(mysql_error());?>
<div class="message">you have sucessfully upload picture<br/></div>
<?php }
else $error="Name is missing please fill it";
}
?>  
<?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$name = $_POST['username']; 

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'config.php';
//include 'library/opendb.php';

$query = "INSERT INTO upload (name,size, type, content ) ".
"VALUES ('$fileName','$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');
//include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
}
?>


<?php
$con=mysql_connect("localhost", "root", "");
mysql_select_db("test",$con);


    $dn = mysql_query('select content from upload where id="'.$id.'" ');
    if(mysql_num_rows($dn)>0)
    {
        $dnn = mysql_fetch_array($dn);
        //We display the user datas
?>
 <?php while($dnn = mysql_fetch_array($dn))
 {?>

<table style="width:500px;">
    <tr>
        <td><?php

?></td>


        <div class='center'><img src="<?php echo $dnn['content']; ?>" /></td></div>
    </tr>

<?php
}
//We add a link to send a pm to the user
if(isset($_SESSION['username']))
{
?>

<?php
}
    }
    else
    {
        echo 'This user dont exists.';
    }



?> 







</body>
</html>

Recommended Answers

All 18 Replies

Member Avatar for LastMitch

@sultankhan

and my code is the follwing but instead of iamge it show garbage value

What do you mean garbage value?

Member Avatar for diafol
<img src="<?php echo $dnn['content']; ?>" />

You can't display the content like this. Your page seems to be doing quite a lot of work. It may be better to have it chunked into smaller files.

You could do this:

header("Content-type: image/gif"); // but get the right type 
echo $dnn['image']; 

Or:

<img src="data:image/gif;base64,<?php echo base64_encode($dnn['contents']);?>" />

The disadvantage of the above AFAIK is that it can't be cached.

If I were to do this, I'd probably have an image.php file with the code above in it (and some filename parsing to get the mime type), so something like this:

<img src="image.php?id=<?php echo $dnn['id'];?>" />

The image.php file then does something like:

//check get id exists then:
$id = $_GET['id'];
//clean for DB if using mysql
//then get the content from the DB via query
//state the mime type
//echo the contents

sorry one thing i have save content as mediumblob in which i am going to save my image

Storing an image in a varchar column is probably causing the issue, use a binary column type.

sir i am sorry i storing it image in content fieald and i have save it meduimblob mean as binary type but still it give the the above error
mean this one
the image http://localhost/php/image.php cannot be displayed because it contain errors

Have you tried diafol's suggestion (using a separate image script)?

yes i have html page and save it with the name upload.php which have the follwoing code and my image.php have the above code as i have post

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css" />
<title>Upload pictuer</title>
</head>

<body>
<form method="post" enctype="multipart/form-data" action="image.php">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">

<td>Name:</td><td><input name="username" type="text" size"20"></input></td>
<tr>
<td width="246">

<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>

</tr>
</table>
</form>

</body>
</html>

please anay one can solve this problem for me please

    id      name                 type       size        content
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]

this my table look like above...........

I was talking about the separate script for showing your image. Show the code you have for that one (what diafol said, not with all the markup you posted in your question).

please anay one can solve this problem for me please

    id      name                 type       size        content
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]
    0       registor-img.jpg    image/jpeg  7417    [BLOB - 7.2KiB]

this my table look like above...........

dear sir this what i have in my image.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
$name = $_POST['username']; 
if($_POST['upload']){
if($name!=''){
$servername="localhost";
    $username="root";
    $conn=  mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("test",$conn);
    $sql = "Insert into upload(username) values('$name')";
$result=mysql_query($sql,$conn) or die(mysql_error());?>
<div class="message">you have sucessfully upload picture<br/></div>
<?php }
else $error="Name is missing please fill it";
}
?>  
<?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$name = $_POST['username']; 

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'config.php';
//include 'library/opendb.php';

$query = "INSERT INTO upload (name,size, type, content ) ".
"VALUES ('$fileName','$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');
//include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
}
?>


<?php
$con=mysql_connect("localhost", "root", "");
mysql_select_db("test",$con);


    $dn = mysql_query('select content from upload where id="'.$id.'" ');
    if(mysql_num_rows($dn)>0)
    {
        $dnn = mysql_fetch_array($dn);
        //We display the user datas
?>
 <?php while($dnn = mysql_fetch_array($dn))
 {?>

<table style="width:500px;">
    <tr>
        <td><?php

?></td>
    <?Php header("Content-type: image/jpeg"); // but get the right type
    echo $dnn['content']; ?>
        <div class='center'><img src="localhost/php/upload/<?php echo $dnn['content']; ?>" /></td></div>
    </tr>

<?php
}
//We add a link to send a pm to the user
if(isset($_SESSION['username']))
{
?>

<?php
}
    }
    else
    {
        echo 'This user dont exists.';
    }



?> 







</body>
</html>

@pritaeas please solve this for me...........
thank you very much

Member Avatar for diafol

Don't use a DOCTYPE for an image generator. The mime type is there so that an image is displayed - there should be no other output before of after. So the way you've gone about this is wrong. As you're using mysql_*, I'll do so too, but moving to mysqli_* or PDO functions would be better.

Your image.php file:

$getDefault = true;
//assume mysql details included here somewhere
if(isset($_GET)){
    $id = intval($_GET);
    $r = mysql_query("SELECT type, content FROM upload WHERE id = $id");
    if(mysql_num_rows($r)){
        $dnn = mysql_fetch_assoc($r);
        $getDefault = false;
    }
}
if($getDefault){
   $r = mysql_query("SELECT type, content FROM upload WHERE id = 1"); //this is the default image
   $dnn = mysql_fetch_assoc($r);
}

$mimeType = $dnn['type'];
$content = $dnn['content'];

header("Content-type: $mimeType");
echo $content;

That's it. Maybe some more error handling for production code. But along those lines.

You just call it from links - which can be static:

<img src="image.php?id=74" />

Or dynamic:

<img src="image.php?id=<?php echo $someIDVar;?>" />

Or:

echo "<img src='image.php?id=$someIDVar' />";

As I said in my previous post - your files are trying to do too much. You can certainly include files, and these help to compartmentalize functions.

@pritaeas please solve this for me...........

Oops. Sorry p, this was for your eyes only, by the sounds of it. :)

dear diafol i have done my task with the following code
`

<?php 
if(isset($_SESSION['username']))
{
$id = intval($_GET['ano']);
$con=mysql_connect('localhost','root','');
if(!$con)
{ 
  echo mysql_error();
}
$db=mysql_select_db('test',$con);
if(!$db)
{ 
  echo mysql_error();
}


$sql='SELECT *FROM image';

$r=mysql_query("$sql",$con);
if($r)
  { 
    while($row=mysql_fetch_array($r))

      { ?> 

         <div class='left1'>Posted by <?php echo $row['userid']; ?><div class='authorcolor'><?php echo $row['username']; ?></a></div></div>

       <?php   echo $row['aname'];
         echo "<br>";
         echo $row['adetails'];
         echo "<br>";

         //header("content-type:aphototype");
         echo "<img src=image.php?ano=".$row['ano']." width=600 height=200/>";
         echo "<br>";
         echo $row['date'];
         echo "<br>";
      }
  }
  else
  {
  echo mysql_error();
  }
  }
else
{
    echo '<div class="message">You must be logged to access this page.</div>';
}
?>Inline Code Example Here

`

and it done my geol but now anthor problem which i am facing is that how to retrive the username which upload picture my image table is

ano id  id2  aname  adetails            aphoto          aphototype  user1   date
 7  0   0   sultan  this good girl  [BLOB - 19.2KiB]    image/jpeg  2     2012-11-27 

ano is image id and user1 is the user whic have upload the picture
now i want that when a user upload a picture then it name come with picture ihave try this one but it dont show name when i change $row[userid] to $row[user1] it give me the id of the user while i want username

 while($row=mysql_fetch_array($r))

      { ?> 

         <div class='left1'>Posted by <?php echo $row['userid']; ?><div class='authorcolor'><?php echo $row['username']; ?></a></div></div>

       <?php   echo $row['aname'];
         echo "<br>";
         echo $row['adetails'];
         echo "<br>";

         //header("content-type:aphototype");
         echo "<img src=image.php?ano=".$row['ano']." width=600 height=200/>";
         echo "<br>";
         echo $row['date'];
         echo "<br>";
      }
Member Avatar for diafol

You need to use a JOIN:

e.g.

SELECT i.*, u.username FROM image AS i INNER JOIN users AS u ON i.user1 = u.user_id

Now you get the 'username' into the $row array too. Note - I don't know what your users table is called nor the primary key for that table - so you may need to change 'users' and 'user_id'

thanks diafol i have solved it by this method
just add user name in table add link to it and its work

SELECT *FROM image
$r=mysql_query("$sql",$con);
if($r)
  { 
    while($row=mysql_fetch_array($r))

      { ?> 

         <div class='left1'> Uploaded by <a href="profilee.php?id= <?php echo $row['userid']; ?>"><div class='authorcolor'>  <?php echo $row['username']; ?></a></div></div><div class='date1'> <?php echo $row['date'];?></div>

       <?php   echo $row['aname'];
         echo "<br>";
         echo $row['adetails'];
         echo "<br>";

         //header("content-type:aphototype");
         echo "<img src=image.php?ano=".$row['ano']." width=600 height=200/>";
         echo "<br>";
         echo "<br>";
         echo "<br>";
         echo "<br>";
      }
  }

you are so sweet and helpul thank you very much i have add a new question please read it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.