Php create txt file in directory

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    We can easily create a folder in PHP, but before that, you have to check if the folder or directory already exists or not. So In this article, you will learn both to Check and Create a folder or directory in PHP. 

    Methods:

    1. file_exists(): It is an inbuilt function that is used to check whether a file or directory exists or not.
    2. is_dir(): It is also used to check whether a file or directory exists or not.
    3. mkdir() :  This function creates a directory.

    Method 1: Using file_exists() function: The file_exists() function is used to check whether a file or directory exists or not.

    Syntax:

    file_exists( $path )

    Parameters: The file_exists() function in PHP accepts only one parameter $path. It specifies the path of the file or directory you want to check.

    Return Value: It returns True on success and false on failure. 

    Example:

    PHP

    $file_path = '/user01/work/gfg.txt';

    if (file_exists($file_path)) {

        echo "The Given file already exists in GEEKSFORGEEKS directory";

    }

    else {

        echo "The file path doesn't exists in GeeksforGeeks directory";

    }

    ?>

    Output

    The file path doesn't exists in GeeksforGeeks directory

    Method 2: Using is_dir() function: The is_dir() function is used to check whether the specified file is a directory or not.

    Syntax:

    is_dir( $file )

    Parameters: The is_dir() function in PHP accepts only one parameter. It specifies the path of the file or directory that you want to check.

    Return Value: It returns True if the file is a directory otherwise returns false.

    Example:

    PHP

    if (is_dir($gfg_directory))

        echo ("Given $gfg_directory exists in GeeksforGeeks Portal");

    else

        echo ("Given $gfg_directory doesn't exists in GeeksforGeeks Portal");

    ?>

    Output

    Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal

    Method 3: Using mkdir() function: The mkdir() creates a new directory with the specified pathname.

    Syntax:

    mkdir(path, mode, recursive, context)

    Parameters:

    • path: It is a mandatory parameter that specifies the path.
    • mode: It is an optional parameter that specifies permission. The mode parameter consists of four numbers and by default, the mode is 0777.
      • The first number is always zero.
      • The second number specifies permissions for the owner.
      • The third number specifies permissions for the owner’s user group.
      • The fourth number specifies permissions for everybody else.
    • recursive: It is an optional parameter that can be used to set recursive mode.
    • context: It is an optional parameter that specifies the behavior of the stream.
      • Return Value: It returns true on success or false on failure. 

        Example:

        PHP

        mkdir("/documents/gfg/articles/", 0770, true)

        ?>

        Output:

        1

        Example: This example checks the file exists or not and if file doesn’t exist then create a new file using mkdir() function.

        PHP

        $file_path = '/user01/work/gfg.txt';

        if (!file_exists($file_path)) {

            mkdir($file_path, 0777, true);

        }

        else {

            echo "The Given file path already exists";

        }

        ?>

        Output:

        1

    Can PHP create a file?

    PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

    How can save file in specific folder in PHP?

    php $target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

    How do I create an index PHP file?

    How to Use Index..
    Right-click the HTML file you want to convert. Click "Open With," then click "Notepad." After Notepad and the code loads, click "Save As" to open the dialog window..
    Type "index. ... .
    Upload the file to your Web host. ... .
    Set the new index.php file as the default Web page..

    How do I save a Notepad file as PHP?

    This is done by navigating to File > Save As... In Notepad, add . php to the end of the filename and enclose in double quotations. This ensures the file will not be converted into a basic text file by Notepad.