Bildername aus Ordner auslesen und namen in DB speichern

Matthiasghh

Mitglied
Hallo Leute

Habe wieder mal ein Problem !
Ich möchte Bilder aus einen Ordner auslesen und den Namen des Bildes in eine Datenbank speichern.

Wer hat Ahnung oder sogar besser Code ?

Vielen Dank ! :)
 
PHP:
<?php
$dir = "images/";
$d = dir($dir);  
    while ($file = $d->read()) {
	if ($file != "." AND $file != ".."){
		$array[] = $file;
		}
	}
	$picanz = count($array);
	for($i=0; $i<=$picanz; $i++) {
		$pic = $array[$i];
		require("db.php"); //bilder in db speichern
		mysql_connect($dbhost,$dbuser,$dbpass) or die ("Keine Verbindung zur db moeglich");
		mysql_select_db($dbbase) or die ("db gibts net");
		mysql_query("INSERT INTO bilder (bildname, id) VALUES ('$pic', '')");
	}
?>

db.php
PHP:
<?php
	$dbhost = "localhost";
	$dbuser = "";
	$dbpass = "";
	$dbbase = "";
?>

Nicht getestet, müsste aber funktionieren.
 
Hallo und vielen Dank,

aber wen ich jetzt einen insert in die DB machen und anschließend die Bilder wieder raus hole ist ein Datenbank eintrag ohne Wert wieso ?



Vielen Dank
 
Ersetz diese Zeile mal durch das:
PHP:
for($i=0; $i<$picanz; $i++) {

Wenn es das ist was ich glaube dass es ist +g+
Also ein leerer Datenbankeintrag ohne Bild ist drin oder? Probier mal obiges.
 
Super aber jetzt kommt das nächste Problem...
der liest jetzt auch die Windnows Thumbs aus, kannst Du mir sagen wie ich nur jpg auslesen kann. ?

Vielen Dank Matthias
 
PHP:
<?php
$dir = "images/";
$d = dir($dir);  
    while ($file = $d->read()) {
    if ($file != "." AND $file != ".."){
        $array[] = $file;
        }
    }
    $picanz = count($array);
	for($i=0; $i<=$picanz; $i++) {
		$filetype = substr(strrchr($array[$i], "."), 1);
		if (($filetype == "JPG") || ($filetype == "jpg") || ($filetype == "JPEG") || ($filetype == "jpeg"))	{
			$pic = $array[$i];
			require("db.php"); //bilder in db speichern
			mysql_connect($dbhost,$dbuser,$dbpass) or die ("Keine Verbindung zur db moeglich");
			mysql_select_db($dbbase) or die ("db gibts net");
			mysql_query("INSERT INTO bilder (bildname, id) VALUES ('$pic', '')");
		}
	}
?>

Ungetestet.
 
Danke,

ich habe jetzt einen Ordner namens "Bilder" in diesen Ordner ist ein Unterordner wie kann ich per PHP die Unterordner als echo oder so ausgeben ?

Vielen Dank
 
Hallo hab da was gefunden aber ich möchte das dass Script nur die Ordner liest nicht
z.B die Windows Thumbs.

PHP:
$handle=opendir ('bilder/');
while (false !== ($file = readdir ($handle))) {
echo $file."<br>";
}
closedir($handle);
 
Hallo wie kann man das lösen ?

PHP:
<?
$handle=opendir ('bilder/'); 
while (false !== ($file = readdir ($handle))) { 
?>
<option>
<?
echo "$file";
?>
</option>
<?
} 
closedir($handle);
?>

Aus gabe sieht dann so aus:
.
..
maja
Thumbs.db
Ich möchte aber nur die Ordner angezeigt bekommen ohne Thumbs.db und Punkt oder Punkt Punkt.

Vielen Dank im voraus !
 
Hab jetzt keine Zeit dir das zu Coden, aber das hier kannst du ein wenig abändern bzw. Teile davon wiederverwenden. Ist ein recht nettes Scriptchen :)

PHP:
<?php

   # Do we have a path? if not, it's the current directory
   $path = $_GET["path"];
   if( !isset( $path ) || $path == "" )  {
     $path = ".";
   }

   # Print out for navigation
   print "Current path: <b>" . $path . "</b><br />";

   # Initialise list arrays, directories and files separately and array counters for them
   $d_arr = array(); $d = 0;
   $f_arr = array(); $f = 0;

   # Open possibly available directory
   if( is_dir( $path ) ) {
     if( $handle = opendir( $path ) ) {
         while( false !== ( $file = readdir( $handle ) ) ) {
           # Make sure we don't push parental directories or dotfiles (unix) into the arrays
           if( $file != "." && $file != ".." && $file[0] != "." ) {
               if( is_dir( $path . "/" . $file ) )
                 # Create array for directories
                 $d_arr[$d++] = $file;
               else
                 # Create array for files
                 $f_arr[$f++] = $file;
           }
         }
     }
   }

   # Wrap things up if we're in a directory
   if( is_dir( $handle ) ) closedir( $handle );

   # Sort and reset the arrays
   asort( $d_arr ); reset( $d_arr );
   asort( $f_arr ); reset( $f_arr );

   # Print a parent directory link
   $d_prev = substr( $path, 0, ( strrpos( dirname( $path . "/." ), "/" ) ) );
   print "<a href=\"?path=" . $d_prev . "\"> Parent directory </a><br />\n";

   # Print the directory list
   for( $i=0; $i < count( $d_arr ); $i++ ) {
     # Print with query string
     print "[ <img src=\"icons/dir.gif\" alt=\"\" /> ] <a href=\"?path=" . $path . "/" . $d_arr[$i] . "\">" . $d_arr[$i] . "</a>/<br />\n";
   }

   # Print file list
   for( $i=0; $i < count( $f_arr ); $i++ ) {
     # Only print path and filename
     print "[ <img src=\"icons/file.gif\" alt=\"\" /> ] <a href=\"" . $path . "/" . $f_arr[$i] . "\"> " . $f_arr[$i] . "</a>";
     # We may want a file size. NOTE: needs $path to stat
     if( filesize( $path . "/" . $f_arr[$i] ) >= 1024 ) {
         # Size in kilobytes
         print " " . round( filesize( $path . "/" . $f_arr[$i] ) / 1024, 1 ) . " KB<br />\n";
     } elseif( filesize( $path . "/" . $f_arr[$i] ) >= 1048576 ) {
         # Size in megabytes
         print " " . round( filesize( $path . "/" . $f_arr[$i] ) / 1024 / 1024, 1 ) . " MB<br />\n";
     } else {
         # Size in bytes
         print " " . filesize( $path . "/" . $f_arr[$i] ) . " bytes<br />\n";
     }
   }

?>
 
Zurück