stockton Posted August 7, 2009 Share Posted August 7, 2009 I have the following code, running on a windows server, which works fine but now I need to have a method of removing the image file my script created after the html section has displayed. When this script is used it could be run hundreds of times & we cannot afford having the folder clogged up with a whole lot of image files. The only thing I can think of currently is to use a session variable to store the name of the file so that the next time the script runs that file can be unlinked? Suggestions please. <?php $SGMDBUsername = "???"; $SGMDBPassword = "?????"; $SGMDBName = "???"; $conn=OCILogon($SGMDBUsername,$SGMDBPassword,$SGMDBName); $Image=rand ().".jpg"; $MemberNumber = (isset($_REQUEST['mnumber'])) ? $_REQUEST['mnumber'] : ''; if ($MemberNumber == "") { $FullName="Invalid member number!"; copy ("IMAGES\noface.jpg", $Image); } else { $SQL2="SELECT MEM_SNAME, MEM_FNAME, MEM_TITLE, i.IMAGEDATA FROM MEMBERS, IMAGESTORE i WHERE MEM_FACE = i.id and MEM_NUMBER=$MemberNumber"; $stmt=OCIParse($conn,$SQL2); OCIExecute($stmt); $Count = 0; while ($row=oci_fetch_array($stmt, OCI_ASSOC)) { $Surname = $row['MEM_SNAME']; $FirstName = $row['MEM_FNAME']; $Title = $row['MEM_TITLE']; $Face = $row['IMAGEDATA']; ++$Count; } if ($Count == 0) $FullName="Invalid member number!"; else $FullName=$Title." ".$FirstName." ".$Surname; $fr = fopen($Image, 'w'); fputs($fr, $Face); fclose($fr); OCICommit($conn); OCIFreeStatement($stmt); OCILogoff($conn); } ?> <html> <head> <title>Display Member</title> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="Mon, 22 Jul 2000 11:12:01 GMT"> <link rel="stylesheet" type="text/css" href="style/style.css"> <!--[if IE]> <style type="text/css"> /* place css fixes for all versions of IE in this conditional comment */ .body { zoom: 1; padding-top: 15px; } /* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */ </style> <![endif]--> </head> <body> <center> <H1>Display Member Images.</H1> <br/><br/> </center> <form name="dm" id="dm" method="post" action="DisplayImage.php"> <center> <font color="white"> Member Number: <input type="text" name="mnumber" /><br/><br/> Member Name: <?php echo $FullName ?></font> <center> <INPUT TYPE="submit" name="submit" value="submit"> </center> <br/><br/> <img src=<?php echo $Image ?> border=0/> </form> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/ Share on other sites More sharing options...
Bjom Posted August 7, 2009 Share Posted August 7, 2009 The session approach sounds pretty good, you could adjust it like so: Write your own session handler (which is pretty easy - and there exist a few ok tutorials - if you get stuck pm me). see: session_set_save_handler The session handler has a garbage collector. It runs (chance based) any time that anyone runs your script and looks if there are any session files that are older than they should be - so it is not only running when the user returns. When the session handler deletes a certain session file, have it delete the image connected to it. Maybe link session files and images by naming the image like session_id() . 'jpg'. Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/#findComment-892805 Share on other sites More sharing options...
micmania1 Posted August 7, 2009 Share Posted August 7, 2009 Why not just unlink() at the end of your script? </body> </html> <?php unlink($image); ?> Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/#findComment-892865 Share on other sites More sharing options...
stockton Posted August 7, 2009 Author Share Posted August 7, 2009 The reason I did not is that as the php runs on the server and the html only displays after the php has completed I felt that the image file would have been deleted before the html displayed. Am I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/#findComment-893122 Share on other sites More sharing options...
mikesta707 Posted August 7, 2009 Share Posted August 7, 2009 use PHP to echo the HTML than unlick? Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/#findComment-893124 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2009 Share Posted August 7, 2009 You should not be physically writing the image file just so that you can put it into an <img ....> tag, you should use a php script and dynamically output the correct image when it is requested. Use - <img src="image.php" alt=""> Where image.php is the portion of your php code that determines which image to output and then outputs the correct content type header followed by the image data. You can either use a $_SESSION variable to tell image.php which user's image to get or you could use a $_GET variable on the end of image.php?user=xyz Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/#findComment-893143 Share on other sites More sharing options...
stockton Posted August 8, 2009 Author Share Posted August 8, 2009 PFMaBiSmAd I really would appreciate being shown how to implement your suggestion from an Oracle database containg a blob which is the image. micmania1 & mikesta707 as far as I am aware the php completes way before the clients browser requests the image file from the server & doing it your way will fail as the image file will already have been deleted. Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/#findComment-893491 Share on other sites More sharing options...
PFMaBiSmAd Posted August 8, 2009 Share Posted August 8, 2009 It's your existing code, with the portion needed to get the image from the database copied into its own file. In your existing code, change the <img tag so that it provides mnumber to the script that outputs the correct image - <img src=<?php echo "image.php?mnumber=$MemberNumber"; ?> border=0/> image.php - <?php $SGMDBUsername = "???"; $SGMDBPassword = "?????"; $SGMDBName = "???"; $conn=OCILogon($SGMDBUsername,$SGMDBPassword,$SGMDBName); $MemberNumber = (isset($_REQUEST['mnumber'])) ? $_REQUEST['mnumber'] : ''; if ($MemberNumber == "") { // no member number provided, use noface.jpg header('Content-type: image/jpeg'); readfile("IMAGES\noface.jpg"); } else { $SQL2="SELECT MEM_SNAME, MEM_FNAME, MEM_TITLE, i.IMAGEDATA FROM MEMBERS, IMAGESTORE i WHERE MEM_FACE = i.id and MEM_NUMBER=$MemberNumber"; $stmt=OCIParse($conn,$SQL2); OCIExecute($stmt); $Count = 0; while ($row=oci_fetch_array($stmt, OCI_ASSOC)) { $Face = $row['IMAGEDATA']; ++$Count; } if ($Count == 0) { // no matching row, use noface.jpg header('Content-type: image/jpeg'); readfile("IMAGES\noface.jpg"); } else { // at least one matching row, use data from database header('Content-type: image/jpeg'); // in real-life the content type would come from the database in order to support more than just .jpg files echo $Face; } } OCICommit($conn); OCIFreeStatement($stmt); OCILogoff($conn); ?> P.S. You need to add validation to the external data being put into your queries to prevent sql injection. You should also store the content type of 'IMAGEDATA' in your database so that you can support other image types. Quote Link to comment https://forums.phpfreaks.com/topic/169207-how-to-removeunlink-file-with-random-name/#findComment-893611 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.