why can't I view the 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

Post Reply
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

why can't I view the image?

Post by cybergirl »

Can't get the images to show up. I don't know how to make it show the directory

here's the problem area:

Code: Select all

<a href="javascript:viewLargeImage('<?php echo $image['tourimage01']; ?>')"><img src="../viewImage.php?type=tourthumbnail&name=<?php echo $image['tourimage01thumb']; ?>" border="0" /></a><br />
Here is the full page

Code: Select all

<?php
include 'config.php';
include 'opendb.php';
?>

<?php

// make sure the image id is present
if (!isset($_GET['tourid'])) {
	echo "Image id is not defined";
} else {
	
	// get the image id
	$tourid = $_GET['tourid'];

	if (isset($_POST[''])) {
		$company  = $_POST[''];
		$description   = $_POST['description'];
		
		if ($_FILES['images']['tmp_name'] != '') {
			$images = uploadImage('images', TOUR_IMG_DIR);
		
			if ($images['image'] == '' && $images['thumbnail'] == '') {
				echo "Error uploading file";
				exit;
			}

			$image     = "'" . $images['image'] . "'";
			$thumbnail = "'" . $images['thumbnail'] . "'";

			$sql  = "SELECT tourimage01, tourimage01thumb
					 FROM tours 
					 WHERE tourid = $tourid";
			
			$result = mysql_query($sql) or die('Error, get image info failed. ' . mysql_error());
			$row = mysql_fetch_assoc($result);
			unlink(TOUR_IMG_DIR . $row['tourimage01']);
			unlink(TOUR_IMG_DIR . $row['tourimage01thumb']);
		} else {
			// the old image is not replaced
			$image     = "tourimage01";
			$thumbnail = "tourimage01thumb";
		}

			
		$sql = "UPDATE tours 
				SET company = '$company', 
					description = '$description', 
					tourimage01 = $image, 
					tourimage01thumb = $thumbnail, 
			    	WHERE tourid = $tourid";
	
		mysql_query($sql) or die('Error, update image failed : ' . mysql_error());                    
		
		echo "<script>window.location.href='index.php?page=image-detail&tourid=$tourid';</script>";
	} else {

		
		$sql  = "SELECT tourid, company, description, tourimage01, tourimage01thumb
				 FROM tours 
				 WHERE tourid = $tourid";
		
		$result = mysql_query($sql) or die('Error, get image info failed. ' . mysql_error());
		
		if (mysql_num_rows($result) == 0) {
			// can't find an image with that id
		?>
<p align="center">Image not found. Click <a href="index.php?page=list-image">here</a> 
    to go to the image list</p>
<?php	
		} else {	
			$image = mysql_fetch_assoc($result);	
	
		
?>
<form action="" method="post" enctype="multipart/form-data" name="frmAlbum" id="frmAlbum">
    <table width="100%" border="0" cellpadding="2" cellspacing="1" class="table_grey">
        
        <tr> 
            <th width="150">Image Title</th>
            <td width="80"> <input name="company" type="text" id="company" value="<?php echo $image['company']; ?>" size="40" maxlength="64"></td>
        </tr>
        <tr> 
            <th width="150">Description</th>
            <td> <textarea name="mtxDesc" cols="50" rows="10" id="mtxDesc"><?php echo htmlspecialchars($image['description']); ?></textarea>            </td>
        </tr>
        <tr> 
            <th width="150">Image</th>
            <td><?php if ($image['tourimage01thumb'] != '') { ; ?>
              <a href="javascript:viewLargeImage('<?php echo $image['tourimage01']; ?>')"><img src="../viewImage.php?type=tourthumbnail&name=<?php echo $image['tourimage01thumb']; ?>" border="0" /></a><br />
			  
			
			
              <?php } ?>
          <input name="images" type="file" class="box" id="images2"></td>
        </tr>
        <tr> 
            <td width="150">&nbsp;</td>
            <td> <input name="btnModify" type="submit" class="box" id="btnModify" value="Modify Image"> 
                <input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.history.back();">            </td>
        </tr>
    </table>
</form>
<?php
		}
	}
}

?>
AND HERE Is THE CONFIG FILE

Code: Select all

<?php 
// hostname or ip of server
$servername='localhost';

// username and password to log onto db server
$dbusername='username';
$dbpassword='password';

// name of database
$dbname='name';


// all tour images iare stored here
define('TOUR_IMG_DIR', '/home/dtours3/public_html/tours/tourimages/'); 

// 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);

?>



Please note that I took away the db info in this post, however I have it in the file I'm using and it works.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Re: why can't I view the image?

Post by TheMoose »

cybergirl wrote:Can't get the images to show up. I don't know how to make it show the directory

here's the problem area:

Code: Select all

<a href="javascript:viewLargeImage('<?php echo $image['tourimage01']; ?>')"><img src="../viewImage.php?type=tourthumbnail&name=<?php echo $image['tourimage01thumb']; ?>" border="0" /></a><br />
Is this page in the same directory as viewImage.php? You're telling the image to look in the directory above the one you're currently in to find the viewImage.php script.
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

Thanks for pointing that out. I'll look
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

this is what I put but it's still not working:

view-image.php

Code: Select all

<?php

if (!isset($_GET['type']) || !isset($_GET['name'])) {	
	exit;
}

// type can be an image
$type = $_GET['type'];

// this is the image name
$name = $_GET['name'];
include 'config.php';

if ($type == 'image') {
	$filePath = TOUR_IMG_DIR . $name;
} else if ($type == 'thumbnail') {
	$filePath = TOUR_IMG_DIR . $name;
} else {
	// invalid image type
	exit;
}

// let 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);
?>
and I changed the code here on the tour-modify.php page:

Code: Select all

<td><?php if ($image['tourimage01thumb'] != '') { ; ?>
              <a href="javascript:viewLargeImage('<?php echo $image['tourimage01']; ?>')"><img src="view-image.php?type=thumbnail&name=<?php echo $image['tourimage01thumb']; ?>" border="0" /></a><br />
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

You're probably getting a "...headers already sent..." warning that gets dumped as image data. To make sure this doesn't happen you can either make sure you are not outputting anything to the client before sending the headers (don't forget to check config.php), or use output buffering. And don't forget to check if the image file exists and output a default image if it doesn't.

/josa
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

I'm not getting any warnings, and I know for fact the images exist. here's what it looks like:

http://3dtours.org/tour-modify.php?tourid=4
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

Well, I checked the image src attribute:

Code: Select all

<img src="view-image.php?type=tourthumbnail&name=jan03-150.jpg" border="0" />
should be:

Code: Select all

<img src="view-image.php?type=thumbnail&name=jan03-150.jpg" border="0" />
/josa
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

Actually I did that already. I ended up doing this:

$mimes = array("jpg" => "jpeg","gif" => "gif");
header("Content-type: image/" . $mimes[substr($name, strpos($name, '.') + 1)]);

But I still need to know it I can modify several images at once in one form, but not necessarily change all of them just to have that ability, as well as the data alongside. Am I heading the right direction, I feel lost. (I'm a real beginner). I have hardly a clue as to what I'm doing, all I am left with is, looking up code online, reading books, and I really need to get helped through this.
Post Reply