ReeceSayer Posted March 12, 2010 Share Posted March 12, 2010 OK so i have this code which i've found after browsing the forums and i understand all of it however, I'm having an issue of my own with what i want to do with the result. Basically i need to retreive a filename "username.jpeg" from a MYSQL database and then place image tags around it so it will display the image for that user as the avatar. I'm not very good at explaining my problem so i'll post the code: <?php session_start(); db_connect(); if(!$_POST['uploaded']){ /** * DISPLAY FORM */ ?> <form action="" method="post" enctype="multipart/form-data"> Upload:<br><br> <input type="file" name="image"><br><br> <input type="hidden" name="uploaded" value="1"> <input type="submit" value="Upload"> </form> <?php }else{ // First line of error checking, make sure upload successful if($_FILES['image']['error'] !== UPLOAD_ERR_OK){ echo 'Error: There was an error with your upload.'; exit(); } // Now we know we have an uploaded file, let's check the type $image_size = getimagesize($_FILES['image']['tmp_name']); if($image_size === FALSE){ echo 'Error: getimagesize() has failed.'; exit(); } $img_arr = explode('/', $image_size['mime']); if(count($img_arr) != 2){ echo 'Error: Problem with image type'; exit(); } $type = $img_arr[0]; // mime type $ext = $img_arr[1]; // file extension if(strlen($type) == 0 || strlen($ext) == 0){ // Neither of those vars can be zero length echo 'Error: No type or extension found'; exit(); } if($type != 'image'){ // Not an image! echo 'Error: Uploaded file was not an image'; exit(); } //get users ID $id = $_SESSION['username']; //don't continue if an image hasn't been uploaded if(is_uploaded_file($_FILES['image']['tmp_name'])){ // set destination directory $dst = realpath(dirname(__FILE__) . '/avatars') . '/' . $id . '.' . $ext; if(move_uploaded_file($_FILES['image']['tmp_name'], $dst)){ // Success, file has been moved and renamed // now do whatever else is necessary $query = "SELECT username,avatar FROM `users` WHERE username='{$_SESSION['username']}'"; $query2 = "UPDATE users SET avatar = '$id.jpg' WHERE username = '{$_SESSION['username']}'"; $result = mysql_query($query); $result = mysql_query($query2); echo "Congratulations you have successfully uploaded " . $_SESSION['username'] .".jpeg as your avatar<br />"; echo "<img src='../avatars/'" . $_SESSION['username'] . "'.jpeg' width='526' height='45' />"; echo "/avatars/" . $_SESSION['username'] . ".jpeg"; } /* Everything else you had is irrelevant at this point */ } } ?> If you can just point me in the right direction or show me what needs doing i will be eternally greatful as i've been searching google and messing with it for about an hour now Thanks in advance Reece Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/ Share on other sites More sharing options...
nafetski Posted March 12, 2010 Share Posted March 12, 2010 Well, obviously you know in order to show an image you have to have it on your filesystem somewhere. What you'll do is <img src="images/imagepath/<?php echo $imagenamefromdatabase" /> Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025033 Share on other sites More sharing options...
aleX_hill Posted March 12, 2010 Share Posted March 12, 2010 Take a look at these two lines: $query2 = "UPDATE users SET avatar = '$id.jpg' WHERE username = '{$_SESSION['username']}'"; echo "<img src='../avatars/'" . $_SESSION['username'] . "'.jpeg' width='526' height='45' />"; Notice something different with the extensions? I see a mystery "e" appearing in there. Check your file system to make sure the file actually exists, and whether it should have the .jpg or .jpeg extension Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025035 Share on other sites More sharing options...
ReeceSayer Posted March 12, 2010 Author Share Posted March 12, 2010 Yeah thanks for both of the replies. I've corrected the case of the missing "e" but still not having any of it. I thought as the images are being renamed to the $_SESSION['username'] i could simple add that with the extension.jpeg to produce my image? i had it before including title, longdesc etc but all i got was the title but no picture? Still unsure where i'm going wrong. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025037 Share on other sites More sharing options...
aleX_hill Posted March 12, 2010 Share Posted March 12, 2010 Firstly: Are the files sitting in your server? Secondly, we cannot assume the .jpg or .jpeg extension, it is based on the uploaded file (if it is .jpg, it should be .jpg, if it is .jpeg it will be .jpeg, if it is a .gif it will be .gif etc) Thirdly, the files should in in /avatars/file.extension based on the script which uploads it. So if your script is: http://myplace.com/scripts/myscript.php Then you need to look in http://myplace.com/scripts/avatars/file.extension When you are trying to show the image you are using img src='../avatars/'" . $_SESSION['username'] . "'.jpeg' width='526' height='45' /> which would point to http://myplace.com/avatars/file.extension I believe. Check the source code of your outputted file and see where it is trying to read the file from. It might be as simple as removing the "../" from the src attribute. Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025040 Share on other sites More sharing options...
ReeceSayer Posted March 12, 2010 Author Share Posted March 12, 2010 Yes files are on my server as such, i'm using XAMPP to build the site at the minute. I'm not sure why but with everyfile i've tested uploading .jpg .jpeg .png they have all been converted to .jpeg before being stored on the server. The avatars folder is essentiall sitting in: http://myplace.com/htdocs/avatars/file.extension With my main site in: http://myplace.com/htdocs/ I've also removed the .. from the ../ but still not working. Could it be because im including the script in a main webpage? Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025042 Share on other sites More sharing options...
aleX_hill Posted March 12, 2010 Share Posted March 12, 2010 It could be. What happens (from memory, someone correct me if I am wrong) when you include a file using the include() fuinction is it basically grabs the code of that file and dumps it into the file that called it before being parsed. So: MainFile.php (http://mysite.com) include("includes/some/file/path.php"); With includes/come/file/path.php (http://mysite.com/includes/some/file/path.php) <img src="myPicture.jpg" /> Will look for http://mysite.com/myPicture.jpg NOT http://mysite.com/includes/some/file/myPicture.jpg Have you tried removing the "../" form the filepath and not just the ".." ? What does the img tag say when you check check the source of the page? Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025043 Share on other sites More sharing options...
ReeceSayer Posted March 12, 2010 Author Share Posted March 12, 2010 Well i thought an easy way to test if it was the include would be to add the same file to my main directory: http://mysite.com/myPicture.jpg Still not working, i even took the avatar/ part out of the img tag so it would search directly in the main folder. I think there must be something wrong with using sessions within img tags. I think my next step would be to try and select a specific row out of the database. I'll try that tomorrow. Not sure where you are but it's getting late over here. Thanks alot for the help though really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025048 Share on other sites More sharing options...
ReeceSayer Posted March 12, 2010 Author Share Posted March 12, 2010 Actually forget that. I thought i'd give it one last shot before bed. I checked the source code and where the code was like this before: echo "<img src=" . $_SESSION['username'] . "'.jpeg' width='526' height='45' />"; when viewing the cource of the image i got this: htdocs/avatar/'myimage'.jpeg Simply removing the single quotes and making the code look like this: echo "<img src=" . $_SESSION['username'] . ".jpeg width='526' height='45' />"; Works perfectly. Thanks alot mate can sleep easy now lol. Reece Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025049 Share on other sites More sharing options...
aleX_hill Posted March 12, 2010 Share Posted March 12, 2010 Kicking myself that I didnt see that now. That is why I was asking you to check the source code of the output page, it would have shown the problem. Glad you sorted it out. Quote Link to comment https://forums.phpfreaks.com/topic/194971-displaying-image-based-on-filename-stored-in-mysql-db/#findComment-1025051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.