Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uploaded image display on browser

Status
Not open for further replies.

cs7536

Programmer
Joined
Apr 20, 2003
Messages
130
Location
US
I have written a script that uploads an image to a directory and the name of the image in a database.

my problem is that I can not get the image to display on a browser, can someone please help.

my code is as followed.

THIS IS MY FORM SCRIPT:

<?
// Name this file as upload.php
print (&quot;<form name='x' enctype='multipart/form-data' method='post' action='uploadresp.php'>&quot;);
print (&quot;<table border=1 align=center>&quot;);
print (&quot;<tr>&quot;);
print (&quot;<td><font face='OCR A Extended'><b>Simple Image Upload</b></td>&quot;);
print (&quot;</tr>&quot;);
print (&quot;<tr>&quot;);
print (&quot;<td><font face='OCR A Extended'>Select Image</font></td><td><input type='file' name='myimage'></td>&quot;);
print (&quot;</tr>&quot;);
print (&quot;<tr>&quot;);
print (&quot;<td></td><td><input type='submit' value=upload></td>&quot;);
print (&quot;</tr>&quot;);
print (&quot;</table>&quot;);
print (&quot;</font>&quot;);
print (&quot;</form>&quot;);
?>

THIS IS MY UPLOAD SCRIPT:

<?
$uploaddir = &quot;images/&quot;;
$username = &quot;Max&quot;;
$email = &quot;cyber@corporatestreet.net&quot;;
$mailsubject=&quot;Image Uploaded&quot;;
$mailsubject2=&quot;Image Upload Failed&quot;;
$Fromname=&quot;Max&quot;;
$Fromaddress=&quot;Max&quot;;
$server=&quot;localhost/&quot;;
//Upload 2 Image -------------------------------------
$db = mysql_connect(&quot;***&quot;, &quot;***&quot;,&quot;***&quot;);
mysql_select_db(&quot;testdb&quot;,$db);
$sql = &quot;INSERT INTO images2 (img_file) VALUES ('$myimage_name')&quot;;
$result = mysql_query($sql);

if(!empty($myimage_name))
{
if(copy($myimage,$uploaddir.$myimage_name));
{
$msg=&quot;Hi Max, the $myimage_name is Uploaded into $uploaddir Directory&quot;;
print ($msg);
if (mail($username.&quot; <&quot;.$email.&quot;>&quot;, $mailsubject, $msg, &quot;From: &quot;.$Fromname.&quot; <&quot;.$Fromaddress.&quot;>\nContent-Type: text/html; charset=iso-8859-1&quot;))
{
print &quot;<br>&quot;;
print (&quot;Confirmation Mail Sent &quot;);
}
else
{
print (&quot;Mail Could not be Delivered&quot;);
}
}
}
else
{
$msg=&quot;Hi Max, Something Went Wrong Image did not Get Uploaded , please use chmod777 to upload directory&quot;;
print ($msg);
if (mail($username.&quot; <&quot;.$email.&quot;>&quot;, $mailsubject2, $msg, &quot;From: &quot;.$Fromname.&quot; <&quot;.$Fromaddress.&quot;>\nContent-Type: text/html; charset=iso-8859-1&quot;))
{
echo &quot;<br>&quot;;
print (&quot;Confirmation Mail Sent &quot;);
}
else
{
print (&quot;Mail Could not be Delivered&quot;);
}
}
?>

MY PROBLEM IS THAT I CAN'T GET THE IMAGE TO DISPLAY ON A BROWSER. I HAVE TRIED TO WRITE A SCRIPT FOR DISPLAYING THE IMAGE BUT I HAVE NO LUCK, I WOULD LIKE TO DISPLAY THE IMAGE BY WAY OF THE ID SO THAT I CAN COMBINE THE IMAGE WITH ARTICLE.
CAN SOMEONE PLEASE HELP WITH THIS PART.

I APPRECIATE YOU IN ADVANCE.
CYBER
 
i followed a different way,
insttead of sending the image along with the mail i uploaded the image to a specific location on the web (on my server). in the mail use html formatting and simply use this code <img src='
hth

Known is handfull, Unknown is worldfull
 
I appreciate your input alot, gives me somthing to think about and also something to fool around with.. I need it to display based on the id, I am trying to figure that out.
 
Here is a trio of files that demonstrate simply what it is I think you're trying to do:

test_upload.html:
Code:
<html>
	<body>
		<form method=post enctype=&quot;multipart/form-data&quot; action=test_upload_store.php>
			<input type=file name=thefile>
			<input type=submit>
		</form>
	</body>
</html>

test_upload_store.php:
Code:
<?php
$imagedir = 'YOUR IMAGES DIRECTORY NAME DOES HERE WITH THE ENDING SLASH';

$dbh = mysql_connect ('localhost', 'test', 'test');

mysql_select_db ('test');

$query = &quot;insert into images (filename) values ('&quot; . $_FILES['thefile']['name'] . &quot;')&quot;;

mysql_query($query);

$result = mysql_insert_id ();

move_uploaded_file ($_FILES['thefile']['tmp_name'], $imagedir . $_FILES['thefile']['name']);

print '
<html>
	<body>
		<a href=&quot;test_fetch_image_by_id.php?imageid=' . $result . '>
			click here
		</a>
	</body>
</html>';

?>

test_fetch_image_by_id.php:
Code:
<?php

mysql_connect ('localhost', 'test', 'test');

mysql_select_db ('test');

$query = 'SELECT filename from images where id = ' . $_GET['imageid'];

$result = mysql_query ($query);

list ($filename) = mysql_fetch_row ($result);

print '<html><body><img src=/test_code/images/' . $filename . '></body></html>';

?>

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks a lot for the script, I can study it for future use. I am having a error that comes up, do you have any idea why?.

Notice: Undefined index: img_id in c:\program files\apache group\apache\htdocs\upload\test_fetch_image_by_id.php on line 14

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\upload\test_fetch_image_by_id.php on line 18
 
14) $query = 'SELECT img_file from images2 where id = ' . $_GET['img_id'];
15)
16) $result = mysql_query ($query);
17)
18)list ($filename) = mysql_fetch_row ($result);

and it generates the error instead of displaying the image.

Thanks
Max
 
The reason for both errors is the same, one directly, one indirectly.

The array index &quot;img_id&quot; does not exist in the array $_GET. That's causing the error in line 14. The fact that the query string is not built correctly causes mysql_query() to return FALSE rather than a resource handle, which generates the error in line 18.

Several things can cause $_GET['img_id'] to not exist:
1. No form is being submitted to the script (or the URL equivalent, as in 2. A form is being submitted, but the name of the form element (case-sensitively) is different.
3. The form is being submitted to the script, but via POST-method. In that case, $_POST['img_id'] will exist, but not $_GET['img_id'].
4. You are running a version of PHP older than 4.1.0, in which case neither $_POST nor $_GET will exist -- instead reference $HTTP_POST_VARS or $HTTP_GET_VARS, respectively.



Want the best answers? Ask the best questions: TANSTAAFL!
 
I got the line 14 to fix, but the line 18 is stil warning:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\upload\test_fetch_image_by_id.php on line 18

this is the code that I am using:

<?php

mysql_connect ('localhost', '***', '***');

mysql_select_db ('**db');

$query = 'SELECT img_file from images2 where id = ' . $_GET['img_id'];

$result = mysql_query ($query);

list ($img_file) = mysql_fetch_row ($result);

print '<html><body><img src=/test_code/images/' . $img_file . '></body></html>';

?>

Looks right to me but still warning hmmmm.
 
The warning is becauser MySQL is barfing on your query.

Without generating an error, mysql_query() returns a FALSE, not a resource handle, when a SELECT query fails.

Use the standard debugging steps:
1. Output the SQL query to the screen. Does it look okay?

2. Paste the query into your preferred MySQL admin tool. Does it return values?

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top