Re: Saving a BLOB image to a file (SOLVED)

From: Date: Sat, 18 Feb 2006 13:16:46 +0000
Subject: Re: Saving a BLOB image to a file (SOLVED)
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-230685@lists.php.net to get a copy of this message
On 2/16/06, tedd <tedd@sperling.com> wrote:
However, after given it some thought, I would imagine that one could get a unique generated string, create a file with that string, give the file the correct permissions, then pull the image from the dB and save, do image processing, and then reload the image into a web page, and delete the file without any conflict. I can envision lot's of users doing that simultaneously without problems. BUT, I haven't tried it yet!
I have used a user's IP address (dots removed) and microtime appended to create a unique file name. The target directory is world writable. Not the most elegant solution... I found that sometimes the file had not been written yet and trying to display it on a page right away resulted in file not found... -- Gerry
Gerry: Above, I said that I haven't tried it yet, but I just finished the code and it works well enough, I think. The technique is pretty simple -- just create a tmp directory with 777 permissions and then run this code. <?php $token = md5(uniqid(1)); $token .= ".txt"; $filename = "tmp/$token"; $contents = "This is the content of the file."; echo("<hr><br/>Writing file <br/><br/>"); $file = fopen( $filename, "w" ); fwrite( $file, $contents); fclose( $file ); if( file_exists( $filename ) ) { $file_length = filesize( $filename ); echo("File created at $filename<br/> "); echo( "File size: $file_length bytes<br/>"); echo( "<br/>$contents<br/>" ); } else { echo( "<br/>Unable to create file<br/>" ); } echo("<hr><br/>Reading file:<br/><br/>"); $filesize = filesize($filename); $file = fopen( $filename, "r" ); $text = fread( $file, $filesize ); fclose( $file ); echo( "File read at: $filename<br/> "); echo( "File Size: $filesize bytes<br/>" ); echo( "<br/>$text<br/>" ); echo("<hr><br/>Deleting file <br/>"); $return = unlink($filename); if($return) { echo("<br/>Successful delete of: $filename<br/>"); } else { echo("<br/>Failed to delete of: $filename<br/>"); } ?> -- -------------------------------------------------------------------------------- http://sperling.com/

« previous php.general (#230685) next »