Jump to content

Prevent script interruption while importing files


manlio
 Share

Recommended Posts

Sorry for the dumb question, but I'm importing some external files into a file field using something like this (it is simplified and $fileurl is changing for every item)

foreach ($items as $item) {
	$item->scheda_tecnica = $fileurl;
	$item->save(); 
}

The script works perfectly but when there is a missing or wrong URL the script stops.

I would like to complete the task and echoing the item that caused the problem.

How can I manage this kind of error?

Thank you!

Link to comment
Share on other sites

You can do this:

$failed_files = array();
foreach($items as $item) {
    if(file_exists($fileurl)) {
        $item->scheda_tecnica = $fileurl;
        $item->save();
    } else {
        $failed_files[] = $fileurl;
    }
}
// when finished, echo/log the contents of $failed_files

 

  • Like 3
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...