PHP Unzip function works fine, but not if you call it from httpdocs/
<p>I'm kinda stuck with a problem and i hope someone here can shine some light. </p>

<p>I am using some functions to unzip a .zip file and its folders, which works fine. But if i try to use the function from any other directory it doesn't seem to work or doesn't seem to create the folder dirs and write the files.</p>

<p>I'm using the following code to unzip a .zip file.</p>

<pre><code>&lt;?PHP error_reporting(e_all);

include("../../includes/config.php");


/**
 * Unzip the source_file in the destination dir
 *
 * @param   string      The path to the ZIP-file.
 * @param   string      The path where the zipfile should be unpacked, if false the directory of the zip-file is used
 * @param   boolean     Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
 * @param   boolean     Overwrite existing files (true) or not (false)
 * 
 * @return  boolean     Succesful or not
 */

function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
{
  if ($zip = zip_open($src_file))
  {
    if ($zip)
    {
      $splitter = ($create_zip_name_dir === true) ? "." : "/";
      if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";

      // Create the directories to the destination dir if they don't already exist
      create_dirs($dest_dir);

      // For every file in the zip-packet
      while ($zip_entry = zip_read($zip))
      {
        // Now we're going to create the directories in the destination directories

        // If the file is not in the root dir
        $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
        if ($pos_last_slash !== false)
        {
          // Create the directory where the zip-entry should be saved (with a "/" at the end)
          create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
        }

        // Open the entry
        if (zip_entry_open($zip,$zip_entry,"r"))
        {

          // The name of the file to save on the disk
          $file_name = $dest_dir.zip_entry_name($zip_entry);

          // Check if the files should be overwritten or not
          if ($overwrite === true || $overwrite === false &amp;&amp; !is_file($file_name))
          {
            // Get the content of the zip entry
            $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

            file_put_contents($file_name, $fstream );
            // Set the rights
            chmod($file_name, 0777);
            echo "save: ".$file_name."&lt;br /&gt;";
          }

          // Close the entry
          zip_entry_close($zip_entry);
        }      
      }
      // Close the zip-file
      zip_close($zip);
    }
  }
  else
  {
    return false;
  }

  return true;
}

/**
 * This function creates recursive directories if it doesn't already exist
 *
 * @param String  The path that should be created
 * 
 * @return  void
 */

function create_dirs($path)
{
  if (!is_dir($path))
  {
    $directory_path = "";
    $directories = explode("/",$path);
    array_pop($directories);

    foreach($directories as $directory)
    {
      $directory_path .= $directory."/";
      if (!is_dir($directory_path))
      {
        //mkdir($directory_path);

        my_ftp_mkdir2('httpdocs/templates/temp/', $directory_path);

        //chmod($directory_path, 0777);
      }
    }
  }
}

/* CHMOD with FTP */

function my_ftp_mkdir2 ($path, $dir) {
    $server = "ftp.domainname.com";

    // Connect to FTP server
    $connection = ftp_connect ($server);

    // Login on FTP server
    $user = "username";
    $pass = "password";
    $result = ftp_login ($connection, $user, $pass);

    if ((!$connection) || (!$result)) {
        return false;
    }
    // Go to the dir thats been given
    if (!@ftp_chdir ($connection, $path)) {
        ftp_close($connection); // close FTP connection
        return false;
    }

    // Create the map and set the rights
    if (!@ftp_mkdir($connection, $dir)) {
        ftp_close($connection); // close FTP connection
        return false;    
    }

    $chmod_cmd = "CHMOD 0777 " . $dir;
    $chmod = ftp_site($connection, $chmod_cmd);

    ftp_close($connection); // close FTP connection

    return true;
}



if (!unzip("../user1/test2.zip", false, true, true)) {
    echo 'The file has failed to be unzipped!';
} else {
    echo 'The file has been unzipped!';
}


?&gt;
</code></pre>

<p>Now this works great.</p>

<p>But im running this file in a test dir which is the following:
httpdocs/templates/temp/unzip.php</p>

<p>The file called test2.zip is located in:
httpdocs/templates/user1/test2.zip</p>

<p>So when i run the script it makes the following:
httpdocs/templates/user1/test2/index.html
httpdocs/templates/user1/test2/img/
etc etc..</p>

<p>But when i run this script from the httpdocs/ directory it says its has been unzipped but doesnt create any dirs or whatsoever.</p>

<p>My PHP has safemode on thats why i used FTP to chmod the dirs to 777 for test purpose.</p>

<p>So when i run this script in httpdocs/unzip.php</p>

<p>i call the function like this.</p>

<pre><code>&lt;?php
if (!unzip("templates/user1/test2.zip", false, true, true)) {
    echo 'The file has failed to be unzipped!';
} else {
    echo 'The file has been unzipped!';
}
?&gt;
</code></pre>

<p>But this doesn't seem to work, i hope someone can help me out with some good advice. Thanks!</p>
