[PHP] Re: Images in Mysql

2002-07-11 Thread Richard Lynch

the web server, how should the image be named [uniquely]? The problem is 
that if the user specifes the name of the image file, there's a chance 
he'll name another one the same way (and thus overwrite the original). I 
was thinking of maybe a dual approach, where the user gives the image 
file a name, but then the PHP script appends, say, the table name and 
record ID--so as to make the filename unique.

What I do is this:

?php
  # Get rid of gnarly characters in filenames:
  $filename = ereg_replace('[^a-zA-Z0-9_\\.-]', '', $photo_name);
  # If their filename was so messed up that there's nothing left, give it a
random name.
  $filename = $filename ? $filename : chr(rand(ord('A'),ord('Z'))) .
chr(rand(ord('A'),ord('Z'))) . chr(rand(ord('A'),ord('Z'))) .
chr(rand(ord('A'),ord('Z')));
  
  # Now be sure it's unique:
  $appendix = '';
  # break the filename up into 'whatever' and '.jpg' (or '.gif' or whatever
it is)
  # In order to force uniqueness (below) I want to change 'duplicate.jpg'
into 'duplicate2.jpg', not 'duplicate.jpg2' :-)
  $extpos = strrpos($filename, '.');
  # echo extpos is $extposBR\n;
  if (!$extpos){
$extension = '';
  }
  else{
$extension = substr($filename, $extpos);
$filename = substr($filename, 0, $extpos);
  }
  
  $pathname = $directory/$filename$appendix$extension;
  # echo extension $extensionBR\n;
  # echo filename $filenameBR\n;
  # echo appendix $appendixBR\n;
  # echo pathname $pathnameBR\n;
  while (file_exists($pathname)){
# It's kinda hacky to ++ the empty string, but it avoids tacking on '0'
# to filenames that were fine as they were...
$appendix++;
$pathname = $directory/$filename$appendix$extension;
# echo extension $extensionBR\n;
# echo filename $filenameBR\n;
# echo appendix $appendixBR\n;
# echo pathname $pathnameBR\n;
  }
  if (move_uploaded_file($photo, $pathname)){
# Success!
  }
  else{
# Error message
  }
?

-- 
Like Music?  http://l-i-e.com/artists.htm
Off-Topic:  What is the moral equivalent of 'cat' in Windows?

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




[PHP] Re: Images in Mysql

2002-07-10 Thread René Fournier

Hi Asaf,

Thanks for the advice. That sounds like a good solution. One thing I was 
wondering about is...  when my PHP script saves the uploaded image to 
the web server, how should the image be named [uniquely]? The problem is 
that if the user specifes the name of the image file, there's a chance 
he'll name another one the same way (and thus overwrite the original). I 
was thinking of maybe a dual approach, where the user gives the image 
file a name, but then the PHP script appends, say, the table name and 
record ID--so as to make the filename unique.

What do you think? Is that how you do it? (Or is there a better way??)

...Rene


On Wednesday, July 10, 2002, at 07:34  AM, Asaf Maruf Ali wrote:

 Hello Rene

 We used a similar approach albeit both our PHP scripts and data were on 
 the
 same server. We however opted to place html links to the images in the 
 MySQL
 db and actual images (GIF's, JPG's)files on the server harddisk.

 It works fine with no performance issues.


 Hope this helps

 Regards
 Asaf Maruf
 PING


 I'm working on a simple content management system that uses PHP and
 MySQL for updating a web site's text (stored in a MySQL database). (The
 PHP scripts that do the updating (my stuf) live on one web server, the
 actual DB data to be updated (my client's stuff) live on another.) So
 far, I've only had to he able to update the text content of a
 site--therefore, I've only had to bother to store textual data in the
 client's DB. But now the client wants to be able to upload/change/delete
 certain pictures on their web site--using my CMS tool--so I am faced
 with the following problem:

 Do I store all such images in the DB? (Which I understand reduces
 performance.)

 Or do I--somehow--store the images as files on the client's web server?
 And if so, how? (Because my PHP scripts are being executed on a
 different server.)

 ...Rene

 ---
 René Fournier,
 [EMAIL PROTECTED]



 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.361 / Virus Database: 199 - Release Date: 5/7/02



---
René Fournier,
[EMAIL PROTECTED]

Toll-free +1.888.886.2754
Tel +1.403.291.3601
Fax +1.403.250.5228
www.smartslitters.com

SmartSlitters International
#33, 1339 - 40th Ave NE
Calgary AB  T2E 8N6
Canada


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




RE: [PHP] Re: Images in Mysql

2002-07-10 Thread joakim . andersson

 Hi Asaf,
 
 Thanks for the advice. That sounds like a good solution. One 
 thing I was 
 wondering about is...  when my PHP script saves the uploaded image to 
 the web server, how should the image be named [uniquely]? The 
 problem is 
 that if the user specifes the name of the image file, there's 
 a chance 
 he'll name another one the same way (and thus overwrite the 
 original). I 
 was thinking of maybe a dual approach, where the user gives the image 
 file a name, but then the PHP script appends, say, the table name and 
 record ID--so as to make the filename unique.
 
 What do you think? Is that how you do it? (Or is there a better way??)

If you have a table that stores information about the images then I suggest
you use the record ID alone (with the correct file-extension) as a filename.
This will allways be unique, otherwise you probably have trouble with your
dB aswell...

This is how I do it.

Regards
Joakim Andersson

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




[PHP] Re: Images and Mysql

2002-01-30 Thread Joe Van Meer

See my reply to Image fields and PHP on the 14th of january.
HTH Joe :)

David Orn Johannsson [EMAIL PROTECTED] wrote in message
006501c1a99a$31ce5cb0$6500640a@gandalf">news:006501c1a99a$31ce5cb0$6500640a@gandalf...
I need to find out how to upload images to a database and then
displaying them again, can anybody
direct me to a howto or any thing like that to help me figure out how
it's done.

 http://www.atom.is/ 
Davíð Örn Jóhannssson
Vefforritari

Atómstöðin hf.
Garðastræti 37
101 Reykjavík

sími: 595-3643
fax: 595-3649
 mailto:[EMAIL PROTECTED] [EMAIL PROTECTED]
 http://www.atom.is/ http://www.atom.is






-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]