Operations on files and directories

To copy a file using a function called copy (). The first parameter is the source file and the second is the destination file or directory. This function returns true if the copy succeeded, or false otherwise. It is good to check whether the copying was successful.Mountains Photo


copy ($source, $destination) or die („Error”);


Moving and renaming
Function to move and rename the file suggests only the second function, however writes very well in both applications. The rename () takes two arguments: the name of the source file and the destination file name if the function is to change the name, the name of the directory where the file is to be moved, or the path with a new name if the file is to be transferred to the name change.


rename („a”, „b”); // Change the file name „a” to „b”
rename („b”, „test /”); // Move the file „b” to the „test”
rename („a”, „..”); // Move the file „a” to the parent directory
rename („a”, „test / b”); // Move the file „a” to the „test”
// The name change to „b”

Deleting files is the most difficult thing about making novice PHP developers. This is mainly because the function removes files called unlink ($filename). This feature may not work under Windows – the solution is to call the program del with the appropriate parameter.
To remove directories is a function rmdir ($name).


Creating directories
Creating directories is a function mkdir ($name, $mode). The second parameter specifies the mode to create a directory (access rights). The function returns true if the directory was created or false otherwise.

Access rights (only for UNIX)
Unix includes a system for controlling access to files by unauthorized persons. This system is based on the assignment of rights to individuals (and more specifically their accounts system) and the groups to which users belong.
Each file and directory in the system can be assigned to 3 sets of rights. Each of these sets are: the right to exercise (letter, x ‚or the number 1) – in the case of directories, this means the possibility of entry’ into him, right writing (letter, in ‚or number 2) and the right to read (the letter r’ or The number of 4).
The first set of these rights applies to the owner of the file, the second and the third group of users who do not own or do not belong to the group. The numbers assigned to specific laws are used to store octal. This provision is 3 digits, each of which corresponds to the complement of rights – the sum of numbers representing the law. For example, the number 7 is the complete ECL, read and write (1 + 2 + 4 = 7) and the number 5 is the right to read and execute (1 + 4 = 5). So to give the owner a set of rights, group reading and writing and the rest of the read-only, set the right „0764” (the number zero at the beginning is to inform PHP that the number is stored in octal).
To set access rights is a function chmod ($filename, $mode), where the second parameter is the access rights stored in octal. Other useful functions for working with the security system is Chown ($filename, $user), changing file ownership, and chgrp ($filename, $group), amending the group.
Setting appropriate access rights is a very important matter for the script writers something to files. Error „Permission denied” happens very often, and it can be solved just by setting the appropriate rights. Script to save something in the file, it is necessary to set write access to the file. PHP usually runs as the user nobody or http – you can check it out by calling the phpinfo ().


Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close