Jump to content

display image after uploaded even after refresh


dazzclub

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.