PHP variable containing directory path not playing well with Zip function

I have a php function that zips a directory.  I seem to be having trouble using a variable in the function relating to the directory path that I'd like to zip.

The php variable that holds the directory path is:
$cdir = $_GET['dir'];

Open in new window

When echoed, it prints the current directory path, e.g. First Folder/Second Folder

When calling the function, I can use the $cdir variable in naming the file, but when trying to use it in the path (in this case "New Employee Forms/New Employee"), , the variable is null/blank/doesn't work.

Zip('/home/nctti/www/coastal/New Employee Forms/New Employee', '/home/nctti/www/coastal/tmp_pdf/'.$cdir2.'.zip');

Open in new window


This code generates a zip of the "New Employee Forms/New Employee" folder as a file called "New_Employee_Forms+New_Employee.zip"

However, when I insert $cdir in place of "New Employee Forms/New Employee" like below, a zip of the "coastal" parent directory is made with the "New_Employee_Forms+New_Employee.zip name"

Zip('/home/nctti/www/coastal/'.$cdir.'', '/home/nctti/www/coastal/tmp_pdf/'.$cdir2.'.zip');

Open in new window


Why is this?  Full code below.  Thank you for any help!

<?php
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();
}
$parent = basename(dirname($_SERVER['PHP_SELF']));
$cdir = $_GET['dir'];
$rep1 = [" ","/"];
$rep2 = ["_","+"];
$cdir2 = str_replace($rep1, $rep2, $cdir );
if (!empty($cdir)) {
Zip('/home/nctti/www/coastal/New Employee Forms/New Employee', '/home/nctti/www/coastal/tmp_pdf/'.$cdir2.'.zip');
}
?>

Open in new window

mynameisjustinAsked:
Who is Participating?
 
Julian HansenCommented:
There is nothing wrong with what you are doing. Try this
Zip('/home/nctti/www/coastal/'.$cdir, '/home/nctti/www/coastal/tmp_pdf/'.$cdir2.'.zip');

Open in new window

Or this
Zip("/home/nctti/www/coastal/{$cdir}", "/home/nctti/www/coastal/tmp_pdf/{$cdir2}.zip");

Open in new window

0
 
rgranlundCommented:
Have you tried using tmp_pdf in the initial directory address? I'm just guessing.
0
 
Dave BaldwinFixer of ProblemsCommented:
I would strongly recommend that you do not put spaces in folder and file names.  They will always be a problem.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Julian HansenCommented:
If that does not work I am guessing there is something wrong with your cdir variable
Do a dump of the variable in [] so we can see what is actually there

echo "[{$cdir}]";

Open in new window

0
 
mynameisjustinAuthor Commented:
Thank you Julian.  Both examples of your code worked.    I realized my error as a result of your post.  The period and two apostrophes after  $cdir was the issue.
0
 
Julian HansenCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.