kind of a problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

kind of a problem

Post by dsick »

for some reason when I enter the contents of this file into my database

it enters fine, but there is a record entered in the database with a period, and another one entered with two periods

Code: Select all

//after u moved uploaded album, save it to db
if ($handle = opendir('testfile')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    
    while (false !== ($file = readdir($handle))) {
        $db = new database(); 
        $db->newalbum($file); 
        
        echo "$file";
        echo "<Br/>";
    }
    closedir($handle);
}


do you have any idea why there is periods inserted in the database?

I assume it has something to do with this output

Directory handle: Resource id #7 Files: .
..
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: kind of a problem

Post by buckit »

. is current directory and .. is parent directory to current

in your loop. skip the directory if it == . or ..
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

Re: kind of a problem

Post by dsick »

ill try that

i need a way to read the contents from a file so i can use them dynamically, and this problem is getting on my nerves

making an album site, and there was 22 MP3s and they all inserted correct but that period issue
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: kind of a problem

Post by AbraCadaver »

You can also try:

Code: Select all

foreach(glob('testfile/*.mp3') as $file) {
        $db = new database();
        $db->newalbum($file);
       
        echo "$file";
        echo "<Br/>";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

Re: kind of a problem

Post by dsick »

this is a test folder that im getting contents from, not a test file

it's just leaving behind a trail of period entries in my database

the first entry is just one period
and the second entry is two periods
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: kind of a problem

Post by AbraCadaver »

dsick wrote:this is a test folder that im getting contents from, not a test file

it's just leaving behind a trail of period entries in my database

the first entry is just one period
and the second entry is two periods
I understand that. What I gave you is a way to do this. Or do what Buckit described:

Code: Select all

if("testfile/$file" != '.' && "testfile/$file" != '..')
Or

Code: Select all

if(!is_dir("testfile/$file"))
To skip those and any other directories.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

Re: kind of a problem

Post by dsick »

oh i see its only going to get the files that are .MP3

this should work :D

edits.. so i can readdir in those conditions? or insert into database in those conditions, i don't care about the periods until its inserted in the database because heres what its doing.. if you can see them

Image
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: kind of a problem

Post by AbraCadaver »

Yes, use one of those conditions around the insert, which I assume is:

Code: Select all

$db = new database();
$db->newalbum($file); 
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

Re: kind of a problem

Post by dsick »

yea your right, im using my database class

edits..

i tried both methods and the periods are still being put in

can i just skip a file if it doesn't end in .mp3 when i read the dir?

Code: Select all

$name = substr($file, 0, strrpos($file, '.')); 
        $db = new database();
        $db->newalbum($name); 

tried doing it like this, but this time it only put one period in, then the other entry was a empty space

and it stripped the extention off the file which is what i wanted anyway but still doesn't fix the period issue
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

Re: kind of a problem

Post by dsick »

don't ask me why its working, but its working now after i made the code even longer :(

Code: Select all

if ($handle) {
   // loop through all of the files
   while (false !== ($fname = readdir($handle))) {
    
      if (($fname != '.') && ($fname != '..') &&
          ($fname != basename($_SERVER['PHP_SELF']))) {
     
          $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
          $db = new database(); 
          $db ->newalbum($fname);
      }
   }

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: kind of a problem

Post by AbraCadaver »

Glad you got it. The glob() would be so much easier, an shorter :D
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply