strange results in mysql with readdir.php
| From: | Alessandro Folghera | Date: | Sun, 06 Jun 2004 15:56:33 +0000 |
| Subject: | strange results in mysql with readdir.php | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-34661@lists.php.net to get a copy of this message | ||
Anybody has an idea about what happened to me ?
The following script should just charge in a mysql db the images located in
a directory. Unfortunately everytime I call "readdir.php" (never mind if
there are or not new images) mysql is charging 2 or 6 copies of the same
last image uploaded into the directory. Anybody may explain to me where I'm
failing?
Thanks for all the phpers!
Alessandro
<?
$dbcnx = mysql_connect("localhost", "root", "password");
mysql_select_db("news");
if (!$dbcnx)
{
echo( "<p>connection to database server failed!</p>");
exit();
}
if (! @mysql_select_db("news") )
{
echo( "<p>Image Database Not Available!</p >" );
exit();
}
$path = "./";
$dir_handle = @opendir($path) or die("Unable to open directory $path");
while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == 'gif' OR $filetyp == 'jpg') {
$handle = fopen($path . "/" . $file,'r');
$file_content = fread($handle,filesize($path . "/" . $file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sql = "INSERT INTO images SET sixfourdata='$encoded'";
@mysql_query($sql);
}
}
closedir($dir_handle);
echo("complete");
?>