0

I know that there is a difference between the result of the function realpath between windows and unix paths

I have this function for create a zip , and i don't know how to convert it to work both on windows and unix (for now only works on unix)

function ZIP($source, $destination){

       if (!extension_loaded('zip') || !file_exists($source)) {
           return false;
       }

       $zip = new ZipArchive();

       if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
           return false;
       }

       $source = str_replace('\\', '/', realpath($source));

       if (is_dir($source) === true){
           $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

           foreach ($files as $file){
               $file = str_replace('\\', '/', $file);

               // Ignore "." and ".." folders
               if(in_array(substr($file, strrpos($file, '/')+1), array('.', '..')))
                   continue;

               $file = realpath($file);

               if (is_dir($file) === true){
                   $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
               } else if (is_file($file) === true){
                   $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
               }
           }
       }else if (is_file($source) === true){
           $zip->addFromString(basename($source), file_get_contents($source));
       }

       return $zip->close();
 }

I think DIRECTORY_SEPARATOR constant can help the conversion


UPDATE

this works on windows and unix

function Zip($source, $destination){

if (!extension_loaded('zip') || !file_exists($source)) {
    return false;
}

$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
    return false;
}

$source = str_replace('\\', '/', realpath($source));

if (is_dir($source) === true)
{
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

    foreach ($files as $file)
    {
        $file = str_replace('\\', '/', $file);

        // Ignore "." and ".." folders
        if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
            continue;

        if (is_dir($file) === true)
        {
            $zip->addEmptyDir(str_replace($source . '/', '', $file));
        }
        else if (is_file($file) === true)
        {

            $str1 = str_replace($source . '/', '', '/'.$file);
            $zip->addFromString($str1, file_get_contents($file));

        }
    }
}
else if (is_file($source) === true)
{
    $zip->addFromString(basename($source), file_get_contents($source));
}

return $zip->close();
}
WhiteLine
  • 1,919
  • 2
  • 25
  • 53
  • And why dont you use PATH_SEPARATOR AS a constant ? – B001ᛦ Feb 20 '15 at 14:39
  • @bub `DIRECTORY_SEPARATOR` is the right one to use. `PATH_SEPARATOR` is the one that seperates paths in your environment variables. – h2ooooooo Feb 20 '15 at 14:53
  • i tried to replace all the `'/'` with DIRECTORY_SEPARATOR. now the code works again on unix , but not yet perfectly on windows, because to the folder is added a `/` final, for example `folder1/` – WhiteLine Feb 20 '15 at 14:59
  • @WhiteLine Thats because you are adding the `DIRECTORY_SEPARATOR` on to the end of your paths... Try prepending to the front when you add a new directory segment instead of doing before hand. For example: `$source = '/path/to/folder'; $target = $source . '/new_dir';` – prodigitalson Feb 20 '15 at 15:12
  • @WhiteLine You can normalize all slashes by `$path = preg_replace('~[/\\]+~', DIRECTORY_SEPARATOR, $path);` – h2ooooooo Feb 20 '15 at 15:32

0 Answers0