Jump to content

Storing Filenames and size into an array


TapeGun007

Recommended Posts

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.

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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! :-)

Link to comment
Share on other sites

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.

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.