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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.