Need to use an external variable inside a function

You’re right that using globals is inelegant at best. There are almost always better solutions that don’t depend on global variables. However, if you pause execution within your file_pull() function (e.g. using a debugger), or even just print $GLOBALS to your php error log within the scope of your function, does your $type variable show up there as part of the globals array?

If not, I’m going to guess that your “first code block” is part of some file that isn’t actually executed at the same time as the file that contains your function.

Would help to know where your code blocks are coming from. E.g. functions.php, a file in the plugins directory, etc. It may be helpful to review how WordPress loads all of its files (Between functions.php, widgets and plugins, which is loaded first?), and even how PHP executes (https://www.sitepoint.com/how-php-executes-from-source-code-to-render/).

(I’m assuming $upgrade_type in your example above was mistyped here, and wasn’t actually mistyped in your code…otherwise the root issue might be that typo.)

Good luck!