Jump to content

File Detection


Duerdum9

Recommended Posts

Hello,

I was wondering if you could help me with the following file, and help me how to fix my error.

 

<?php
$directory='./music/';
$extension='';
$files_array = array();
$dir_handle = @opendir($directory) or die("There is an error with your file directory!");
while ($file = readdir($dir_handle)) 
{
if($file{0}=='.') {continue;}
if($extension == 'php'){ continue;}
if($extension == 'htaccess') {continue;}
$files_array[]=$file;
}
$file_downloads=sort($files_array,SORT_STRING);
$path_parts = pathinfo('musicogg');
foreach ($files_array as $file_name){
//echo "{mp3:\"music/$file_name.mp3\"}";
echo $path_parts['extension'], "\n";
}

?>

(removed spaces to compress it).

 

Anyhow, the last few lines of code, where I've commented a line of code out - that's working as it should by going into the dir and checking all the files and echo'ing them. So this is what I want.

I want it the final code to do this execute this line of code. {mp3:"music/name.mp3",ogg:"music/name.ogg"} - Notice, I do not really care if they the files need to be in one dir, or two dirs.

How would I do, so it would show that in HTML, of course with all the files in the dir(s). (Note, name.mp3 & name.ogg is the same song).

 

Thanks a ton :)

Link to comment
Share on other sites

I'm not understanding everything you said. Especially about

Notice, I do not really care if they the files need to be in one dir, or two dirs.

 

It looks like the fields would always be in one directory. Anyway, I expect your problem is that the curly braces are in the wrong positions. Also, why hardcode the directory in that line when you made the directory a variable above?

echo "mp3: \"{$directory}{$file_name}\"<br>\n";

Link to comment
Share on other sites

JSON? The keys need to be quoted, and you can't return multiple objects at once.

 

Taking a guess at what you want, the output should look like

[{"mp3": "music/name.mp3", "ogg": "music/name.ogg"}, {"mp3": "music/name.mp3", "ogg": "music/name.ogg" }...]

which you can do easily with json_encode(). Except your code needs changing to get that. What if there are multiple .mp3 or .ogg files?

Link to comment
Share on other sites

I'm not understanding everything you said. Especially about

Notice, I do not really care if they the files need to be in one dir, or two dirs.

 

It looks like the fields would always be in one directory. Anyway, I expect your problem is that the curly braces are in the wrong positions. Also, why hardcode the directory in that line when you made the directory a variable above?

echo "mp3: \"{$directory}{$file_name}\"<br>\n";

http://themidlifecrisis.eu/musicplayer/ - Look at the source code. In the JS header - that's how I want the final output to be like, and of course way more songs. How would I do so?

Link to comment
Share on other sites

Do you have an mp3 AND an ogg version of EVERY file? if so, then just do a search for mp3 (or ogg) files and build the output from that. If not, then you will need to process the jpb and ogg files into an array or some structure to verify those that are paired up and those that aren't. Then either create output only for those that are paired or allow them to be in the output as an unmatched pair.

 

A couple other things. your logic int he while loop is overly complicated. Just check for the file types you are interested in instead of trying to program around all the other types. Also, the sort() function only returns true/false. It will sort the original array. So, the following lines that try to foreach() over the result will fail (unless I am reading something wrong)

 

This might get you closer

//Define directory to scan
$directory='./music/';
//Define acceptable file types
$extensions = array('jpg', 'ogg');

$filesArray = array();
$dir_handle = @opendir($directory) or die("There is an error with your file directory!");

while ($file = readdir($dir_handle)) 
{
    $filePath = $directory.$file;
    $info = pathinfo($filePath);
    if(in_array(strtolower($info['extension'])), $extensions)
    {
        $filesArray[$info['filename']][$info['extension']] = $filePath;
    }
}

sort($files_array, SORT_STRING);

$path_parts = pathinfo('musicogg');
foreach ($filesArray as $fileName => $files)
{
    if(count($files)==2)
    {
        echo "{mp3:\"{$files['mp3']}\",ogg:\"{$files['ogg']}\"}\n";
    }
}

Link to comment
Share on other sites

What you should do is to use glob () to get all of the files & folders, within a recursive function to follow the sub-folders. Within it you'll need to construct an array with the following pattern:

$Songs[] = array ('mp3' => $pathMP3, 'ogg' => $pathOGG);

 

Once the function has run through all of the folders, you'll have a 2-dimensional array containing the information you need. Next step is to simply JSON encode it, and send the results to the client.

Link to comment
Share on other sites

getting this error "Parse error: syntax error, unexpected ','"  ... any tips?

 

Spot the difference:

if(in_array(strtolower($info['extension'])), $extensions) - original

if(in_array(strtolower($info['extension']), $extensions)) - fixed

 

As for getting the right array or JSON, the replies above should help you out.

 

Also, are all of the files in one folder? The guys above aren't sure whether you need to get files from one directory, several directories, or any level of directories.

Link to comment
Share on other sites

It may not matter to you, but when writing code it matters a whole lot. Since you prefer having them in separate directories, within one master directory (I presume/hope), then you'll need to use the recursive function as I described above.

So, two directories is simpler? As more simple is better & also, I don't know anything about JSON :L

Link to comment
Share on other sites

So, two directories is simpler? As more simple is better...

 

No, not simpler.

 

YOU need to decide what makes sense for the problem at hand. If you want to put all the files in one directory it definitely makes the code simpler. But, if you don't that's fine too. You would just need to modify the code accordingly. A recursive function would make sense if all the files are in directories under a parent directory. But, if there are many other directories under that same parent that are not for music files that probably won't make sense. In that case, I'd define the "music" directories within an array and then process each of those in the code.

 

But, on further consideration, I would ask how often you expect the files to change? It seems a waste to regenerate the javascript data for hundreds of files every time you load the web page. It might make sense to instead create a script to process the files and write a file with the data needed by the JavaScript. Then, include that code into the HTML page when you load it. If your music files change then rerun the processing script.

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.