replace.php:34 PHP's Filesystem provides a set of functions to work with files and directories. This is a broad topic, so let's break it down by aspect and provide examples for each:
$content = file_get_contents('myfile.txt');
echo $content;
$lines = file('myfile.txt');
foreach ($lines as $line) {
echo $line . "<br>";
}
2. Writing to Files:
file_put_contents('myfile.txt', 'Hello, world!');
$file = fopen('myfile.txt', 'w');
fwrite($file, 'Hello, world!');
fclose($file);
3. File Information:
echo filesize('myfile.txt');
echo filetype('myfile.txt'); // e.g. "file"
if (file_exists('myfile.txt')) {
echo 'File exists!';
}
4. Directories:
mkdir('new_directory');
rmdir('empty_directory');
$items = scandir('.');
foreach ($items as $item) {
echo $item . "<br>";
}
5. File Manipulation:
rename('oldfile.txt', 'newfile.txt');
copy('source.txt', 'destination.txt');
unlink('delete_me.txt');
6. Working with Paths:
echo dirname('/path/to/myfile.txt'); // outputs "/path/to"
echo basename('/path/to/myfile.txt'); // outputs "myfile.txt"
$info = pathinfo('/path/to/myfile.txt');
echo $info['dirname']; // outputs "/path/to"
echo $info['basename']; // outputs "myfile.txt"
echo $info['extension']; // outputs "txt"
echo $info['filename']; // outputs "myfile"
7. File Permissions:
chmod('myfile.txt', 0755);
Remember, when working with the filesystem, always be cautious. Make sure to validate inputs, especially if they come from untrusted sources, to avoid vulnerabilities like path traversal. Also, always backup important data before performing operations that could potentially result in data loss.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here