Jump to content

try not working - even the ones I copy as is from forums


jasonc310771

Recommended Posts

I am new to TRY, I am wanting to scan all files, but if one fails say the file name contains invalid characters then catch the error and still continue to the next foreach loop.  But this just shows the error and stops.  What is the correct way to do this ?

<?php

$dir = "C:/Users/RH/Desktop";

$flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS;// some flags to filter . and .. and follow symlinks
$iterator = new \RecursiveDirectoryIterator($dir, $flags);// create a simple recursive directory iterator
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);// make it a truly recursive iterator



foreach ($iterator as $path) {
	if ($path->isDir()){
	continue;
	}

		try {
			@$mdatetime = date_format(DateTime::createFromFormat('U', filemtime($path)), 'Y-m-d H:i:s');
			} catch (Exception $e) {
			// store in separate table error files.
			echo "<br><br><br>display my error text<br>" . $path . "<br><br><br>";
			}
			
	@$cdatetime = date_format(DateTime::createFromFormat('U', filectime($path)), 'Y-m-d H:i:s');
	//echo("path " . $path . "<br>");
	//echo("info of path " . gettype($path) . "<br>");
	@$sizebytes = @$path->getSize();
	$sizemb = ($sizebytes/1024/1024); // this is in MB
	$sizegb = ($sizemb/1024); // this is in GB
	$ext = pathinfo($path, PATHINFO_EXTENSION);
	echo($path . "<br>");
}

echo("still running");
?>

 

Link to comment
Share on other sites

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.