Re: using PHP script to add address'es of all picture files in a dir to MYSQL DB
| From: | Stefan Rusterholz | Date: | Tue, 06 Nov 2001 12:37:24 +0000 |
| Subject: | Re: using PHP script to add address'es of all picture files in a dir to MYSQL DB | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-73583@lists.php.net to get a copy of this message | ||
I appended a function which gives you all files in a directory (including
subdirectories). I wrote it in a hurry for something else so don't expect
very good code.
You just set the $basedir to that directory where your .jpg's are in. Then
you do a 'foreach ($files as $file)' loop and check each file if it is a jpg
(if it is ok for you then I'd suggest to use a preg_match like "\\.jpg$/i")
and then do the UPDATE or INSERT query. That's it.
Just one point to clear: it is written for windows, so directories are
delimited by backslash (\) and not by slash (/). If you want to use it on a
*nix-box you will have to search an replace all \\.
Code (just paste it into a .php file an look how it works)
<?php
function getFilesInDir($theDir, $ignore=true){
$delim = "\\";
$dircon = array();
$dh = openDir($theDir);
while ($dirORfile = readdir($dh)){
if ($ignore == false || ($dirORfile != '.' && $dirORfile != '..')){
if (is_Dir($theDir.$delim.$dirORfile)){
#echo "DIR: $theDir$delim$dirORfile\n";
$files = array();
$files = getDircon($theDir.$delim.$dirORfile);
$dircon = array_merge($dircon,$files);
}else{
#echo "FILE: $theDir$delim$dirORfile\n";
array_push($dircon,$theDir.$delim.$dirORfile);
}
}
}
return $dircon;
}
$basedir = "C:\Your Image Directory";
$dircon = getFilesInDir($basedir);
foreach ($dircon as $file){
echo "$file\n";
}
?>
Stefan Rusterholz, s.rusterholz@interaktion.ch
----------------------------------
interaktion gmbh
Stefan Rusterholz
Zürichbergstrasse 17
8032 Zürich
----------------------------------
T. +41 1 253 19 55
F. +41 1 253 19 56
W3 www.interaktion.ch
----------------------------------
----- Original Message -----
From: "Daniel" <spam@nospam.com>
To: <php-general@lists.php.net>
Sent: Tuesday, November 06, 2001 1:12 PM
Subject: [PHP] using PHP script to add address'es of all picture files in a
dir to MYSQL DB
> Hi,
>
> I want to make a simple database driven photo gallery for my site where
> users can search the picture descriptions to find the pictures they want,
> all fairly simple PHP/MySQL stuff which I know. My big problem is that I
> have like 500+ images I want to put into the MySQL DB and I am way too
lazy
> to consider doing it the hard way. I was wondering if it would be
possible
> to write a PHP script which would get a list of all the .jpg files in its
> current directory and then insert a reference for each file (basically
just
> its location on the web server) into the MySQL PHOTO table? I was
thinking
> about this on the train yesterday so heres the pseudo PHP/C code of the
sort
> of script I was thinking about(I don't know the PHP file functions):
>
> file://get the number of jpeg files in same dir as this script (I don't
know code
> for this)
> $NumFiles = GetNumberOfJPGFilesInDirectory();
> While ($NumFiles > 0)
> {
> file://get file location of current jpeg file (I don't know code for
this)
> $FileName = GetFileNameAndLocation($NumFiles);
> // construct MySQL query
> $sql = "INSERT INTO PHOTO SET " . "FILE='$FileName ";
> file://add file location of current jpeg and do error handling (easy
stuff)
> if (mysql_query($sql))
> {
> echo("Photo " . $FileName ." has been added.<br>");
> }else
> {
> echo("Error adding photo: " . mysql_error() . " <br>");
> }
> $NumFiles = $NumFiles-1;
> }
>
> Would anyone be able to help me get the code for this script right? I can
> do basic PHP & MySQL stuff but I am not familiar with PHP file functions
so
> I need someone to help me get it working...
>
>
> Cheers,
>
> Daniel
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
>