2. Understanding Files and Directories
In PHP, working with files and directories involves interacting
with the file system. You can create, open, read, write, rename,
and delete files or directories.
3. Opening and Closing a File
Opening a file: PHP provides the fopen() function to open a file. You can
open a file in various modes:
• 'r': Read-only, starts at the beginning of the file.
• 'w': Write-only, truncates the file to zero length or creates a new file if
it doesn't exist.
• 'a': Write-only, appends to the end of the file
4. Example:
Opening a file: Use the fopen() function
$file = fopen("example.txt", "r");
// Opens the file in read-only mode
Closing a file: Use the fclose() function to close an opened file.
fclose($file);
// Closes the file
5. Copying, Renaming, and Deleting a File
• Copying a file: You can use the copy() function to copy files.
copy("source.txt", "destination.txt");
• Renaming a file: The rename() function allows you to rename a file or
move it to a different directory.
rename("oldname.txt", "newname.txt");
• Deleting a file: To delete a file, use the unlink() function.
unlink("example.txt")
8. Explanation of the Script
• $dirPath = "uploads/";:
You are defining the path for the directory to be created, in this case,
uploads/.
• file_exists($dirPath):
This checks if the directory already exists at the specified path.
9. • mkdir($dirPath, 0777, true):
If the directory does not exist, this function creates it.
0777 specifies the permissions (read, write, and execute permissions
for everyone).
The third parameter true allows creating nested directories if needed.
11. <?php
$dirPath = “a/b/c”;
if ($handle = opendir($dirPath))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
echo "$entry<br>";
}
}
closedir($handle);
}
else
{
echo "Unable to open directory.";
}
?>
12. Explanation of the Script
• $dirPath = "uploads/";:
Specifies the path of the directory to be opened.
• opendir($dirPath):
Opens the directory for reading. The directory handle is stored in the
variable $handle.
• readdir($handle):
Reads each entry in the directory. It returns false when all the entries have
been read.
In the loop, it reads each file or folder one by one.
13. • if ($entry != "." && $entry != ".."):
Skips the special entries . and .. (representing the current and parent
directories).
• echo "$entry<br>";:
Outputs each file or folder name followed by an HTML line break
(<br>).
• closedir($handle):
Closes the directory handle once reading is complete.
14. File Uploading & Downloading
File Uploading: To allow file uploads, your HTML form should contain the
attribute enctype="multipart/form-data".
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
15. File Uploading
In upload.php, handle the uploaded file using PHP’s $_FILES
array.
if(isset($_FILES['file’]))
{
$filename = $_FILES['file']['name'];
$tempname = $_FILES['file']['tmp_name'];
move_uploaded_file($tempname, "uploads/" . $filename);
}
16. File Downloading
File Downloading: You can force a file download by sending
appropriate headers.
$file = "path/to/file.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;
filename="'.basename($file).'"');
readfile($file);
17. Generating Images with PHP
Basics of Computer Graphics in PHP: PHP can dynamically
generate images using the GD library, which is built-in in most
PHP installations. You can create images from scratch,
manipulate existing ones, and output them in various formats
like PNG, JPEG, and GIF.
18. Creating an Image
Step 1: Create a blank image using imagecreate() or
imagecreatetruecolor() for a higher-quality image.
$width = 200;
$height = 100;
$image = imagecreatetruecolor($width, $height);
Step 2: Allocate colors with imagecolorallocate().
$background = imagecolorallocate($image, 0, 0, 0); // Black background
$text_color = imagecolorallocate($image, 255, 255, 255); // White text
19. Step 3: Add shapes or text using functions like imageline(),
imagefilledrectangle(), or imagestring().
imagestring($image, 5, 10, 10, "Hello World!", $text_color); //
Adds text
Step 4: Output the image in a desired format (e.g., PNG).
header('Content-Type: image/png');
imagepng($image);
Step 5: Free up memory with imagedestroy().
imagedestroy($image);
20. <?php
$width = 200; // Create a blank image
$height = 100;
$image = imagecreatetruecolor($width, $height);
// Allocate colors
$background = imagecolorallocate($image, 0, 0, 255); // Blue background
$text_color = imagecolorallocate($image, 255, 255, 255); // White text
imagefill($image, 0, 0, $background); // Fill the background
imagestring($image, 5, 50, 40, "Hello, PHP!", $text_color); // Add text to the image
header('Content-Type: image/png'); // Set the header for PNG output
imagepng($image); // Output the image
imagedestroy($image); // Free memory
?>