title: PHP solution for validating file path
body: <p>I need to validate passed file path, before it will be written to disk and new directory structure for it will be created. For example, we have <code>$path = "/uploads/base/../../user_new_dir/img.jpg"</code>. This is incorrect path due to <code>"/.." ("/uploads/base"</code> - allowed for saving directory, <code>"/user_new_dir/img.jpg"</code> - user defined path and file name).</p>
  
  <p>I can't use <code>realpath($path)</code>, because it works only with existing files. I can't use <code>realpath(dirname($path))</code>, because we may create new folders for saving file.</p>
  
  <p>I look for script, which checks all possible cases. I worry that I can miss something like multi-byte representation of the <code>'.'</code> character or others unexpected ways, which may cause security issues. Could you advice appropriate implementation of my task in any framework or cms, so I could look into it?</p>