dazzclub Posted August 21, 2013 Share Posted August 21, 2013 Hi there, I'm trying to get my image to still be visible on a page even after I refresh it. I've managed to insert the file name into the database, store it temporarily and then move it to the right folder.. When i upload (or INSERT INTO a table) it, you can instantly see the image, it just disappears when for example I'll press enter in the browsers url or when i refresh it. Here is my current code $target = "img/tees/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $image_file = basename( $_FILES['uploaded']['name']) ; $ok=1; //remember to sanitize $_POST if($_POST['upload']){ //This is our size condition if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; } //This is our limit file type condition if ($uploaded_type =="text/php") { echo "No PHP files<br>"; $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0) { Echo "Sorry your file was not uploaded"; } //If everything is ok we try to upload it else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded"; $display_image = '<img src="'.$target.'" title="" alt="" />'; global $dbc; $sql = "INSERT INTO full_range (image) VALUES ('$image_file')"; $result = mysqli_query($dbc, $sql) or trigger_error("SQL", E_USER_ERROR); if($result) { echo 'All done'; } } else { echo "Sorry, there was a problem uploading your file."; } } } And here is the code that displays the image file that's been inserted... echo '<img src="img/tees'.$image_file.'" />'; I've read on the internet that I may need to do pass an img variable to another page such as, display.php?img=name_of_img and the have that in the image src file... <img src="display.php?img=name_of_img" /> but I think I could be getting ahead of myself and making a mountain out of a mole hill.. Any pointers to what I'm doing wrong would be great Quote Link to comment https://forums.phpfreaks.com/topic/281446-display-image-after-uploaded-even-after-refresh/ Share on other sites More sharing options...
mac_gyver Posted August 22, 2013 Share Posted August 22, 2013 echo '<img src="img/tees'.$image_file.'" />'; did you look at the 'view source' of the output from the above statement in your browser and make sure it is correct? it's likely missing a / Quote Link to comment https://forums.phpfreaks.com/topic/281446-display-image-after-uploaded-even-after-refresh/#findComment-1446202 Share on other sites More sharing options...
dazzclub Posted August 22, 2013 Author Share Posted August 22, 2013 Hi mac_guyver, That was a typing error on my behalf...so now this particular code looks like.. echo '<img src="img/tees/'.$image_file.'" />'; Now, it uploads and you can see the image but disappears when i refresh the page....I'm thinking the problem is how i laid out the code and in what order..I'm guessing here..maybe $image_file holds the file name just for the upload only? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281446-display-image-after-uploaded-even-after-refresh/#findComment-1446249 Share on other sites More sharing options...
dazzclub Posted August 23, 2013 Author Share Posted August 23, 2013 I think the problem has been was that the variable i need to echo out the image was only set after i uploaded the file only. So obviously when i refresh the page it's looking at the variable and it's empty, because nothing has been uploaded.. so my first solution, because it's not displaying after refresh, is to query the database to retrieve the most recent INSERT via ID... I think having the ID stops two people retrieving the same image? If not, I'll have to query the database and use their username has a way of identifying their own recent INSERT.. Well, that's my thinking.. Quote Link to comment https://forums.phpfreaks.com/topic/281446-display-image-after-uploaded-even-after-refresh/#findComment-1446450 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.