PHP mkdir - throwing error "File exists"
Asked Answered
H

3

6

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

Hairbrush answered 13/12, 2012 at 12:16 Comment(1)
i think there should be else if (!is_dir($name)) since the error u\you have there is beacuse you already have a dir with that nameBrandibrandice
D
1

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));
            }
Deductible answered 13/12, 2012 at 12:27 Comment(0)
H
0

Many thanks Michael, however that did not work - throwing up the same errors.

Thanks to you, I have however fixed my problems.

In the else is statement I check again that it is not a directory:

else if (is_dir($name) && substr($fileObject->getFileName(), 0, 1) != ".")

As for the remove directory error, I simply had to add an extra method in which I would send a message to, checking if the directory is empty before trying to remove it.

Many thanks, Nick

Hairbrush answered 13/12, 2012 at 13:7 Comment(0)
S
-4

i hope this one help u
add a “@” sign in front of mkdir, so it looks like this:

@mkdir($this->workFolder."/".str_replace($path."/", "",$name));
Severin answered 28/3, 2013 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.