gsaldutti Posted November 23, 2008 Share Posted November 23, 2008 This isn't completely a PHP question, but you guys are geniuses so I figured I'd ask and see if any of you knew... I have a PHP page that has several forms on it that upload images to my server. The action of all forms is to reload the same page the forms are on. As soon as the images are uploaded, the images appear on the user's browser (It's a simple script to put the image into a folder on my server and a simple echo of the file name to display it). This part works fine on both IE and Firefox. However, I have a "remove image" script as well and on IE only, when the user clicks "Remove image", the image is completely removed from my server fine (i.e. it truly no longer exists on my server), but the image still shows in the IE browser (Firefox works fine, meaning as soon as the user clicks "Remove image", the image disappears completely, as it should since it no longer exists on my server). If the IE user refreshes the page, then the image finally goes away, but I can't expect them do that. Is there a way to make IE work just like Firefox in respect to images disappearing if they truly don't exist, or is there some way to make a page refresh itself immediately after the page reloads with PHP? It just seems ridiculous that IE continues to display an image that no longer exists. Any thoughts would be appreciated greatly. Thank you... Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 can you post code i have a script kinda like this that works fine in ie.. so im wondering what your code looks like Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 it's very likely that IE is just displaying a cache'd copy of the image. Try using this line any place that causes the page to change: header('Pragma: No-cache'); BEFORE any content is sent to the browser that is. If that doesn't fix the issue I guess you could put a couple second sleep in the script. Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 Sure, my code is below. FYI, I'm VERY new to PHP so I'm sure my code is not even close to being as efficient as it can be, so don't laugh too hard .... First, this is what my image upload form looks like... <!-- START Upload Form #1-------------------------------------------> <tr> <td width=300 height=200> <?php if ($isfile1=="yes") { ?> <form action="fillscript.php" method="POST"> <input type="submit" name="submit" value="Remove Image 1"> (So you can add a different image) </form> <?php } else { ?> <form action="fillscript.php" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="5000000"> Upload Image: <input type="file" name="userfile" > <font size="1">Click browse to upload a file</font> <input type="submit" name="submit" value="Upload Image 1"> </form> <?php } ?> </td> <td width=400 height=200><img src="http://localhost/pas/upload/<?php echo $file1name; ?>" /> </td> </tr> <!-- END Upload Form #1------------------------------------------> And here is the code that removes the image when they click the "Remove Image 1" button above... if (isset($_POST['submit']) && $_POST['submit'] == "Remove Image 1" ) { //to delete image from the UPLOAD folder $removeimage = unlink("upload/$file1name"); if($removeimage) { $removemessage = "Actual Image was deleted from UPLOAD folder"; $isfile1="no"; } else { $removemessage = "Actual Image was NOT deleted!"; } } Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 Hey Genericnumber1, Do I just put that header at the top of the page, or does it need to go in the script that removes the actual image from my server? That's kind of rhetorical because I actually tried it in both spots and neither worked. What's even weirder that I forgot to mention is that in IE, I upload an image (we'll call it IMAGE-A) and then I hit "Remove Image" and IMAGE-A is removed from the server but it still shows in the browser. THEN, I upload another different image (we'll call it IMAGE-B) and it STILL SHOWS IMAGE-A in the browser, even though the only image on my server is IMAGE-B. I just don't get it. Makes no sense. Anyway, can you elaborate a bit on that "sleep" thing you mentioned? I've never heard of that but I'm willing to try anything. Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 top of the page all it does is block cacheing of the page but yeah all php headers need to be on the top of the page before any content (including whitespace are displayed to the browser Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 Yeah, if it's still showing imageA after it's been deleted then it's the browser cache. You say adding the header to the top didn't fix it? Crazy IE causing problems! Try this one instead: header("Cache-Control: no-cache, must-revalidate"); Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 That one didn't work either. Is there some way to test that the cache is not being used (i.e. to test that your header is doing it's job)? I totally trust that it should be doing what you say, but if I can confirm that it's not using the cache, then I'll know there must be something else in my code causing the issue. But like you said, it sounds like it's gotta be the cache causing the problem. I wish there was some PHP code that would recognize if someone was using IE and automatically close their IE, automatically install Firefox on their machine, and automatically re-open my site in Firefox Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 that code sounds awesome and kinda illegal but i want it to exist damnit. my code works different in the fact that it submits to a different page then redirects back therfor refreshing the page Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 Do ctrl-f5 once on the page and then try to test it, perhaps it's your current cache that's being problematic. Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 Still no luck. MAN I HATE IE. I guess I'll have to submit all my upload-image forms to a different page and then redirect back (like dropfaiths) but I don't know if that is feasible with the way I want the site to work. Guess the only way I'll know is to try it. Hey Dropfaith, does your "other page" ever display in the user's browser, or does it simply process data and ALWAYS redirect back to the original page? I am using SESSIONS by the way, not sure if that matters Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 Perhaps try the meta tag <meta http-equiv="pragma" content="no-cache" /> If that doesn't fix the issue I guess we SHOULD consider other possible issues. Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 The meta tag didn't work either. Thanks to both of you for all the suggestions. I think I might be screwed Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 its ugly but im working on alot of my scripts for this site still my example of my page that deletes it never displays anything to the user just deletes and redirects <?php session_start( ); //bored of this function yet? if(!isset($_SESSION["username"])){ header('Location: /user/index.php'); } else { header('Location: /user/index.php'); ?> <? // includes include("../header.php"); // open database connection $conn = mysql_connect(HOST, DBUSER, PASS) or die('Could not connect !<br />Please contact the site\'s administrator.'); $db = mysql_select_db(DB) or die('Could not connect to database !<br />Please contact the site\'s administrator.'); // connects to db above $Id = mysql_escape_string(trim(htmlentities($_GET['Id']))); $result = mysql_query("SELECT * FROM Artistimages WHERE Id='$Id'") or die(mysql_error()); $row = mysql_fetch_object($result); $destination = '/home/www/lawrenceguide.org/www/art/'; $uploaded = $row->uploaded; //assume that this is the image_name field from your database //unlink function return bool so you can use it as conditon if(unlink($destination.$uploaded)){ } $query = mysql_query("DELETE FROM Artistimages WHERE Id='$Id'") or die(mysql_error()); ?> <?php } ?> Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 I'm sure this is a dumb question but your code appears to redirect the user BEFORE the script to delete the image runs. It's probably PHP 101, but I'm guessing ALL code on the page runs even after the redirect. Is that right? Or am I missing something else? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 headers must be sent before any content, so his header redirect must be up there or an error will occur. Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 right it redirects but the code still does its job i also have it set to redirect if the session doesnt exist which for deleting is a good idea to use.. and on idex.php if session doesnt exist it shows the login form Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 FYI, I created a new, separate page called "deleteimage.php" which does nothing but delete the image from my server and redirect back to the main page and it STILL will not work. The old images that are nowhere to be found on my server still display in the IE browser. Unbelievable. Anyway, thanks for all your help up to this point. Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 got a link i want to see exactly whats going on i know with php i wont see code but i cant grasp why it wont work even with the new page that should act as a page refresh Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 23, 2008 Share Posted November 23, 2008 Agreed, try it out for us dropfaith. I don't have IE on this comp so I can't test it. It certainly is an odd problem and I'm still suspicious that it is some kind of cache. Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 Do you mean a link to actually do this on a public website? (I assume that's what you mean but just checking). I'm only doing this on my local machine so far but let me see if I can upload the files to my website.... Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 you might find uploading it to fix this issue local machines act funny Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 I hope it's that easy of a solution. Anyway, I uploaded my stuff to http://gdoot.com/imagetest/uploadimage.php but when I try to upload, naturally I get an error: Fatal error: Call to undefined function imagecreatefromjpeg() in /home/gdootcom/public_html/imagetest/uploadimage.php on line 50 I think that means my server doesn't have a certain image library or something, right? That could be a problem (unless it's easy to get it installed on the server). Any thoughts? ***Disregard the stuff above the Table. That's just testing stuff for my own reference Quote Link to comment Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 hrmm can you post the entire file for us... Quote Link to comment Share on other sites More sharing options...
gsaldutti Posted November 23, 2008 Author Share Posted November 23, 2008 I think that GD library thing is NOT enabled on my server, so I think I gotta figure out how to get that enabled (I made the entire PHPINFO show on that link I gave you earlier and I don't see "GD" anywhere). Anyway, here is the entire file (I tried to take out all the repetitive stuff to make it easier to browse through). Again, this is the only file and the action of the upload form is the page itself.... <?php header("Cache-Control: no-cache, must-revalidate"); require_once("includes/session.php"); require_once("includes/connection.php"); $success=""; $message=""; $badinsert=""; $deletemessage=""; $imagemessage=""; $removemessage=""; $wrongfiletype1=""; $nofilechosen1=""; $sessionid = session_id(); $file1name = "0-" . $sessionid . ".jpg"; if (isset($_POST['submit']) && $_POST['submit'] == "Upload Image 1") { if ($_FILES['userfile']['type'] == "image/pjpeg" || $_FILES['userfile']['type'] == "image/jpeg" ) { // This is the temporary file created by PHP $uploadedfile = $_FILES['userfile']['tmp_name']; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture the original size of the uploaded image list($width,$height) = getimagesize($uploadedfile); // For our purposes, I have resized the image to be // 600 pixels wide, and maintain the original aspect // ratio. This prevents the image from being "stretched" // or "squashed". If you prefer some max width other than // 600, simply change the $newwidth variable $newheight=200; $newwidth=($width/$height)*200; $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // now write the resized image to disk. I have assumed that you want the // resized, uploaded image file to reside in the ./images subdirectory. $imagefolder = "upload/0-". $sessionid . ".jpg"; if (imagejpeg($tmp,$imagefolder,100)) { $imagemessage = "File is valid, and was successfully uploaded into FOLDER.\n"; $success="yes"; } else { $imagemessage = "Possible file upload attack!\n"; $success="no"; } imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request // has completed. $isfile1 = "yes"; $filesize = $_FILES['userfile']['size']; } elseif ($_FILES['userfile']['type'] == "") { $nofilechosen1 = "You did not select a file"; } else { $wrongfiletype1 = "This image is not a JPEG image!!!!!!!!!"; } if($success=="yes"){ $query = "INSERT INTO images ( name, size ) VALUES ('" . $file1name . "','" . $filesize . "')"; $result = mysql_query($query, $connection); if ($result) { $message = "<p>The image was successfully submitted into the DATABASE. </p>"; } else { $message = "The image was NOT submitted."; $message .= "<br />" . mysql_error(); } } else { $badinsert = "<p>Didnt work!</p>"; } } else { } if (isset($_POST['submit']) && $_POST['submit'] == "Remove Image 1" ) { //need to delete image from the UPLOAD folder $removeimage = unlink("upload/$file1name"); if($removeimage) { $removemessage = "Actual Image was deleted from UPLOAD folder"; $isfile1="no"; } else { $removemessage = "Actual Image was NOT deleted!"; } //need to delete image info from the DATABASE $query = "DELETE FROM images WHERE name = '$file1name'"; $result = mysql_query($query, $connection); if ($result) { $deletemessage = "<p>The image was successfully DELETED. </p>"; } else { $deletemessage = "The image was NOT deleted."; $deletemessage .= "<br />" . mysql_error(); } } else { } if (file_exists("upload/$file1name")){ $isfile1 = "yes"; } else { $isfile1 = "no"; } ?> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <title>Fill Script</title> <link href="smile.css" type="text/css" rel="stylesheet"> </head> <body> <div id="entirepage"> <?php echo $sessionid; echo "SUCCESS=" . $success; echo $imagemessage; echo $message; echo $badinsert; echo $deletemessage; echo $removemessage; echo "File 1 =" . $isfile1 . "<br>"; ?> <table WIDTH="980" CELLPADDING=5 CELLSPACING=5 BORDER=1 ALIGN=left> <tr > <td colspan=3>blah blah blah </td> </tr> <!-- START Upload Form #1-------------------------------------------> <tr> <td width=300 height=200> <?php if ($isfile1=="yes") { ?> <form action="uploadimage.php" method="POST"> <input type="submit" name="submit" value="Remove Image 1"> (So you can add a different image) </form> <?php } else { ?> <form action="uploadimage.php" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="5000000"> Upload Image: <input type="file" name="userfile" > <font size="1">Click browse to upload a file</font> <input type="submit" name="submit" value="Upload Image 1"> </form> <?php echo "<span class='error'>" . $nofilechosen1 . "</span>"; echo "<span class='error'>" . $wrongfiletype1 . "</span>"; } ?> </td> <td width=400 height=200><img src="http://localhost/pas/upload/<?php echo $file1name; ?>" /> </td> </tr> <!-- END Upload Form #1------------------------------------------> </table> <?php phpinfo(); ?> </div> </body> </html> <?php require_once("includes/footer.php"); ?> Quote Link to comment 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.