About directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • roby

    #1

    About directory

    Hello everyone, I'm a beginner in PHP and I want to ask about
    directory. How to check correctly if the directory already exist in the
    webroot? I've tried with this syntax:
    ....
    bool $state = mkdir( "test" );
    if ( $state == false ) chdir( "test" );
    ....
    but on the page there is a warning. How to solve this problem? Thanks
    in advance.

  • Michael Fesser

    #2
    Re: About directory

    ..oO(roby)
    >Hello everyone, I'm a beginner in PHP and I want to ask about
    >directory. How to check correctly if the directory already exist in the
    >webroot?
    Checks whether a file or directory exists


    Micha

    Comment

    • Noodle

      #3
      Re: About directory


      roby wrote:
      Hello everyone, I'm a beginner in PHP and I want to ask about
      directory. How to check correctly if the directory already exist in the
      webroot? I've tried with this syntax:
      ...
      bool $state = mkdir( "test" );
      if ( $state == false ) chdir( "test" );
      ...
      but on the page there is a warning. How to solve this problem? Thanks
      in advance.
      Or you could do something like this if you don't need the state
      recorded...

      <?

      if(is_dir("test ")){
      chdir("test");
      }

      ?>

      Comment

      • Noodle

        #4
        Re: About directory


        roby wrote:
        Hello everyone, I'm a beginner in PHP and I want to ask about
        directory. How to check correctly if the directory already exist in the
        webroot? I've tried with this syntax:
        ...
        bool $state = mkdir( "test" );
        if ( $state == false ) chdir( "test" );
        ...
        but on the page there is a warning. How to solve this problem? Thanks
        in advance.
        Try using the function is_dir() to test if a dir exists and is of type
        directory.



        <?php
        mkdir("test");
        if(is_dir("test ")){
        chdir("test");
        }
        ?>

        Comment

        • roby

          #5
          Re: About directory

          Thanks for the help... it works.
          Noodle wrote:
          roby wrote:
          Hello everyone, I'm a beginner in PHP and I want to ask about
          directory. How to check correctly if the directory already exist in the
          webroot? I've tried with this syntax:
          ...
          bool $state = mkdir( "test" );
          if ( $state == false ) chdir( "test" );
          ...
          but on the page there is a warning. How to solve this problem? Thanks
          in advance.
          >
          Try using the function is_dir() to test if a dir exists and is of type
          directory.
          >

          >
          <?php
          mkdir("test");
          if(is_dir("test ")){
          chdir("test");
          }
          ?>

          Comment

          • Carl Pearson

            #6
            Re: About directory

            roby wrote:
            Hello everyone, I'm a beginner in PHP and I want to ask about
            directory. How to check correctly if the directory already exist in the
            webroot? I've tried with this syntax:
            ...
            bool $state = mkdir( "test" );
            if ( $state == false ) chdir( "test" );
            ...
            but on the page there is a warning. How to solve this problem? Thanks
            in advance.
            >
            As already mentioned, is_dir might be a better way to handle such tests.

            A couple of things, though. One, that logic seems suspect. Is the
            warning perhaps one of not being able to change the directory? Your
            code as written seems to only try to change dirs if it was *NOT* made.

            Also, PHP is pretty smart. You don't always need to type variables
            (though if you want to, it doesn't hurt anything).

            // A slight re-working of your code.
            //
            // Will automatically return true or false based on mkdir success.
            $state = mkdir("test");

            // This will actually fail, as "false" will mean the
            // directory was NOT created...
            if (!$state)
            {
            chdir("test");
            }

            // Still incorrect logic, but here is your code all in one statement...
            if (!mkdir("test") )
            {
            chdir("test");
            }


            // You could also have put it on one line...
            if (!mkdir("test") ) chdir("test");

            I personally like the brackets, they *do* take up a tiny amount more
            code but provide a good visual for the logic. Your mileage may vary.

            Comment

            Working...