Jump to content

reading directories


Kingy

Recommended Posts

I'm completely lost on how to do this, and im pretty sure it can be.

 

What im looking it do is read all the files in a certain directory and then add them all to a mysql database. When i say add them to a database, i just mean add the name and perhaps the link to the file?

 

any help would be gladly appreicated

Link to comment
Share on other sites

This should work, modify to your needs.

 

<?php

// Open up the directory
$directoryHandle = opendir('images/test/');

// Now read through the directory and insert folder and files names into the database
while (false !== ($file = readdir($handle)))
{
    $sql = "INSERT INTO files
       (files_filename)
       VALUES ('$file')";
    mysql_query($sql);   
}

?>

Link to comment
Share on other sites

<?php
// this file shows how to list the files in a specific directory

//works with php4
if ($handle = opendir('/home/content/img')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    while (false !== ($file = readdir($handle))) {
	if($file != "." && $file != "..") {
        echo "$file<br />\n";
	}
    }
    closedir($handle);
}


//works with php5
echo "The name of this file, including full server path is: ".$_SERVER['SCRIPT_FILENAME']. ".<br />The following is the list of files withing the same directory<br /><br />";
$d = scandir('C:/wamp/www/script_library/');
  foreach($d as $f) {
    if ($f != '.' && $f != '..') {
      echo $f . "<br />";
    }
  }

?>

 

This doesn't insert the files into the database, but I was just being lazy.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.