Page 1 of 1
kind of a problem
Posted: Mon Jul 26, 2010 6:22 am
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: .
..
Re: kind of a problem
Posted: Mon Jul 26, 2010 8:29 am
by buckit
. is current directory and .. is parent directory to current
in your loop. skip the directory if it == . or ..
Re: kind of a problem
Posted: Mon Jul 26, 2010 2:10 pm
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
Re: kind of a problem
Posted: Mon Jul 26, 2010 2:14 pm
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/>";
}
Re: kind of a problem
Posted: Mon Jul 26, 2010 2:35 pm
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
Re: kind of a problem
Posted: Mon Jul 26, 2010 2:49 pm
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
To skip those and any other directories.
Re: kind of a problem
Posted: Mon Jul 26, 2010 2:56 pm
by dsick
oh i see its only going to get the files that are .MP3
this should work
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

Re: kind of a problem
Posted: Mon Jul 26, 2010 3:41 pm
by AbraCadaver
Yes, use one of those conditions around the insert, which I assume is:
Code: Select all
$db = new database();
$db->newalbum($file);
Re: kind of a problem
Posted: Mon Jul 26, 2010 4:04 pm
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
Re: kind of a problem
Posted: Mon Jul 26, 2010 5:10 pm
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);
}
}
Re: kind of a problem
Posted: Mon Jul 26, 2010 6:44 pm
by AbraCadaver
Glad you got it. The glob() would be so much easier, an shorter
