TapeGun007 Posted March 20, 2010 Share Posted March 20, 2010 Here is my code, and I have no clue what I'm doing with this array because I cannot seem to find an article that displays what I'm trying to do. $All_Files = array("Dir_Files" => array(FileName,FileSize)); while (false !== ($file = readdir($handle))) { $File_Size=filesize('Music_Library/'.$file); $File_Size=$File_Size/1048576; $Dir_Files['$File_Name']['$File_Size']; } My problem is that I don't understand the syntax that I need here. What I want to do is 1) Dump the file names of a directory into an array, and store the filesize 2) Sort the filename alphabetically. Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/ Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 This would do for your file size: function bytes_to_string($size, $precision = 0) { $sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'Bytes'); $total = count($sizes); while($total-- && $size > 1024) $size /= 1024; $return['num'] = round($size, $precision); $return['str'] = $sizes[$total]; return $return; } $stat = stat($file); // ... slow, but faster than using filemtime() & filesize() instead. $info = pathinfo($file); $item['name'] = $info['filename']; $item['bytes'] = $stat['size']; $total_size += $item['bytes']; $total_size = bytes_to_string($total_size, 2); Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1028998 Share on other sites More sharing options...
TapeGun007 Posted March 20, 2010 Author Share Posted March 20, 2010 Yeah, that's not really the issue. I'm not storing the filenames and filesize correctly in the array...well... at all actually. I suppose (after further reading) I could create two arrays one for the filename and filesize, then merge them, sort, and then spit out the results.... Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029001 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 try sort(); then. Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029003 Share on other sites More sharing options...
TapeGun007 Posted March 20, 2010 Author Share Posted March 20, 2010 Yes, I know... that's the easy part. My code above does NOT work. I'm asking how to store those variable file names into an array, THEN sort it. The code above is not storing the values into the array, because my syntax is incorrect. That is what I'm asking for help on. Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029007 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 are you trying to array a folder and the contents inside that? Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029009 Share on other sites More sharing options...
TapeGun007 Posted March 20, 2010 Author Share Posted March 20, 2010 Yes, there is a folder called "Music_Library". There are many .mp3 files and such in there. I want to capture the filenames AND file sizes into a multidimensional array For example: $All_Files [Filename1]="Tigger.mp3" [Fillesize1]="10 MB" $All_Files [Filename2]="PooBear.mp3" [Filesize2]="5 MB" $All_Files [Filename3]="Christopher.mp3" [Filesize3]="15 MB" If I can simply store this type of information into an array as listed above, I can figure out the rest. The problem is, my code doesn't work, it doesn't store anything into the array. My code does spit out the filenames and sizes because I tested that already. Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029011 Share on other sites More sharing options...
oni-kun Posted March 20, 2010 Share Posted March 20, 2010 Yes, there is a folder called "Music_Library". There are many .mp3 files and such in there. I want to capture the filenames AND file sizes into a multidimensional array For example: $All_Files [Filename1]="Tigger.mp3" [Fillesize1]="10 MB" $All_Files [Filename2]="PooBear.mp3" [Filesize2]="5 MB" $All_Files [Filename3]="Christopher.mp3" [Filesize3]="15 MB" If I can simply store this type of information into an array as listed above, I can figure out the rest. The problem is, my code doesn't work, it doesn't store anything into the array. My code does spit out the filenames and sizes because I tested that already. Look at glob and foreach, It can't get that much harder from there. $array = null; foreach (glob("/Music_library/*.mp3") as $filename) { $array[$filename] = $filename; $array[$filename]['filesize'] = filesize($filename) } Well, don't quote me on that working, but it is that simple. Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029012 Share on other sites More sharing options...
scvinodkumar Posted March 20, 2010 Share Posted March 20, 2010 hi i have similar problem, i have directory for each user, and user has permission to upload only ten files and these files stored in the below format, for example, if the user id is 299 and uploading only three files, and creating thumbanails with three sizes, then 299_1.jpg,299_1medium.jpg,299_1small.jpg 299_2.jpg,299_2lmedium.jpg,299_2small.jpg 299_3.jpg,299_3medium.jpg,299_3small.jpg now i need to get only original image filenames in an array, like 299_1.jpg,299_2.jpg,299_3.jpg. how should i do this? Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029049 Share on other sites More sharing options...
scvinodkumar Posted March 20, 2010 Share Posted March 20, 2010 I tried some thing like this foreach (glob("/Music_library/[0-9]*_[0-9].jpg") as $filename) { its displaying correctly, but still it has one problem, its not displaying file with name, 299_10.jpg could u plz correct it Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029050 Share on other sites More sharing options...
TapeGun007 Posted March 21, 2010 Author Share Posted March 21, 2010 glob() wasn't the answer, it was far from it actually. I read up that, and it didn't apply to what I was trying to accomplish really. My question was really that of syntax in storing to the array, and nothing seemed to spell out exactly what I needed. I finally figured it out, and thought I'd post it here in the event someone ever needed this. <?php if ($handle = opendir('Music_Library')) { $y==0; /* Read the files in the directory, then store it in an array to sort it alphabetically */ $All_Files = array(); while (false !== ($file = readdir($handle))) { /* Weed out the . and .. for going up one directory, so this keeps those from showing */ if ( $file != '.' && $file != '..' ) { $y++; $File_Size=filesize('Music_Library/'.$file); $File_Size=$File_Size/1048576; $All_Files[$y]['FileName']=$file; $All_Files[$y]['FileSize']=$File_Size; } } closedir($handle); sort($All_Files); print_r($All_Files); } ?> My output shows just as I wanted it to: Array ( [0] => Array ( [FileName] => Hermie.wma [FileSize] => 3.85604286194 ) [1] => Array ( [FileName] => Jolly.wav [FileSize] => 1.26176643372 ) [2] => Array ( [FileName] => Sing for Joy.mp3 [FileSize] => 8.63101959229 ) [3] => Array ( [FileName] => Wildlife.wmv [FileSize] => 25.0301609039 ) ) Now if there is a better way to accomplish this, then by all means enlighten me! :-) Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029363 Share on other sites More sharing options...
TapeGun007 Posted March 21, 2010 Author Share Posted March 21, 2010 I did change one line of code actually to streamline the size of the file, just an FYI for noobs like me. $All_Files[$y]['FileSize']=$File_Size; To this: $All_Files[$y]['FileSize']=number_format($File_Size,2)." MB"; Now I get this on the output: Array ( [0] => Array ( [FileName] => Hermie.wma [FileSize] => 3.86 MB ) [1] => Array ( [FileName] => Jolly.wav [FileSize] => 1.26 MB ) [2] => Array ( [FileName] => Sing for Joy.mp3 [FileSize] => 8.63 MB ) [3] => Array ( [FileName] => Wildlife.wmv [FileSize] => 25.03 MB ) ) It just looks nicer. Quote Link to comment https://forums.phpfreaks.com/topic/195897-storing-filenames-and-size-into-an-array/#findComment-1029366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.