forked from bradtraversy/php-crash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a92be3
commit 71b52b4
Showing
11 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,7 @@ | |
|
||
// Encode to JSON | ||
var_dump(json_encode($people)); | ||
|
||
// Decode from JSON | ||
$jsonobj = '{"first_name":"Brad","last_name": "Traversy","email":"[email protected]"}'; | ||
var_dump(json_decode($jsonobj)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* ---------- File Handling --------- */ | ||
|
||
/* | ||
File handling is the ability to read and write files on the server. | ||
PHP has built in functions for reading and writing files. | ||
*/ | ||
|
||
$file = 'extras/users.txt'; | ||
|
||
// if(file_exists($file)) { | ||
// // Returns the content and number of bytes read from the file on success, or FALSE on failure. | ||
// echo readfile('extras/users.txt'); | ||
// } | ||
|
||
// File Open, Read, Write, Close | ||
if (file_exists($file)) { | ||
// fopen() gives you more control over the file. | ||
// Modes: r, w, a, x, r+, w+, a+, x+ See below for details | ||
$handle = fopen($file, 'r'); | ||
// fread() reads the file and returns the content as a string on success, or FALSE on failure. | ||
$contents = fread($handle, filesize($file)); | ||
// fclose() closes the file resource on success, or FALSE on failure. | ||
fclose($handle); | ||
echo $contents; | ||
} else { | ||
// Create the file | ||
$handle = fopen($file, 'w'); | ||
// PHP_EOL is a constant that represents the end of line character. | ||
$contents = 'Brad' . PHP_EOL . 'Sara' . PHP_EOL . 'Mike' . PHP_EOL . 'John'; | ||
// fwrite() writes the contents to the file and returns the number of bytes written on success, or FALSE on failure. | ||
fwrite($handle, $contents); | ||
fclose($handle); | ||
} | ||
|
||
/* | ||
r - Open a file for read only. File pointer starts at the beginning of the file | ||
w - Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file | ||
a - Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist | ||
x - Creates a new file for write only. Returns FALSE and an error if file already exists | ||
r+ - Open a file for read/write. File pointer starts at the beginning of the file | ||
w+ - Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file | ||
a+ - Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist | ||
x+ - Creates a new file for read/write. Returns FALSE and an error if file already exists | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/* ----------- File upload ---------- */ | ||
|
||
|
||
$allowed_ext = array('png', 'jpg', 'jpeg', 'gif'); | ||
|
||
if(isset($_POST['submit'])) { | ||
// Check if file was uploaded | ||
if(!empty($_FILES['upload']['name'])) { | ||
$file_name = $_FILES['upload']['name']; | ||
$file_size = $_FILES['upload']['size']; | ||
$file_tmp = $_FILES['upload']['tmp_name']; | ||
$target_dir = "uploads/${file_name}"; | ||
// Get file extension | ||
$file_ext = explode('.', $file_name); | ||
$file_ext = strtolower(end($file_ext)); | ||
// echo $file_ext; | ||
|
||
// Validate file type/extension | ||
if(in_array($file_ext, $allowed_ext)) { | ||
// Validate file size | ||
if($file_size <= 1000000) { // 1000000 bytes = 1MB | ||
// Upload file | ||
move_uploaded_file($file_tmp, $target_dir); | ||
|
||
// Success message | ||
echo '<p style="color: green;">File uploaded!</p>'; | ||
} else { | ||
echo '<p style="color: red;">File too large!</p>'; | ||
} | ||
} else { | ||
$message = '<p style="color: red;">Invalid file type!</p>'; | ||
} | ||
} else { | ||
$message = '<p style="color: red;">Please choose a file</p>'; | ||
} | ||
} | ||
?> | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>File Upload</title> | ||
</head> | ||
<body> | ||
<?php echo $message ?? null; ?> | ||
<form action="<?php echo htmlspecialchars( | ||
$_SERVER['PHP_SELF'] | ||
); ?>" method="post" enctype="multipart/form-data"> | ||
Select image to upload: | ||
<input type="file" name="upload"> | ||
<input type="submit" value="Submit" name="submit"> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* ----------- Exceptions ----------- */ | ||
|
||
/* | ||
PHP has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch or finally block. | ||
*/ | ||
|
||
function inverse($x) { | ||
if (!$x) { | ||
throw new Exception('Division by zero.'); | ||
} | ||
return 1/$x; | ||
} | ||
|
||
// echo inverse(0); // Causes an exception to be thrown and stops script execution | ||
|
||
// Handles the exception | ||
// try { | ||
// echo inverse(5) . ' '; | ||
// echo inverse(0) . ' '"; | ||
// } catch (Exception $e) { | ||
// echo 'Caught exception: ', $e->getMessage(), ' '; | ||
// } | ||
|
||
// Finally block is executed regardless of whether an exception is thrown or not | ||
|
||
try { | ||
echo inverse(5) . ' '; | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), ' '; | ||
} finally { | ||
echo 'First finally '; | ||
} | ||
|
||
try { | ||
echo inverse(0) . ' '; | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), ' '; | ||
} finally { | ||
echo "Second finally "; | ||
} | ||
|
||
|
||
echo 'Hello World'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/* --- Object Oriented Programming -- */ | ||
|
||
/* | ||
From PHP5 onwards you can write PHP in either a procedural or object oriented way. OOP consists of classes that can hold "properties" and "methods". Objects can be created from classes. | ||
*/ | ||
|
||
class User { | ||
// Properties are just variables that belong to a class. | ||
// Access Modifiers: public, private, protected | ||
// public - can be accessed from anywhere | ||
// private - can only be accessed from inside the class | ||
// protected - can only be accessed from inside the class and by inheriting classes | ||
private $name; | ||
public $email; | ||
public $password; | ||
|
||
// The constructor is called whenever an object is created from the class. | ||
// We pass in properties to the constructor from the outside. | ||
public function __construct($name, $email, $password) { | ||
// We assign the properties passed in from the outside to the properties we created inside the class. | ||
$this->name = $name; | ||
$this->email = $email; | ||
$this->password = $password; | ||
} | ||
|
||
// Methods are functions that belong to a class. | ||
// function setName() { | ||
// $this->name = $name; | ||
// } | ||
|
||
function getName() { | ||
return $this->name; | ||
} | ||
|
||
function login() { | ||
return "User $this->name is logged in."; | ||
} | ||
|
||
// Destructor is called when an object is destroyed or the end of the script. | ||
function __destruct() { | ||
echo "The user name is {$this->name}."; | ||
} | ||
} | ||
|
||
// Instantiate a new user | ||
$user1 = new User('Brad', '[email protected]', '123456'); | ||
echo $user1->getName(); | ||
echo $user1->login(); | ||
|
||
// Add a value to a property | ||
// $user1->name = 'Brad'; | ||
|
||
var_dump($user1); | ||
// echo $user1->name; | ||
|
||
/* ----------- Inheritence ---------- */ | ||
|
||
/* | ||
Inheritence is the ability to create a new class from an existing class. | ||
It is achieved by creating a new class that extends an existing class. | ||
*/ | ||
|
||
class employee extends User { | ||
public function __construct($name, $email, $password, $title) { | ||
parent::__construct($name, $email, $password); | ||
$this->title = $title; | ||
} | ||
|
||
public function getTitle() { | ||
return $this->title; | ||
} | ||
} | ||
|
||
$employee1 = new employee('John','[email protected]','123456','Manager'); | ||
echo $employee1->getTitle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
/* ---------- File Handling --------- */ | ||
|
||
/* | ||
File handling is the ability to read and write files on the server. | ||
PHP has built in functions for reading and writing files. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
/* ----------- File upload ---------- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
/* ----------- Exceptions ----------- */ | ||
|
||
/* | ||
PHP has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch or finally block. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
/* --- Object Oriented Programming -- */ | ||
|
||
/* | ||
From PHP5 onwards you can write PHP in either a procedural or object oriented way. OOP consists of classes that can hold "properties" and "methods". Objects can be created from classes. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Brad | ||
Sara | ||
Mike | ||
John |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.