Jump to content

Directory iteration and changing file extensions


afish674
 Share

Recommended Posts

Hi, I'm learning PHP from a book and in one of the end of chapter tests it asks you to build a script that iterates through a directory and changes any files with the extension ".txt" to ".xtx" I struggled with this and then eventually went to the back of the book to check the authors code, which is as follows:

<?php    $dp = opendir(".") or die("Cannot open directory!");while($file = readdir($dp)){  $fileData = pathinfo($file);  if($fileData['extension'] == "txt"){   rename($file, $fileData['filename'] . ".xtx") or die("Error! Cannot rename file ".$file);  }}closedir($dp);?>

However, this code generates an error that "extension" is not a valid index. He uses this in another script earlier in the book to check image extensions. Is this because the book is for an older version of PHP or is it a mistake? How would I fix it? Thanks for any help!

Link to comment
Share on other sites

if file has not an extension it will return NULL for extension index. readdir() fetch directory also. it is possibly the directories inside target directory which is causing this. you can check if it is directory using is_dir() or if it is file using is_file(). there is a function glob() which can get all files and directories in an array and you dont need to handle the directory and loop through its resource. resource=>http://php.net/readdirhttp://php.net/pathinfohttp://php.net/is_dirhttp://php.net/is_filehttp://php.net/glob

Edited by birbal
Link to comment
Share on other sites

There are definatly some .txt files in the directory and there are no sub-directories. I changed the code to:

<?php	$dp = opendir(".") or die("Cannot open directory!");while($file = readdir($dp)){  $fileData = pathinfo($file);  echo "$fileData <br/>";  /*if($fileData['extension'] == "txt"){   rename($file, $fileData['filename'] . ".xtx") or die("Error! Cannot rename file ".$file);*/  }//}closedir($dp);?>

To see what it was passing on to the next bit and it echoed: (html)ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray(end html) Is this what is causing the problem? Because it is not returning the file names?

Edited by afish674
Link to comment
Share on other sites

you cant echo array element like that. if you echo an array it will be type casted to string 'Array'. use echo print_r($fileinfo,true); to see the array element.

Link to comment
Share on other sites

Okay, I changed "echo" to "print_r" but didn't use the true arguement because I wanted it printed to the screen so I could read it. It comes out in one long string: Array ( [dirname] => . [basename] => . [extension] => [filename] => ) Array ( [dirname] => . [basename] => .. [extension] => [filename] => . ) Array ( [dirname] => . [basename] => .project [extension] => project [filename] => ) Array ( [dirname] => . [basename] => .settings [extension] => settings [filename] => ) Array ( [dirname] => . [basename] => fruits-reversed [filename] => fruits-reversed ) Array ( [dirname] => . [basename] => fruits-reversed.xtx [extension] => xtx [filename] => fruits-reversed ) Array ( [dirname] => . [basename] => fruits.xtx [extension] => xtx [filename] => fruits ) Array ( [dirname] => . [basename] => readfileandreversecontents.php [extension] => php [filename] => readfileandreversecontents ) Array ( [dirname] => . [basename] => rename_files.php [extension] => php [filename] => rename_files ) Array ( [dirname] => . [basename] => reverse_fruit.php [extension] => php [filename] => reverse_fruit )Is there an easy way to format it? Also, it shows that there .txt file names that it can see, so I don't understand why my original code wasn't working?

Link to comment
Share on other sites

You can print it inside a <pre> block: echo '<pre>' . print_r($array, true) . '</pre>'; It looks like the array for "fruits-reversed" doesn't have an extension index. You can use isset to check for that: if(isset($fileData['extension']) && $fileData['extension'] == "txt"){

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...