tags:

views:

48

answers:

1

I am working on image uploader in which the user uploads the image to the mysql database via a simple HTML/PHP form.

It works, and now I want to retrieve the saved images from MySQL in a HTML table format. But I am not successful. I am very much a novice in PHP. I have attached the currently used code... can someone please help me create a solution which displays the result as pretty looking table?

I also want to allow the user to upload multiple files/images at a time and store those in the database. How would that be possible?

Here is my code:

$query = "SELECT * FROM ae_gallery";
$result = mysql_query($query) or die('Error, query failed');
 if(mysql_num_rows($result) == 0)
 {
echo "Database is empty <br>";
 }
else
{
 echo "<center><b><h1><u>WV Images</u></h1></b></center><br>";
 echo "E_Name &ndash; Job_Num &ndash; Reason &ndash; Time &ndash;Image<br><hr>";
while(list($E_Name,$Job_num,$ext,$image_time,$data,$Remarks,)=mysql_fetch_array($result))
  {
   echo "
   {$E_Name}
    &ndash;
   {$Job_num}
    &ndash;
    {$Model}
    &ndash;
    {$Reason}
    &ndash;
    {$image_time}
    &ndash;
    <a href='download.php?id={$id}'><img src='http://pagename/wv-images   /download.php?id={$id}' width='80' height='80' alt='{$title}' border='0'></a> 
   <br><hr>" ;
    }
    }
   ?>
  </body>
  </html>
A: 

Normally what you'll do is upload the image to the filesystem, typically under a filename like 123.jpeg, where 123 is the primary key of the database row associated with the uploaded file. (Don't trust the user's submitted filename to store the file under.)

You can upload images into a BLOB column in the database itself, and then use a download.php script to retrieve and spit it out:

<?php
    $result= mysql_query('SELECT imagedata FROM ae_gallery WHERE id='.intval($_GET['id']))
    if (mysql_num_rows($result)==0) {
        header('Status: 404 Not Found');
        exit();
    }
    $row= mysql_fetch_assoc($result);
    header('Content-Type: image/jpeg');
    echo $row['imagedata'];
// no PHP end-tag to avoid accidental whitespace being included in the output

However doing it this way means you'll get no caching by default. You have to add a load of quite complicated caching header handling to stop it re-fetching the image each time. The web server is in general a more efficient way of serving static data.

For the page you quoted, you'll really want to display in a <table>. You will also need to use htmlspecialchars() every time you echo a text value into HTML, otherwise you're going to have HTML-injection issues and potential cross-site-scripting security holes.

<?php
    function h($s) {
        echo htmlspecialchars($s);
    }

    $result= mysql_query('SELECT * FROM ae_gallery') or die('Error, query failed');
?>

<?php if (mysql_num_rows($result)==0) { ?>
    Database is empty <br/>
<?php } else { ?>
    <table>
        <tr>
            <th>E_Name</th>
            <th>Job_Num</th>
            <th>Model</th>
            <th>Reason</th>
            <th>Time</th>
            <th>Image</th>
        </tr>
        <?php while ($row= mysql_fetch_assoc($result)) { ?>
            <tr>
                <td><?php h($row['E_Name']); ?></td>
                <td><?php h($row['Job_Num']); ?></td>
                <td><?php h($row['Model']); ?></td>
                <td><?php h($row['Reason']); ?></td>
                <td><?php h($row['image_time']); ?></td>
                <td>
                    <a href="/uploaded-images/<?php h($row['id']); ?>.jpeg">
                        <img src="/uploaded-images/<?php h($row['id']); ?>.jpeg" alt="<?php h($row['title']); ?>"/>
                    </a>
                </td>
            </tr>
        <?php } ?>
    </table>
<?php  } ?>

Notes:

  • I've defined a function with a short name to avoid having to type out echo htmlspecialchars() so much.

  • I've assumed that id and title are columns in the table. (They don't seem to come from anywhere in the example code.)

  • I've assumed that the variable names you used are the column names in the table, so used the same names to access $row. It's better to access the results of a query through named columns rather than relying on their order (which may change if you start altering the schema).

bobince
thanks ...it work.
how to place a thumbnail of images in place of large image, i think it can done by width and height parameter in img tag.
i tried width, height ...but not work...i thinks some colon or semicolon,quotes issue..so what is the proper syntax for width, height for this situation.
i also same image displaying....that's means ['id'] not working..
width, height done..only problem with ['id]
You can set `height="..." width="..."` on the `img` tag, or use CSS to set `height: ...px; width: ...px;` on all `img` elements in the table at once. However note that letting the browser resize images itself usually results in somewhat ugly pixelly results (due to browsers using ‘nearest neighbour’ resizing by default). And of course you're transferring the whole (large) image file for every image where a thumbnail version could be much smaller. It's possible to have the server-side create a thumbnail by resizing the uploaded image at upload-time eg with `imagecopyresampled`.
bobince
@bobince thanks can you tell me why ['id'] don't send any value to download.php page ... i am unable to display images.
Can't really say without seeing code, can you put it up somewhere? I don't know where your `id` actually comes from or how you're passing it. However note if the `id` is not numeric-only and you are making a URL with it in a query, you will need to URL-encode it: `src="/download.php?id=<?php echo urlencode($row['id']); ?>"`.
bobince
['id'] comes from database.
i have two php file one is view.php and another is download.php in download.php coding like this... <?php if(isset($_GET['id'])) ----- $id = $_GET['id'];$query="SELECT * FROM ae_gallery WHERE id='$id'";$result = mysql_query($query) or die('Error, query failed');list($id,$E_Name,$IMEI,$Job_num,$Model,$Reason,$ext,$image_time,$data,$Remarks,$Approved_by) = mysql_fetch_array($result);header("Content-type: image/{$ext}");header("Content-Disposition: attachment; filename=$IMEI");echo $data;exit;}
coding of veiw.php given in question.
You've got a serious SQL injection vulnerability in your SELECT query. You must SQL-string-literal-escape `$_GET['id']` before inserting it into a query. Use `mysql_real_escape_string`, or parameterised queries. Other than that, if you are going to `?id=x` then `$_GET['id']` definitely should be `x`.
bobince