Home > other >  Delete some part of filenames from a folder using index with PHP
Delete some part of filenames from a folder using index with PHP

Time:02-05

I have been studying PHP. and I came across a thought if there is a way to delete just some part of the file names using the filename's index.

For example, if the files are named as 

    abcdefghijk.jpg
    lmnopqrstuv.jpg 

and Lets say we want to delete from 6th character until 11th character, which means from "f" until "k" in 1st and "q" until "v" in 2nd.

and the output should be

abcde.jpg
lmnop.jpg 

CodePudding user response:

<?php

$files = array("abcdefghijk.jpg", "lmnopqrstuv.jpg") ;



foreach($files as $file)
  {
$a = substr($file,0,4);

$b = substr($file,-4);

$c = $a.$b;

echo $c;
echo "\r\n";
            }

?>

You can use rename() with $file as old name and $c as new name

Output

abcde.jpg lmnop.jpg 
  •  Tags:  
  • Related