Jump to content

Recommended Posts

Posted

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!

Posted

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
Posted

Or if it's a remote URL you might need something like this instead of file_exists.

$file_headers = @get_headers($filename);
if($file_headers[0] == 'HTTP/1.0 404 Not Found') {

 

  • Like 3

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
  • Recently Browsing   0 members

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