jasonc310771 Posted December 17, 2022 Share Posted December 17, 2022 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/315655-try-not-working-even-the-ones-i-copy-as-is-from-forums/ Share on other sites More sharing options...
kicken Posted December 17, 2022 Share Posted December 17, 2022 What error are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/315655-try-not-working-even-the-ones-i-copy-as-is-from-forums/#findComment-1603669 Share on other sites More sharing options...
jasonc310771 Posted December 18, 2022 Author Share Posted December 18, 2022 I placed all the code in the try, a few tweaks and its working. Quote Link to comment https://forums.phpfreaks.com/topic/315655-try-not-working-even-the-ones-i-copy-as-is-from-forums/#findComment-1603696 Share on other sites More sharing options...
ginerjm Posted December 18, 2022 Share Posted December 18, 2022 You really shouldn't be using @ signs Quote Link to comment https://forums.phpfreaks.com/topic/315655-try-not-working-even-the-ones-i-copy-as-is-from-forums/#findComment-1603697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.