Re: [PHP] Saving a BLOB image to a file (SOLVED)

2006-02-18 Thread tedd

On 2/16/06, tedd [EMAIL PROTECTED] 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(hrbr/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 $filenamebr/ );
echo( File size: $file_length bytesbr/);
echo( br/$contentsbr/ );
}
else
{
echo( br/Unable to create filebr/ );
}

echo(hrbr/Reading file:br/br/);

$filesize = filesize($filename);
$file = fopen( $filename, r );
$text = fread( $file, $filesize );
fclose( $file );

echo( File read at: $filenamebr/ );
echo( File Size: $filesize bytesbr/ );
echo( br/$textbr/ );

echo(hrbr/Deleting file br/);

$return = unlink($filename);
if($return)
{
echo(br/Successful delete of: $filenamebr/);
}
else
{
echo(br/Failed to delete of: $filenamebr/);
}

?
--

http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Saving a BLOB image to a file (SOLVED)

2006-02-17 Thread Gerry Danen
On 2/16/06, tedd [EMAIL PROTECTED] 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... Rendering the image to the screen
without saving to a file works best for me.

--
Gerry
http://portal.danen.org/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Saving a BLOB image to a file (SOLVED)

2006-02-16 Thread Gerry Danen
What if several users try to access a different picture at the same
time? Won't that render the wrong image for some?

Gerry

On 2/16/06, tedd [EMAIL PROTECTED] wrote:
 Hi gang:

 In a previous post (i.e., [PHP] Strange response to MySQL query) I
 was trying to save a BLOB as a file, namely using an INTO DUMPFILE
 query.

 If you are the system admin, and have all the system permissions,
 then this is not a big problem. However, if you aren't, then getting
 MySQL to dump a file becomes very problematic with respect to getting
 the right permissions to accomplish the task.

 So, I found a way around it. I am sure that others have thought of
 this before me, so I'm not claiming credit and I am sure that this
 solution is obvious to some -- but, it's just that I searched
 literally hundreds of posts and spent considerable time without
 finding an answer.  The following is the result of my tinkering. So
 here goes:

 First, on your site create a file called my_image.png

 Second, give it 777 permissions.

 Third, in your code after opening the dB, grab the image from MySQL:

 $dbQuery = SELECT image;
 $dbQuery .= FROM $your_table ;
 $dbQuery .= WHERE image_Id = $pageNum;

 $result = mysql_query($dbQuery) or die(Error: .mysql_error());

 Forth, drop the image into a variable:

 $fileContent = @mysql_result($result, 0, image);

 Fifth, create an image from a string (a great function here):

 $original=imagecreatefromstring($fileContent);

 Sixth, save the image.

 imagepng($original, my_image.png);

 And Bingo, the image is now the my_image.png file.

 Any comments or suggestions are welcomed.

 tedd

 --
 
 http://sperling.com/

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




--
Gerry
http://portal.danen.org/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Saving a BLOB image to a file (SOLVED)

2006-02-16 Thread tedd

Gerry wrote:


What if several users try to access a different picture at the same
time? Won't that render the wrong image for some?

Gerry



Gerry:

Normally, it's not needed for civilians. This is for when YOU want to 
do some image stuff on your images, like to do some background 
processing via your code.


As for users, they just want to see the image and you can pull that 
out of your dB or files, whichever you prefer.


tedd

---




On 2/16/06, tedd [EMAIL PROTECTED] wrote:

 Hi gang:

 In a previous post (i.e., [PHP] Strange response to MySQL query) I
 was trying to save a BLOB as a file, namely using an INTO DUMPFILE
 query.

 If you are the system admin, and have all the system permissions,
 then this is not a big problem. However, if you aren't, then getting
 MySQL to dump a file becomes very problematic with respect to getting
 the right permissions to accomplish the task.

 So, I found a way around it. I am sure that others have thought of
 this before me, so I'm not claiming credit and I am sure that this
 solution is obvious to some -- but, it's just that I searched
 literally hundreds of posts and spent considerable time without
 finding an answer.  The following is the result of my tinkering. So
 here goes:

 First, on your site create a file called my_image.png

 Second, give it 777 permissions.

 Third, in your code after opening the dB, grab the image from MySQL:

 $dbQuery = SELECT image;
 $dbQuery .= FROM $your_table ;
 $dbQuery .= WHERE image_Id = $pageNum;

 $result = mysql_query($dbQuery) or die(Error: .mysql_error());

 Forth, drop the image into a variable:

 $fileContent = @mysql_result($result, 0, image);

 Fifth, create an image from a string (a great function here):

 $original=imagecreatefromstring($fileContent);

 Sixth, save the image.

 imagepng($original, my_image.png);

 And Bingo, the image is now the my_image.png file.

 Any comments or suggestions are welcomed.

 tedd

 --


 http://sperling.com/

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php





--
Gerry
http://portal.danen.org/



--

http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Saving a BLOB image to a file (SOLVED)

2006-02-16 Thread tedd

So single-user disclaimer would apply?


Obviously, if you have numerous people accessing your dB and doing 
inserts and updates, then all the lock-down stuff needs to be 
implemented.


But, my code is not for that, but rather for someone (a single 
programmer) who wants to pull an image from a BLOB and save it as a 
file.


Normally, a civilian wouldn't be doing that for if they wanted to see 
a file, your code would simply pull the image directly from the MySQL 
to the web page. No need to save as a file.


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!


tedd



On 2/16/06, tedd [EMAIL PROTECTED] wrote:

 Gerry wrote:

 What if several users try to access a different picture at the same
 time? Won't that render the wrong image for some?
 
 Gerry


 Gerry:

 Normally, it's not needed for civilians. This is for when YOU want to
 do some image stuff on your images, like to do some background
 processing via your code.

 As for users, they just want to see the image and you can pull that
 out of your dB or files, whichever you prefer.

 tedd

 ---


 
 On 2/16/06, tedd [EMAIL PROTECTED] wrote:
   Hi gang:
 
   In a previous post (i.e., [PHP] Strange response to MySQL query) I
   was trying to save a BLOB as a file, namely using an INTO DUMPFILE
   query.
 
   If you are the system admin, and have all the system permissions,
   then this is not a big problem. However, if you aren't, then getting
   MySQL to dump a file becomes very problematic with respect to getting
   the right permissions to accomplish the task.
 
   So, I found a way around it. I am sure that others have thought of
   this before me, so I'm not claiming credit and I am sure that this
   solution is obvious to some -- but, it's just that I searched
   literally hundreds of posts and spent considerable time without
   finding an answer.  The following is the result of my tinkering. So
   here goes:
 
   First, on your site create a file called my_image.png
 
   Second, give it 777 permissions.
 
   Third, in your code after opening the dB, grab the image from MySQL:
 
   $dbQuery = SELECT image;
   $dbQuery .= FROM $your_table ;
   $dbQuery .= WHERE image_Id = $pageNum;
 
   $result = mysql_query($dbQuery) or die(Error: .mysql_error());
 
   Forth, drop the image into a variable:
 
   $fileContent = @mysql_result($result, 0, image);
 
   Fifth, create an image from a string (a great function here):
 
   $original=imagecreatefromstring($fileContent);
 
   Sixth, save the image.
 
   imagepng($original, my_image.png);
 
   And Bingo, the image is now the my_image.png file.
 
   Any comments or suggestions are welcomed.
 
   tedd
 
   --
 
 

   http://sperling.com/
 
   --
   PHP General Mailing List (http://www.php.net/)
   To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 
 
 --
 Gerry
 http://portal.danen.org/


 --


 http://sperling.com/




--
Gerry
http://portal.danen.org/



--

http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php