What are the potential pitfalls of using substr() to manipulate file names in PHP scripts?

Using substr() to manipulate file names in PHP scripts can lead to errors if the file names are not consistent in length. It is better to use functions like pathinfo() or basename() to extract specific parts of a file name. This ensures more reliable and consistent results when working with file names.

// Example of using pathinfo() to extract filename components
$filename = "example_file.txt";
$fileInfo = pathinfo($filename);

$basename = $fileInfo['filename'];
$extension = $fileInfo['extension'];

echo "File name: $basename\n";
echo "File extension: $extension\n";