Custom PHP script throws critical error ONLY when editing page

When you’re editing the page, you’re in the directory wp-admin, and wp-admin doesn’t contain wp-content (they’re both children of your site’s root directory).

Instead of 'wp-content/uploads/cams/', I’d recommend ABSPATH . 'wp-content/uploads/cams/' which should work wherever you might be in the WordPress environment.