├── .gitignore ├── workshop ├── files │ ├── 2-open-read-file.txt │ ├── 1-introduction-file.txt │ └── 3-create-write-file.txt ├── 4.1-upload-script.php ├── 3-file-create-write.php ├── 2-file-open-read.php ├── 4-file-upload.php └── 1-introduction.php ├── assets ├── img │ ├── folder.gif │ ├── assembler.png │ ├── file-manager.gif │ └── assembler_icon.jfif ├── js │ └── cheatsheet.js └── css │ └── cheatsheet.css ├── README.md ├── package.json └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /workshop/files/2-open-read-file.txt: -------------------------------------------------------------------------------- 1 | This is the content of the "2-open-read-file.txt" file. -------------------------------------------------------------------------------- /workshop/files/1-introduction-file.txt: -------------------------------------------------------------------------------- 1 | This is the content of the "1-introduction-file.txt" file. -------------------------------------------------------------------------------- /assets/img/folder.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DogSoulDev/php-manage-files-workshop/HEAD/assets/img/folder.gif -------------------------------------------------------------------------------- /assets/img/assembler.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DogSoulDev/php-manage-files-workshop/HEAD/assets/img/assembler.png -------------------------------------------------------------------------------- /assets/img/file-manager.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DogSoulDev/php-manage-files-workshop/HEAD/assets/img/file-manager.gif -------------------------------------------------------------------------------- /workshop/files/3-create-write-file.txt: -------------------------------------------------------------------------------- 1 | This is the content of the "3-create-write-file.txt" file. 2 | New content in a new line. -------------------------------------------------------------------------------- /assets/img/assembler_icon.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DogSoulDev/php-manage-files-workshop/HEAD/assets/img/assembler_icon.jfif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | `php` `#backend` `#manage-files` `#master-in-software-engineering` 2 | 3 | # Assembler Institute: Manage files with PHP - Workshop 4 | 5 | In this workshop you will learn how to manage your backend files with PHP. 6 | 7 | ## Table of Contents 8 | 9 | - [Getting Started](#getting-started) 10 | 11 | ## Getting Started 12 | 13 | First of all, you will need to clone this repo: 14 | 15 | ```bash 16 | $ git clone https://github.com/assembler-institute/php-manage-files-workshop.git 17 | ``` 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "php-manage-files-workshop", 3 | "version": "1.0.0", 4 | "description": "`php` `#backend` `#manage-files` `#master-in-software-engineering`", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/assembler-school/php-manage-files-workshop.git" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/assembler-school/php-manage-files-workshop/issues" 18 | }, 19 | "homepage": "https://github.com/assembler-school/php-manage-files-workshop#readme", 20 | "dependencies": { 21 | "bootstrap": "^5.0.1" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /workshop/4.1-upload-script.php: -------------------------------------------------------------------------------- 1 | File is an image - " . $check["mime"] . ".
"; 11 | $uploadOk = 1; 12 | if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) { 13 | echo "El fichero es válido y se subió con éxito.
"; 14 | } else { 15 | echo "¡Posible ataque de subida de ficheros!
"; 16 | } 17 | } else { 18 | echo "File is not an image."; 19 | $uploadOk = 0; 20 | } 21 | } 22 | 23 | ?> 24 | -------------------------------------------------------------------------------- /assets/js/cheatsheet.js: -------------------------------------------------------------------------------- 1 | /* global bootstrap: false */ 2 | 3 | (function () { 4 | "use strict"; 5 | 6 | // Tooltip and popover demos 7 | document.querySelectorAll(".tooltip-demo").forEach(function (tooltip) { 8 | new bootstrap.Tooltip(tooltip, { 9 | selector: '[data-bs-toggle="tooltip"]', 10 | }); 11 | }); 12 | 13 | document 14 | .querySelectorAll('[data-bs-toggle="popover"]') 15 | .forEach(function (popover) { 16 | new bootstrap.Popover(popover); 17 | }); 18 | 19 | document.querySelectorAll(".toast").forEach(function (toastNode) { 20 | var toast = new bootstrap.Toast(toastNode, { 21 | autohide: false, 22 | }); 23 | 24 | toast.show(); 25 | }); 26 | 27 | // Disable empty links 28 | document.querySelectorAll('[href="#"]').forEach(function (link) { 29 | link.addEventListener("click", function (event) { 30 | event.preventDefault(); 31 | }); 32 | }); 33 | 34 | function setActiveItem() { 35 | var hash = window.location.hash; 36 | 37 | if (hash === "") { 38 | return; 39 | } 40 | 41 | var link = document.querySelector('.bd-aside a[href="' + hash + '"]'); 42 | var active = document.querySelector(".bd-aside .active"); 43 | var parent = link.parentNode.parentNode.previousElementSibling; 44 | 45 | link.classList.add("active"); 46 | 47 | if (parent.classList.contains("collapsed")) { 48 | parent.click(); 49 | } 50 | 51 | if (!active) { 52 | return; 53 | } 54 | 55 | var expanded = active.parentNode.parentNode.previousElementSibling; 56 | 57 | active.classList.remove("active"); 58 | 59 | if (expanded && parent !== expanded) { 60 | expanded.click(); 61 | } 62 | } 63 | 64 | setActiveItem(); 65 | window.addEventListener("hashchange", setActiveItem); 66 | })(); 67 | -------------------------------------------------------------------------------- /workshop/3-file-create-write.php: -------------------------------------------------------------------------------- 1 |4 | You also can create your own files and modify it, there is two main functions for this. 5 | Keep in mind that you must have permissions on you server side in order to create/modify files. 6 | Every time that you modify a file, you're replacing all the previous content of the file. 7 |
8 | 9 || Function | 13 |Description | 14 |Documentation | 15 |
|---|---|---|
| fopen() | 20 |This function is also used to create a file. If you specify a file that doesn't exist, it will create it, given that the file is opened for writing (w) or appending (a). | 21 |Link | 22 |
| fwrite() | 25 |This function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written. | 26 |Link | 27 |
Code:
32 | 33 |<?php
34 | try {
35 | $newFileName = "./workshop/files/3-create-write-file.txt";
36 | $fileContent = 'This is the content of the "3-create-write-file.txt" file.';
37 |
38 | // Now the file is created, but it's empty.
39 | $file = fopen($newFileName, "w");
40 |
41 | // Here we add the content to the file
42 | fwrite($file, $fileContent);
43 |
44 | // You can add new content to the file
45 | fwrite($file, "\nNew content in a new line.");
46 |
47 | $file = fopen($newFileName, "r");
48 |
49 | // Print the content
50 | $content = fread($file, filesize($newFileName));
51 | echo nl2br($content);
52 |
53 | // Close the file buffer
54 | fclose($file);
55 | } catch (Throwable $t) {
56 | echo $t->getMessage();
57 | }
58 |
?>
59 |
60 | Result:
61 | 62 |64 | 69 |
70 |4 | Like you can see before, the readFile() function it's very simple to use, but you can't customize how you want to retrieve 5 | the information from the file. 6 |
7 |8 | Now you will learn how to open, read, and close a file on the server in a manual way. 9 | There is tree main functions that you will have to use are: 10 |
11 || Function | 15 |Description | 16 |Documentation | 17 |
|---|---|---|
| fopen() | 22 |Opens a URL or a file, it requires two parameters, the file/url that you want to open and the way that you want to be opened. | 23 |Link | 24 |
| fread() | 27 |Once the file is opened, fread() function reads the file, you must provide two parameters, the path and the length of the file. | 28 |Link | 29 |
| fclose() | 32 |Finally, you should close the file in order to save server memory (not mandatory but recommended). | 33 |Link | 34 |
Code:
39 | 40 |<?php
41 | try {
42 | $fileName = "./workshop/files/example-file.txt";
43 |
44 | if (!file_exists($fileName)) {
45 | throw new Exception('File open failed');
46 | }
47 |
48 | // The function returns a pointer to the file if it is successful or zero if it is not. Files are opened for read or write operations.
49 | $file = fopen($fileName, "r");
50 |
51 | // Reads the file
52 | $content = fread($file, filesize($fileName));
53 |
54 | echo $content;
55 |
56 | // Close the file buffer
57 | fclose($file);
58 | } catch (Throwable $t) {
59 | echo $t->getMessage();
60 | }
61 |
?>
62 |
63 | Result:
64 | 65 |67 | 72 |
73 |4 | You also can create your own files and modify it, there is two main functions for this. 5 | Keep in mind that you must have permissions on you server side in order to create/modify files. 6 | Every time that you modify a file, you're replacing all the previous content of the file. 7 |
8 | 9 || Function | 13 |Description | 14 |Documentation | 15 |
|---|---|---|
| fopen() | 20 |This function is also used to create a file. If you specify a file that doesn't exist, it will create it, given that the file is opened for writing (w) or appending (a). | 21 |Link | 22 |
| fwrite() | 25 |This function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written. | 26 |Link | 27 |
Code:
32 | 33 |<?php
34 | $target_dir = "./files/";
35 | $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
36 | $uploadOk = 1;
37 | $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
38 | // Check if image file is a actual image or fake image
39 | if (isset($_POST["submit"])) {
40 | $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
41 | if ($check !== false) {
42 | echo "<p>File is an image - " . $check["mime"] . ".</p>";
43 | $uploadOk = 1;
44 | if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) {
45 | echo "<p>El fichero es válido y se subió con éxito.</p>";
46 | } else {
47 | echo "<p>¡Posible ataque de subida de ficheros!</p>";
48 | }
49 | } else {
50 | echo "File is not an image.";
51 | $uploadOk = 0;
52 | }
53 | }
54 |
?>
55 |
56 | Result:
57 | 58 |When you are working in the server environment you are able to manipulate the information and files of the backend part. 13 | In PHP you have built-in methods that allows you to read, modify, create and delete files and directories
14 |You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, 17 | filling a hard-drive with garbage data, and deleting the content of a file by accident.
18 |Some of the most used cloud tools to manage files are Google Drive, Dropbox, OneDrive, ... This tools 31 | must manipulate files in the backend part in order to simulate an environment similar to the one you have on your computer.
32 |
33 | In PHP there is a simple function to read a file and print their content. The readfile() function reads a file and writes it to the output buffer.
45 |In this repository you have a file called "example-file.txt" that contains some plain text.
46 |Code:
47 | 48 |<?php
php echo readfile("./workshop/files/example-file.txt");
?>
49 |
50 | Result:
51 |53 | 56 |
57 |