SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Put the names of all files in a database

    Is it possible to get the names of files in a dir and add it to mysql database using php? I have a dir named Files that have about 1000 zip files i want to add there names (file1.zip, file2.zip, file3.zip, ..) to my database (ID,Filename,Info,Size), If It can add the size to it will be great , So i have to add only the info of the files.

    Thanx

  2. #2
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello? Any one!
    Can some one help me with this or just give me a hint?

  3. #3
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, here's the general idea.
    There's a function in PHP, opendir(), which allow you to list all the files and directories in the current path, for example
    PHP Code:
    <?php  
    if ($dir = @opendir('/your_path')) {  
      while (
    $file readdir($dir)) {  
        echo 
    "$file\n";  
      }    
      
    closedir($dir);  
    }  
    ?>
    Now, just include in the script above the function filesize($filename) to get the size's file.

    You can store temporally the detail of each file (name, extension and size) into an array, but before that you need to discriminate the files from directory, so use the function is_file() to check if the element $file is a file or not.
    After that just run over all the elements of the array, with foreach(), and insert them into your database.

    Hope this could help you
    Paul

  4. #4
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That helped alot! thanx

    The script now prints the file name (Files only) and its size, every thing is good now but i'm not sure how to put this data in the database ,
    PHP Code:
    <?php  
    if ($dir = @opendir('')) {  
      while (
    $file readdir($dir)) {  
    if ((
    filesize($file) != "0")) {
     echo 
    "$file "
         echo 
    filesize($file);
         echo 
    "<br>";
     }      
     }    
      
    closedir($dir);  
    }  
    ?>
    This will print:
    file.php 827
    file2.php 854
    file3.php 3645
    file4.php 23917
    file5.php 246
    ...
    i need to put this in the database!

    Thanx again Paul

  5. #5
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why dont u take a look at Kevin's famous tutorial? So u can do database part urself

  6. #6
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have read it, and I can do some (simple) DB work, but i'm not sure how to take this data and put it in the database!

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very much untested.


    PHP Code:

    <?php  
        $d 
    dir('.');
        while (
    $f $d->read()) {  
            if ((
    filesize($f) > 0)) {
            
    $files[] = array("filename" => $f,
                             
    "filesize" => filesize($f)
                             );
                }      
            }    
        
    $d->close();


        if(
    count($files)) {
            foreach(
    $files as $key => $file) {
                
    $result mysql_query("INSERT INTO file set Filename = '{$files[$key]['filename']}', 
                                                            Size = '
    {$files[$key]['filesize']}'");
                if(
    mysql_affected_rows($result)) {
                    print 
    "{$files[$key]['filename']} inserted successfully<br>";
                    }
                }
            }
        else {
            print 
    "No files in directory";
            }

    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It have an error in the part of the database, i get this error:
    Warning: Supplied argument is not a valid MySQL-Link resource in c:\phpdev\www\site\dir2.php on line 24

    line 24 = if(mysql_affected_rows($result)) {

    line 21 :
    $result = mysql_query("INSERT INTO file set name = '{$files[$key]['filename']}',
    size = '{$files[$key]['filesize']}'");

    when i remove line 24 it work, and print
    file1 inserted successfully
    file2 inserted successfully
    file3 inserted successfully
    ..

    here is the full code
    PHP Code:
    <?php
    include("commen.php");

    //Connect to database
    $dbcnx dbConnect("m0h");

    $d dir('.'); 
    while (
    $f $d->read()) { 
    if ((
    filesize($f) > 0)) { 
    $files[] = array("filename" => $f
    "filesize" => filesize($f
    ); 


    $d->close(); 


    if(
    count($files)) { 
    foreach(
    $files as $key => $file) { 

    $result mysql_query("INSERT INTO file set name = '{$files[$key]['filename']}', 
    size = '
    {$files[$key]['filesize']}'"); 

    if(
    mysql_affected_rows($result)) { 
    print 
    "{$files[$key]['filename']} inserted successfully<br>"



    else { 
    print 
    "No files in directory"


    ?>
    Thanx alot for your help

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry it should just be

    PHP Code:
    if(mysql_affected_rows()) { 
    Not

    PHP Code:
    if(mysql_affected_rows($result)) { 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx, working very good


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •