dragon7713 2012-12-13 12:16
浏览 259

PHP mkdir - 抛出错误“文件存在”

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

  • 写回答

3条回答 默认 最新

  • drqvsx1228 2012-12-13 12:27
    关注

    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));
                }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决