will not display uploaded image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

will not display uploaded image

Post by odie2828 »

Can someone please help.

the following codes for 3 files is used to view images in an album. the images are coming up as red x's.

any thoughts?

list-image.php

Code: Select all

 
 
<?php
 
if (isset($_GET['delete']) && isset($_GET['album']) && isset($_GET['imgId'])) {
    // get the image file name so we
    // can delete it from the server 
    $sql = "SELECT im_image, im_thumbnail
            FROM tbl_image
            WHERE im_id = {$_GET['imgId']} AND im_album_id = {$_GET['album']}";
    $result = mysql_query($sql) or die('Delete album failed. ' . mysql_error());
    if (mysql_num_rows($result) == 1) {
        $row = mysql_fetch_assoc($result);
        
        // remove the image and the thumbnail from the server
        unlink(GALLERY_IMG_DIR . $row['im_image']);
        unlink(GALLERY_IMG_DIR . 'thumbnail/' . $row['im_thumbnail']);
            
        // and then remove the database entry
        $sql = "DELETE FROM tbl_image
                WHERE im_id = {$_GET['imgId']} AND im_album_id = {$_GET['album']}";
        mysql_query($sql) or die('Delete album failed. ' . mysql_error());      
    
    }   
}
 
$imagePerPage = 10;
 
$album = isset($_GET['album']) ? $_GET['album'] : '';
$pageNumber  = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
 
$offset = ($pageNumber - 1) * $imagePerPage;
$serial = $offset + 1;
 
// get album list
$sql = "SELECT al_id, al_name
        FROM tbl_album
        ORDER BY al_name";
$result = mysql_query($sql) or die('Error, get album list failed : ' . mysql_error());                    
 
$albumList = '';
while ($row = mysql_fetch_assoc($result)) {
    $albumList .= '<option value="' . $row['al_id'] . '"' ;
    
    if ($row['al_id'] == $album) {
        $albumList .= ' selected';
    }
    
    $albumList .= '>' . $row['al_name'] . '</option>';  
}
?>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
 
 
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1">
    <tr> 
        <td align="right">Album : 
    <select name="cboAlbum" id="cboAlbum" onChange="viewImage(this.value)">
        <option value="">-- All Album --</option>
        <?php echo $albumList; ?> 
    </select></td>
    </tr></table>
<?php
$sql  = "SELECT im_id, im_title, im_thumbnail, DATE_FORMAT(im_date, '%Y-%m-%d') AS im_date
         FROM  tbl_image ";
 
if ($album != '') {
    $sql .= "WHERE im_album_id = $album ";
}
 
$sql .= "ORDER BY im_title ";
 
$result = mysql_query($sql . "LIMIT $offset, $imagePerPage") or die('Error, list image failed. ' . mysql_error());
?>
<table width="100%" border="1" align="center" cellpadding="2" cellspacing="1" bordercolor="#FFFFFF" class="table_grey">
<tr> 
        <th width="30" align="center" bgcolor="#000000"><span class="style1">#</span></th>
    <th align="center" bgcolor="#000000"><span class="style1">Image </span></th>
    <th width="120" align="center" bgcolor="#000000"> Date</th>
    <th width="60" align="center" bgcolor="#000000">&nbsp;</th>
    <th width="60" align="center" bgcolor="#000000">&nbsp;</th>
  </tr>
    <?php 
if (mysql_num_rows($result) == 0) {
?>
    <tr bgcolor="#000000"> 
        <td colspan="5"><span class="style1">No image in this album</span></td>
  </tr>
    <?php
} else {
    while ($row = mysql_fetch_assoc($result)) {
        extract($row);  
?>
    <tr bgcolor="#FFFFFF"> 
        <td width="30" align="center" bgcolor="#000000"><span class="style1"><?php echo $serial++; ?></span></td>
      <td align="center" bgcolor="#000000"><a href="?page=image-detail&imgId=<?php echo $im_id; ?>" class="style1"><img src="../viewImage.php?type=glthumbnail&name=<?php echo $row['im_thumbnail']; ?>" border="0"><br>
      <?php echo $row['im_title']; ?></a></td>
      <td width="120" align="center" bgcolor="#000000"><?php echo $im_date; ?></td>
      <td width="60" align="center" bgcolor="#000000"><a href="?page=modify-image&imgId=<?php echo $im_id; ?>">Modify</a></td>
      <td width="60" align="center" bgcolor="#000000"><a href="javascript&#058;deleteImage(<?php echo "'$album', $im_id"; ?>);">Delete</a></td>
  </tr>
    <?php
    } // end while
}
?>
    <tr bgcolor="#000000"> 
        <td colspan="5" align="center"> <span class="style1">
        <?php 
$result = mysql_query($sql);
$totalResults = mysql_num_rows($result);    
    
   echo getPagingLink($totalResults, $pageNumber, $imagePerPage, "page=list-image&album=$album");
   ?>
        &nbsp;</span></td>
  </tr>
    <tr bgcolor="#000000">
      <td colspan="5" align="right"><input name="btnAdd" type="button" onclick="window.location.href='index.php?page=add-image&album=<?php echo $album; ?>';" value="Add Image" /></td>
  </tr>
</table>
 
 

veiwImage.php

Code: Select all

 
<?php
/*
    All images in the gallery can be stored outside the
    webroot directory to prevent direct linking.
    To display the image we must provide the image type
    and the image name 
*/
if (!isset($_GET['type']) || !isset($_GET['name'])) {   
    exit;
}
 
// type can be album or gallery image
$type = $_GET['type'];
 
// this is the image name
$name = $_GET['name'];
include 'library/config.php';
 
if ($type == 'album') {
    $filePath = ALBUM_IMG_DIR . $name;
} else if ($type == 'glimage') {
    $filePath = GALLERY_IMG_DIR . $name;
} else if ($type == 'glthumbnail') {
    $filePath = GALLERY_IMG_DIR . 'thumbnail/' . $name;
} else {
    // invalid image type
    exit;
}
 
// the the browser know the file size
header("Content-length: " . filesize($filePath));
 
// the image content type is image/[file extension]
header("Content-type: image/" . substr($name, strpos($name, '.') + 1));
 
// read the image file from the server and send it to the browser
readfile($filePath);
?>
 
 
 

config.php

Code: Select all

 
<?php
 
 
// db properties
$dbhost = '******';
$dbuser = '******';
$dbpass = '****';    
$dbname = *******;
 
// an album can have an image used as thumbnail
// we save the album image here
define('ALBUM_IMG_DIR', '../images/album/');
 
// all images inside an album are stored here
define('GALLERY_IMG_DIR', '../images/gallery/'); 
 
// When we upload an image the thumbnail is created on the fly
// here we set the thumbnail width in pixel. The height will
// be adjusted proportionally
define('THUMBNAIL_WIDTH', 100);
 
// make a connection to mysql here
$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error());
?>
 
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: will not display uploaded image

Post by lukewilkins »

Right click on one of those red x's and find Properties. There you can find out the URL that it is trying to find the image at. Are you storing the images outside your root directory?
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: will not display uploaded image

Post by odie2828 »

When i right click on the x this is what i get as the address (URL)

http://www.clubsohotan.com/image-gallery/viewImage.php?

the images are located in a sub-directory below the file veiwimage.php
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: will not display uploaded image

Post by onion2k »

Your viewImage.php script exits if the 'type' or 'name' variables are missing. They're not specified in the link you just posted. That might be the reason.
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: will not display uploaded image

Post by odie2828 »

if you look at the first post i made, look at the code veiwImage.php line 14 and 17

$type and $name are defined
efficacious
Forum Newbie
Posts: 5
Joined: Wed Aug 13, 2008 2:13 pm

Re: will not display uploaded image

Post by efficacious »

so where along the lines your that url to the viewimage.php page is being put in in place of your files url.

thats why the images aren't showing up.
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: will not display uploaded image

Post by odie2828 »

What do you mean?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: will not display uploaded image

Post by onion2k »

odie2828 wrote:if you look at the first post i made, look at the code veiwImage.php line 14 and 17

$type and $name are defined
http://www.clubsohotan.com/image-gallery/viewImage.php? <- They're not in that URL so the script will exit.
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: will not display uploaded image

Post by odie2828 »

ok now i am confused.

the veiwimage.php file depicts wheather or not the page is trying to display and image of the following type: ablum, image, thumbnail, etc...

if it is calling for a thumbnail then it directs it to the thumbnail folder that is defined by the GALLERY_IMG_DIR in the config.php file.

so when i view and image it is supposed to fine the image first by `type` then by `name`.
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: will not display uploaded image

Post by odie2828 »

this is supposed to be the location of each image..

Code: Select all

<img src="../viewImage.php?type=glthumbnail&name=<?php echo $row['im_thumbnail']; ?>" border="0">
defining the type and name
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: will not display uploaded image

Post by onion2k »

The point is that the URL of the image that you posted (http://www.clubsohotan.com/image-gallery/viewImage.php?) has no GET variables in it. Your viewimage.php script exits if they are missing. That is why you get the broken image.
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: will not display uploaded image

Post by odie2828 »

ok so if i define

$filepath = ($_GET[???])

then it should work?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: will not display uploaded image

Post by onion2k »

It'll only work if the variables are in the URL. Otherwise how is viewimage.php going to know what to display?
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: will not display uploaded image

Post by odie2828 »

http://www.clubsohotan.com/image-galler ... b64994.jpg


that is the url it is trying to get
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: will not display uploaded image

Post by onion2k »

Have you viewed that URL? You've not mentioned those errors before. They might have been helpful. The first one is telling you that the thumbnail file is missing.
Post Reply