Something is wrong with this global var/method?

Here is the full code that’s bugged.
http://pastebin.com/yMR8xP5J

Here is the full code that is not bugged.
http://pastebin.com/0QXGPACi

I’m new to global variables and methods so I posted the full code as I
don’t know which part is causing it to not work.

The code that’s bugged might be somewhere on line 49-53 OR 68-69.
OR maybe the method save_file_to_dir is flawed.

The variable my eyes are on is $mix_both
As it will hold the new name to create a file.

It is supposed to do work like this

if file exists create new one to save $output_file & $input_file’s value
else just save it on un-existing/empty file

But it will just keep overwriting the same file instead of creating a
new one.

Everything runs as expected when I don’t use the save_file_to_dir method
but I really need it to work.

Could someone help me point out the part that’s flawed or am I missing
something?

Thanks.