Hướng dẫn php copy file create directory if not exists - php copy file tạo thư mục nếu không tồn tại

Tôi cần mã PHP đơn giản để sao chép một tệp và tạo thư mục nếu nó không tồn tại (PHP).

Example:

$f1 = "x.txt";
$f2 = "a/b.txt";
mycopy($f1, $f2);

Bản sao của tôi sẽ đảm bảo thư mục tồn tại (tức là, tạo nó nếu cần thiết) và sao chép tệp.

Nỗ lực của tôi:

function mycopy($s1, $s2) {
    $path = pathinfo($s2);
    if (!file_exists($path['dirname'])) {
        mkdir($path['dirname'], 0777, true);
    }
    if (!copy($s1, $s2)) {
        echo "copy failed \n";
    }
}

Số thứ ba chỉ định quyền cho nhóm người dùng của chủ sở hữu.

Số thứ tư chỉ định quyền cho mọi người khác.

Kiếm lại: Đây là một tham số tùy chọn có thể được sử dụng để đặt chế độ đệ quy.

  1. Bối cảnh: Đây là một tham số tùy chọn chỉ định hành vi của luồng.
  2. Giá trị trả về: nó trả về đúng khi thành công hoặc sai khi thất bại. & Nbsp;
  3. Ví dụ: Ví dụ này kiểm tra tệp có tồn tại hay không và nếu tệp không tồn tại thì hãy tạo một tệp mới bằng hàm mkdir ().

function mycopy($s1, $s2) {
    $path = pathinfo($s2);
    if (!file_exists($path['dirname'])) {
        mkdir($path['dirname'], 0777, true);
    }
    if (!copy($s1, $s2)) {
        echo "copy failed \n";
    }
}
6
is_dir( $file )
6
function mycopy($s1, $s2) {
    $path = pathinfo($s2);
    if (!file_exists($path['dirname'])) {
        mkdir($path['dirname'], 0777, true);
    }
    if (!copy($s1, $s2)) {
        echo "copy failed \n";
    }
}
8
function mycopy($s1, $s2) {
    $path = pathinfo($s2);
    if (!file_exists($path['dirname'])) {
        mkdir($path['dirname'], 0777, true);
    }
    if (!copy($s1, $s2)) {
        echo "copy failed \n";
    }
}
7__121

2
The file path doesn't exists in GeeksforGeeks directory
5
function mycopy($s1, $s2) {
    $path = pathinfo($s2);
    if (!file_exists($path['dirname'])) {
        mkdir($path['dirname'], 0777, true);
    }
    if (!copy($s1, $s2)) {
        echo "copy failed \n";
    }
}
7__12

2
3 
mkdir(path, mode, recursive, context)
1
function mycopy($s1, $s2) {
    $path = pathinfo($s2);
    if (!file_exists($path['dirname'])) {
        mkdir($path['dirname'], 0777, true);
    }
    if (!copy($s1, $s2)) {
        echo "copy failed \n";
    }
}
5

//The name of the directory that we need to create.
$directoryName = './images/users/uploads/';

//Check if the directory already exists.
if(!is_dir($directoryName)){
    //Directory does not exist, so lets create it.
    mkdir($directoryName, 0755, true);
}

Làm thế nào để bạn tạo một thư mục nếu không tồn tại trong PHP?

  1. Phương pháp 3: Sử dụng hàm mkdir (): mkdir () tạo một thư mục mới với tên đường dẫn được chỉ định ..
  2. Chúng tôi đặt tham số thứ ba trên MKDIR thành true. Tham số thứ ba cho MKDIR là đệ quy: & nbsp; cho phép tạo các thư mục lồng nhau được chỉ định trong tên đường dẫn.

Nếu các người dùng của người dùng trên mạng và các bộ phận phụ tải lên trên mạng không tồn tại, thì MKDIR sẽ tự động tạo chúng.

Số thứ ba chỉ định quyền cho nhóm người dùng của chủ sở hữu.

Số thứ tư chỉ định quyền cho mọi người khác.

Kiếm lại: Đây là một tham số tùy chọn có thể được sử dụng để đặt chế độ đệ quy.

  • Bối cảnh: Đây là một tham số tùy chọn chỉ định hành vi của luồng.
  • Giá trị trả về: nó trả về đúng khi thành công hoặc sai khi thất bại. & Nbsp;
  • Số thứ ba chỉ định quyền cho nhóm người dùng của chủ sở hữu.

    Số thứ tư chỉ định quyền cho mọi người khác.

    Kiếm lại: Đây là một tham số tùy chọn có thể được sử dụng để đặt chế độ đệ quy.

    Bối cảnh: Đây là một tham số tùy chọn chỉ định hành vi của luồng.

    Methods:

    1. file_exists (): Đây là một hàm sẵn được sử dụng để kiểm tra xem một tệp hoặc thư mục có tồn tại hay không.: It is an inbuilt function that is used to check whether a file or directory exists or not.
    2. is_dir (): Nó cũng được sử dụng để kiểm tra xem một tệp hoặc thư mục có tồn tại hay không.: It is also used to check whether a file or directory exists or not.
    3. mkdir (): & nbsp; Hàm này tạo ra một thư mục.  This function creates a directory.

    Phương pháp 1: Sử dụng hàm file_exists (): hàm file_exists () được sử dụng để kiểm tra xem tệp hoặc thư mục có tồn tại hay không. The file_exists() function is used to check whether a file or directory exists or not.

    Syntax:

    file_exists( $path )

    Tham số: Hàm file_exists () trong PHP chỉ chấp nhận một đường dẫn $ tham số. Nó chỉ định đường dẫn của tệp hoặc thư mục bạn muốn kiểm tra.The file_exists() function in PHP accepts only one parameter $path. It specifies the path of the file or directory you want to check.

    Giá trị trả về: nó trả về đúng khi thành công và sai về thất bại. & Nbsp;It returns True on success and false on failure. 

    Example:

    PHP

    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    1

    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    2
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    3
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    4
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    5

    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    6
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    7
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    8
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    7__121

    2
    3 
    4
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    5

    6

    7 
    8

    2
    3 
    //The name of the directory that we need to create.
    $directoryName = './images/users/uploads/';
    
    //Check if the directory already exists.
    if(!is_dir($directoryName)){
        //Directory does not exist, so lets create it.
        mkdir($directoryName, 0755, true);
    }
    1
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    5

    6

    //The name of the directory that we need to create.
    $directoryName = './images/users/uploads/';
    
    //Check if the directory already exists.
    if(!is_dir($directoryName)){
        //Directory does not exist, so lets create it.
        mkdir($directoryName, 0755, true);
    }
    4

    Đầu ra

    The file path doesn't exists in GeeksforGeeks directory

    Phương pháp 2: Sử dụng hàm is_dir (): Hàm is_dir () được sử dụng để kiểm tra xem tệp được chỉ định có phải là thư mục hay không.is_dir() function: The is_dir() function is used to check whether the specified file is a directory or not.

    Syntax:

    is_dir( $file )

    Tham số: Hàm is_dir () trong PHP chỉ chấp nhận một tham số. Nó chỉ định đường dẫn của tệp hoặc thư mục mà bạn muốn kiểm tra.The is_dir() function in PHP accepts only one parameter. It specifies the path of the file or directory that you want to check.

    Giá trị trả về: Nó trả về true nếu tệp là một thư mục khác trả về sai.It returns True if the file is a directory otherwise returns false.

    Example:

    PHP

    //The name of the directory that we need to create.
    $directoryName = './images/users/uploads/';
    
    //Check if the directory already exists.
    if(!is_dir($directoryName)){
        //Directory does not exist, so lets create it.
        mkdir($directoryName, 0755, true);
    }
    5

    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    2
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    3
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    4
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    5

    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    6
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    7
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    8
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    7__121

    7

    2
    3 
    4
    function mycopy($s1, $s2) {
        $path = pathinfo($s2);
        if (!file_exists($path['dirname'])) {
            mkdir($path['dirname'], 0777, true);
        }
        if (!copy($s1, $s2)) {
            echo "copy failed \n";
        }
    }
    
    5

    //The name of the directory that we need to create.
    $directoryName = './images/users/uploads/';
    
    //Check if the directory already exists.
    if(!is_dir($directoryName)){
        //Directory does not exist, so lets create it.
        mkdir($directoryName, 0755, true);
    }
    4

    Đầu ra

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

    Phương pháp 2: Sử dụng hàm is_dir (): Hàm is_dir () được sử dụng để kiểm tra xem tệp được chỉ định có phải là thư mục hay không.mkdir() function: The mkdir() creates a new directory with the specified pathname.

    Syntax:

    mkdir(path, mode, recursive, context)

    Parameters:

    • Tham số: Hàm is_dir () trong PHP chỉ chấp nhận một tham số. Nó chỉ định đường dẫn của tệp hoặc thư mục mà bạn muốn kiểm tra. It is a mandatory parameter that specifies the path.
    • Giá trị trả về: Nó trả về true nếu tệp là một thư mục khác trả về sai. It is an optional parameter that specifies permission. The mode parameter consists of four numbers and by default, the mode is 0777.
      • function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        6
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        7
        //The name of the directory that we need to create.
        $directoryName = './images/users/uploads/';
        
        //Check if the directory already exists.
        if(!is_dir($directoryName)){
            //Directory does not exist, so lets create it.
            mkdir($directoryName, 0755, true);
        }
        8
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        7
        file_exists( $path )
        0
        file_exists( $path )
        1
      • 2
        3 
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        7
        file_exists( $path )
        5
        file_exists( $path )
        6
      • 2
        3 
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        7
        The file path doesn't exists in GeeksforGeeks directory
        1
        file_exists( $path )
        6
      • Phương pháp 3: Sử dụng hàm mkdir (): mkdir () tạo một thư mục mới với tên đường dẫn được chỉ định.
    • Đường dẫn: Đó là một tham số bắt buộc chỉ định đường dẫn.It is an optional parameter that can be used to set recursive mode.
    • Chế độ: Đây là một tham số tùy chọn chỉ định quyền. Tham số chế độ bao gồm bốn số và theo mặc định, chế độ là 0777. It is an optional parameter that specifies the behavior of the stream.
      • Số đầu tiên luôn bằng không. It returns true on success or false on failure. 

        Example:

        PHP

        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        1

        The file path doesn't exists in GeeksforGeeks directory
        5
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        7
        The file path doesn't exists in GeeksforGeeks directory
        7
        The file path doesn't exists in GeeksforGeeks directory
        8

        //The name of the directory that we need to create.
        $directoryName = './images/users/uploads/';
        
        //Check if the directory already exists.
        if(!is_dir($directoryName)){
            //Directory does not exist, so lets create it.
            mkdir($directoryName, 0755, true);
        }
        4

        Output:

        1

        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        2
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        3
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        4
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        5
        This example checks the file exists or not and if file doesn’t exist then create a new file using mkdir() function.

        PHP

        //The name of the directory that we need to create.
        $directoryName = './images/users/uploads/';
        
        //Check if the directory already exists.
        if(!is_dir($directoryName)){
            //Directory does not exist, so lets create it.
            mkdir($directoryName, 0755, true);
        }
        5

        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        2
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        3
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        4
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        5

        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        6
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        7
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        8
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        7__121

        2
        3 
        4
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        5

        6

        7 
        8

        2
        3 
        //The name of the directory that we need to create.
        $directoryName = './images/users/uploads/';
        
        //Check if the directory already exists.
        if(!is_dir($directoryName)){
            //Directory does not exist, so lets create it.
            mkdir($directoryName, 0755, true);
        }
        1
        function mycopy($s1, $s2) {
            $path = pathinfo($s2);
            if (!file_exists($path['dirname'])) {
                mkdir($path['dirname'], 0777, true);
            }
            if (!copy($s1, $s2)) {
                echo "copy failed \n";
            }
        }
        
        5

        6

        //The name of the directory that we need to create.
        $directoryName = './images/users/uploads/';
        
        //Check if the directory already exists.
        if(!is_dir($directoryName)){
            //Directory does not exist, so lets create it.
            mkdir($directoryName, 0755, true);
        }
        4

        Output:

        1

    Làm thế nào để bạn tạo một thư mục nếu không tồn tại trong PHP?

    Phương pháp 3: Sử dụng hàm mkdir (): mkdir () tạo một thư mục mới với tên đường dẫn được chỉ định ...
    Số đầu tiên luôn bằng không ..
    Số thứ hai chỉ định quyền cho chủ sở hữu ..
    Số thứ ba chỉ định quyền cho nhóm người dùng của chủ sở hữu ..
    Số thứ tư chỉ định quyền cho mọi người khác ..

    Làm cách nào để sao chép một tệp từ thư mục này sang thư mục khác trong PHP?

    Hàm Copy () trong PHP được sử dụng để sao chép một tệp từ nguồn sang mục đích hoặc thư mục đích.Nó tạo một bản sao của tệp nguồn vào tệp đích và nếu tệp đích đã tồn tại, nó sẽ bị ghi đè.Hàm COPY () trả về đúng khi thành công và sai khi thất bại.. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.

    PHP có thể tạo ra các thư mục không?

    Hàm mkdir () được sử dụng để tạo thư mục trong PHP.Nó là một chức năng sẵn có trong PHP.Hàm mkdir () tạo một thư mục mới với tên đường dẫn được chỉ định.Đường dẫn và chế độ được gửi dưới dạng tham số đến hàm mkdir () và nó trả về thành công hoặc sai khi thất bại.. It is an inbuilt function in PHP. The mkdir() function creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure.

    Làm thế nào tôi có thể tạo một thư mục trong một thư mục trong PHP?

    $ the_path = '/user/15';$ the_mode = '0700';mkdir ($ the_path, $ the_mode, true);Bạn có thể tạo đường dẫn và quyền bắt buộc cho thư mục mới, chuyển chúng đến hàm MKDIR cùng với việc đặt cờ 'đệ quy' thành true.Lưu câu trả lời này. you can generate the required path & permissions for the new directory, pass them to the mkdir function along with setting the 'recursive' flag to true. Save this answer.