Jump to content

Create a list of filtered mp3 by specific file name from directory and with dropdown move to another target directory.


Recommended Posts

What I have...hope someone can help, sorry about the messy code ill clean it up.

The idea is to filter a file by first part of the filename dave mohan - song.mp3 and move only that to the target directory. 

//LIST FILES WITH DROPDOWN SELECTOR

<?php

class IgnorePathFilterIterator extends \FilterIterator {
    public function accept(): bool {
        $current = strtolower(parent::current()->getFilename());
        return !in_array($current, ['thumbs.db', 'tag', 'playlist.m3u']);
    }
}

$dir = 'C:\\EZSTREAM\\smash2\\';
//$dir = '.';
$rdi = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
$rii = new \RecursiveIteratorIterator($rdi, \RecursiveIteratorIterator::CHILD_FIRST);
$iterator = new IgnorePathFilterIterator($rii);

foreach($iterator as $path => $info) {
    if ($info->isDir()) {
        echo "DIR $path<br />\n";
    } else {
        echo "FILE $path<br />\n";
    }
}
?>
<hr>
//My thingy filtering out only the dave mohan mp3 files<br>
<?php
$dir = ('C:\\EZSTREAM\\SMASH2\\');
$iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $filename => $fileInfo) {
    if ($fileInfo->isDir()) {
     
    //probally still want to use this somewhere
    //$ignore = Array(".", "..", "Thumbs.db" , "tag", "playlist.m3u");
    
    } else {
        if (preg_match("/(\A[Dave Mohan].*.mp3)/", $fileInfo->getFileName())) {
   //echo $filename . '<br/>';
    
    echo "<a href='?file=".$filename."'>$filename</a> <form action=\"removesmash2.php\" style=\"display: inline\" method=\"post\"><input type=\"hidden\" name=\"filename\" value=\"".htmlentities($curfile)."\" />";
           pickstation();
           
           echo " <input style=\"padding: 1.26em; line-height: 0.899em; height: auto\" type=\"submit\" value=\"move\" /></form> (move this song into the archive or trash)</br>\n <hr> ";
        
}
        }
    }

?>

//My problem is the removesmash2.php below throws and error I cant seem to solve...

<?php
   //set folder to read from
   //$dirname = "C:\\EZSTREAM\\SMASH2\\";

$dir = ('C:\\EZSTREAM\\SMASH2\\');
$iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $filename => $fileInfo) {
    if ($fileInfo->isDir()) {


   //read files in folder
   //$files = scandir($dir);
   //ignore hidden files
   //$ignore = Array(".", "..", "Thumbs.db", "Playlist.m3u");

   //loop through all files
   $moved = false;
   
   //foreach($files as $filename){
       //if($filename !== $_POST['filename']) continue;
       
       foreach(stationlist() as $stationname => $path) {
           if($stationname !== $_POST['station']) continue;
           
            $source = $dir . $filename;
            $destination = $path . "\\" . $filename;
            
            echo "<h2><center>Moving from this Source: $source</h2></center><br />";
            echo "<h2><center>To this Destination: $destination</h2></center><br />";
            
            rename($source, $destination);
       }
   }  
   

?>
   // for reference path selector (dropdown) looks like this

<?php
function stationlist() {
    return [
        //"REASSIGN" => "C:\\XAMPP\\HTDOCS\\MPG\\RADIO\\BSP\\ASSIGNMENT",
        "Select Destination" => "/",
        //"Select Destination" => "C:\\XAMPP\\HTDOCS\\MPGRADIO\\BSP\\DAVEMOHAN",
        "BACKUP" => "C:\\XAMPP\\HTDOCS\\MPGRADIO\\BSP\\DAVEMOHAN\\BACKUP",
        "SMASH1" => "C:\\EZSTREAM\\SMASH1",
        "SMASH2" => "C:\\EZSTREAM\\SMASH2",
        "Trash it" => "C:\\BACKUP\\TRASH\\SONGS\\DAVEMOHAN",
        
    ];
}

?>  

First please use the code icon (<>) for your code and specify PHP. The formatter makes your code easier to read. Second, you forgot to give us the error message and on which line it is occurring.

Whoops for got to do that....

What I have...hope someone can help, sorry about the messy code ill clean it up.

The idea is to filter a file by first part of the filename dave mohan - song.mp3 and move only that to the target directory. 

//LIST FILES WITH DROPDOWN SELECTOR

<?php

class IgnorePathFilterIterator extends \FilterIterator {
    public function accept(): bool {
        $current = strtolower(parent::current()->getFilename());
        return !in_array($current, ['thumbs.db', 'tag', 'playlist.m3u']);
    }
}

$dir = 'C:\\EZSTREAM\\smash2\\';
//$dir = '.';
$rdi = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
$rii = new \RecursiveIteratorIterator($rdi, \RecursiveIteratorIterator::CHILD_FIRST);
$iterator = new IgnorePathFilterIterator($rii);

foreach($iterator as $path => $info) {
    if ($info->isDir()) {
        echo "DIR $path<br />\n";
    } else {
        echo "FILE $path<br />\n";
    }
}
?>


<hr>
//My thingy filtering out only the dave mohan mp3 files<br>

<?php
$dir = ('C:\\EZSTREAM\\SMASH2\\');
$iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $filename => $fileInfo) {
    if ($fileInfo->isDir()) {
     
    //probally still want to use this somewhere
    //$ignore = Array(".", "..", "Thumbs.db" , "tag", "playlist.m3u");
    
    } else {
        if (preg_match("/(\A[Dave Mohan].*.mp3)/", $fileInfo->getFileName())) {
   //echo $filename . '<br/>';
    
    echo "<a href='?file=".$filename."'>$filename</a> <form action=\"removesmash2.php\" style=\"display: inline\" method=\"post\"><input type=\"hidden\" name=\"filename\" value=\"".htmlentities($curfile)."\" />";
           pickstation();
           
           echo " <input style=\"padding: 1.26em; line-height: 0.899em; height: auto\" type=\"submit\" value=\"move\" /></form> (move this song into the archive or trash)</br>\n <hr> ";
        
}
        }
    }

?>

//My problem is the removesmash2.php below throws and error I cant seem to solve...

<?php
   //set folder to read from
   //$dirname = "C:\\EZSTREAM\\SMASH2\\";

$dir = ('C:\\EZSTREAM\\SMASH2\\');
$iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $filename => $fileInfo) {
    if ($fileInfo->isDir()) {


   //read files in folder
   //$files = scandir($dir);
   //ignore hidden files
   //$ignore = Array(".", "..", "Thumbs.db", "Playlist.m3u");

   //loop through all files
   $moved = false;
   
   //foreach($files as $filename){
       //if($filename !== $_POST['filename']) continue;
       
       foreach(stationlist() as $stationname => $path) {
           if($stationname !== $_POST['station']) continue;
           
            $source = $dir . $filename;
            $destination = $path . "\\" . $filename;
            
            echo "<h2><center>Moving from this Source: $source</h2></center><br />";
            echo "<h2><center>To this Destination: $destination</h2></center><br />";
            
            rename($source, $destination);
       }
   }  
   

?>


   // for reference path selector (dropdown) looks like this

<?php
function stationlist() {
    return [
        //"REASSIGN" => "C:\\XAMPP\\HTDOCS\\MPG\\RADIO\\BSP\\ASSIGNMENT",
        "Select Destination" => "/",
        //"Select Destination" => "C:\\XAMPP\\HTDOCS\\MPGRADIO\\BSP\\DAVEMOHAN",
        "BACKUP" => "C:\\XAMPP\\HTDOCS\\MPGRADIO\\BSP\\DAVEMOHAN\\BACKUP",
        "SMASH1" => "C:\\EZSTREAM\\SMASH1",
        "SMASH2" => "C:\\EZSTREAM\\SMASH2",
        "Trash it" => "C:\\BACKUP\\TRASH\\SONGS\\DAVEMOHAN",
        
    ];
}

?>  

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\mpgradio\bsp\davemohan\removesmash2.php on line 99

foreach ($iterator as $filename => $fileInfo) {

Where is the end of that foreach loop supposed to be?

image.png.1a4d5a60b5ec37b224028109cae49199.png

Edited by Barand
  • Like 1
1 hour ago, dodgeitorelse3 said:

I don't think you need the last comma in your dropdown stationlist() function.

You're right, but it won't break anything if it is there.

For eaxample

<?php
  $a = [1,2,3,4,5,];
  
  echo '<pre>' . print_r($a, 1) . '</pre>';
?>
outputs

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

 

2 hours ago, Barand said:
foreach ($iterator as $filename => $fileInfo) {

Where is the end of that foreach loop supposed to be?

image.png.1a4d5a60b5ec37b224028109cae49199.png

Ah crap that worked for got to close, at least the page loads but still doesn't move the file selected......humm  

Ok here is a test page if this helps.

Must be something im missing in part of foreach...just not sure what..

  

<?php
session_start();
require('rotationswitch.php');
$stationlist = stationlist();
?>
										
<?php ob_start();?>
<html>
<head>
<!DOCTYPE HTML>
<html>
	<head>
		<title>MPG Radio - Music for Review</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
		<link rel="stylesheet" href="./assets/css/mainstation.css" />

	</head>

<body bgcolor="#b23c30">

	<div style="background-color:white;color:black;padding:20px;">
		<center><h2>Moving Selected Music</h2>
			<h3>Music is being moved, just a moment.</h3>
		</center><br>
<?php
   //set folder to read from
$dir = ('C:\\EZSTREAM\\SMASH2\\');
$iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $filename => $fileInfo) {
    if ($fileInfo->isDir()) {

   //loop through all files
   $moved = false;
   
   foreach(stationlist() as $stationname => $path) {
	   if($stationname !== $_POST['station']) continue;
		  
			$source = $dir . $filename;
			$destination = $path . "\\" . $filename;
			
			echo "<h2><center>Moving from this Source: $source</h2></center><br />";
			echo "<h2><center>To this Destination: $destination</h2></center><br />";
			
			rename($source, $destination);
	   }
   }  
   
}
echo "files dont move from <b>source</b> to <b>destination</b><br>";
echo "<a href='filter.php'>return for now and lets try again...</a>";
?>
</body>
</html> 
<?php ob_flush(); ?>

 

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.