jazzyy22 jazzyy22 - 3 months ago 12
PHP Question

PHP code that splits the file name, and rename it with contents in the file

I have many files, and some of which contain "5010" in there name, usually when I see these numbers, I open the file and get the code next to the name REF and paste it instead of the 5010, and send it to the server, however I'm suppose to do it through a php script that would be able to do this task on its own, Any ideas on where to start with this?

For example my file name is

"hasdbgf.5010.dfgur.fde"


I want to remove the "5010", so I open the file:

DTM*405*20160822~
N1*PR*AETNA~
N3*151 FARMINGTON AVENUE~
N4*HARTFORD*CT*06156~
REF*2U*60054~


I see that after REF*2U is "60054", so I rename it:

"hasdbgf_60054_dfgur.fde"

Answer

I have wrote this script in 5 minutes. Just put it inside your directory where you would like renaming operation to be done. That is all.

//expression to be found in file name
$find = '.5010.';

//directory name
//we will store renamed files here
$dirname = 'renamed_5010';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

//read all files from a directory
//skip directories
$dh  = opendir('.');
$files = [];
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, ['.', '..']) || is_dir($filename))
        continue;

    $files[] = $filename;
}

//iterate collected files
foreach($files as $file)
{
    //check if file name is matching $find
    if(stripos($file, $find) !== false)
    {

        //open file
        $handle = fopen($file, "r");
        if ($handle)
        {
            //read file, line by line
            while (($line = fgets($handle)) !== false)
            {

                //find REF line
                if(stripos($line, 'REF') !== false)
                {
                    //we are going to REF line reverse
                    $reverse = strrev($line);
                    $found = false;
                    $refnumber = [];

                    //find reference number
                    for($i = 0; $i < strlen($reverse); $i++)
                    {

                        if(is_numeric($reverse[$i]))
                        {
                            $found = true;
                            $refnumber[] = $reverse[$i];
                        }

                        if($found == true && !is_numeric($reverse[$i]))
                            break;
                    }

                    //glue refernce numbers
                    //check if reference number is not empty
                    $refnumber = strrev(join('', $refnumber));
                    if(!empty($refnumber))
                    {
                        $refnumber = '_' . $refnumber . '_';
                        $filerenamed = str_replace($find, $refnumber, $file);
                        copy($file, $dirname . '/' . $filerenamed);
                    }

                    echo $refnumber . "\n";
                }
            }

            //close file
            fclose($handle);
        }
    }
}