Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mkdir - throwing error "File exists"

Tags:

php

mkdir

rmdir

I am currently working through the Passbook section of iOS6 By Tutorials by the team at raywenderlich.com, and am getting the following errors in my PHP:

Warning: mkdir() [function.mkdir]: File exists in /xxx/xxx/xxx/xxx/Pass.php on line 26

Warning: mkdir() [function.mkdir]: File exists in /xxx/xxx/xxx/xxx/Pass.php on line 26

Warning: rmdir(/tmp/50c8d11c60538/..) [function.rmdir]: Directory not empty in /xxx/xxx/xxx/xxx/Pass.php on line 54

The code in my class for this is as follows:

class Pass
{
    private $workFolder = null;
    private $ID = null;

    var $content = null;
    var $passBundleFile = null;

    private function copySourceFolderFilesToWorkFolder($path)
    {
        // recurse over the contents and copy files
        $files = new RecursiveIteratorIterator(
                        new RecursiveDirectoryIterator($path),
                        RecursiveIteratorIterator::SELF_FIRST);

        foreach ($files as $name => $fileObject)
        {
            if (is_file($name) && substr($fileObject->getFileName(), 0, 1) != ".")
            {
                copy($name, $this->workFolder."/".str_replace($path."/", "",$name));
            }
            else if (is_dir($name))
            {
                mkdir($this->workFolder."/".str_replace($path."/", "",$name));
            }
        }
    }

    // delete all auto-generated files in the temp folder
    function cleanup()
    {
        // recurse over conrents and delete files
        $files = new RecursiveIteratorIterator(
                        new RecursiveDirectoryIterator($this->workFolder),
                        RecursiveIteratorIterator::CHILD_FIRST);

        foreach ($files as $name => $fileObject)
        {
            if (is_file($name))
            {
                unlink($name);
            }
            else if (is_dir($name))
            {
                rmdir($name);
            }
        }

        rmdir($this->workFolder);
    }

    function __construct($path)
    {
        assert(file_exists($path."/pass.json"));

        $this->ID = uniqid();

        $this->workFolder = sys_get_temp_dir()."/".$this->ID;
        mkdir($this->workFolder);
        assert(file_exists($this->workFolder));

        $this->copySourceFolderFilesToWorkFolder($path);

        $this->readPassFromJSONFile($this->workFolder."/pass.json");
    }

    // cleanup the temp folder on object destruction
    function __destruct()
    {
        $this->cleanup();
    }
}

And am instantiating an instance with:

$coupon = new Pass("pass/source");

I have tried uploading the sample code supplied with the book and get the same errors.

I have posted this on the relevant forum, however no one has replied as yet, and would like to get to the bottom of this before moving on.

Thanks, Nick

like image 941
Nick Avatar asked Nov 26 '22 18:11

Nick


1 Answers

Your first if tests for '.' and '..' but it will be false and go to the else. And '.' and '..' are directories. So it tries to create a directory called '.', Which already exists.

if (is_file($name) && substr($fileObject->getFileName(), 0, 1) != ".")
        {
            copy($name, $this->workFolder."/".str_replace($path."/", "",$name));
        }
        else if (is_dir($name))
        {
            mkdir($this->workFolder."/".str_replace($path."/", "",$name));
        }

Fix by doing this:

if($name == '.' || $name == '..'){// ignore '.' and '..', but not hidden files
continue;
}
if (is_file($name))
            {
                copy($name, $this->workFolder."/".str_replace($path."/", "",$name));
            }
            else if (is_dir($name))
            {
                mkdir($this->workFolder."/".str_replace($path."/", "",$name));
            }
like image 50
Michael Ozeryansky Avatar answered Dec 30 '22 05:12

Michael Ozeryansky