-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
8 changed files
with
254 additions
and
13 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"license": "MIT", | ||
|
||
"require": { | ||
"php": ">=8.0", | ||
"phpgt/http": "^v1.1" | ||
}, | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,107 @@ | ||
<?php | ||
/** | ||
* This example shows how to work with a multiple file upload. For this to work, | ||
* files must be uploaded through a form with enctype="multipart/formdata" using | ||
* a file input type. The file input must be named with square brackets, | ||
* indicating that multiple values can be present. | ||
* | ||
* The value of $_FILES in this script is hard-coded to what PHP will be | ||
* provided when the user enters three images into a form such as the one shown | ||
* below. | ||
* | ||
* Example form: | ||
* <!doctype html> | ||
* <form method="post" enctype="multipart/form-data"> | ||
* <label> | ||
* <span>Your name:</span> | ||
* <input name="name" /> | ||
* </label> <br /> | ||
* <label> | ||
* <input type="checkbox" name="colour[]" value="red" /> Red | ||
* </label><br /> | ||
* <label> | ||
* <input type="checkbox" name="colour[]" value="green" /> Green | ||
* </label><br /> | ||
* <label> | ||
* <input type="checkbox" name="colour[]" value="blue" /> Blue | ||
* </label><br /> | ||
* <input type="file" multiple name="upload[]" /> | ||
* <button name="do" value="upload">Upload!</button> | ||
* </form> | ||
*/ | ||
use Gt\Input\Input; | ||
use Gt\Input\InputData\Datum\FailedFileUpload; | ||
|
||
require(__DIR__ . "/../vendor/autoload.php"); | ||
|
||
$_GET = []; | ||
$_POST = [ | ||
"do" => "upload", | ||
"name" => "Greg", | ||
"colour" => [ | ||
"red", | ||
"blue", | ||
], | ||
]; | ||
$_FILES = [ | ||
"upload" => [ | ||
"name" => [ | ||
"front.jpg", | ||
"back.jpg", | ||
"description.txt", | ||
], | ||
"full_path" => [ | ||
"front.jpg", | ||
"back.jpg", | ||
"description.txt", | ||
], | ||
"type" => [ | ||
"image/jpeg", | ||
"image/jpeg", | ||
"text/plain", | ||
], | ||
"tmp_name" => [ | ||
"/tmp/phpkLgfwE", | ||
"/tmp/phpiZKQf6", | ||
"/tmp/php9UtO5A", | ||
], | ||
"error" => [ | ||
0, | ||
0, | ||
0, | ||
], | ||
"size" => [ | ||
123891, | ||
165103, | ||
915, | ||
], | ||
] | ||
]; | ||
|
||
$input = new Input($_GET, $_POST, $_FILES); | ||
|
||
echo "Your name: " . $input->getString("name"), PHP_EOL; | ||
|
||
if(!$input->contains("colour")) { | ||
echo "No colours chosen...", PHP_EOL; | ||
exit; | ||
} | ||
foreach($input->getMultipleString("colour") as $colour) { | ||
echo "Colour chosen: $colour", PHP_EOL; | ||
} | ||
|
||
if(!$input->contains("upload")) { | ||
echo "Nothing uploaded...", PHP_EOL; | ||
exit; | ||
} | ||
|
||
foreach($input->getMultipleFile("upload") as $fileName => $upload) { | ||
if($upload instanceof FailedFileUpload) { | ||
echo "Error uploading $fileName!", PHP_EOL; | ||
continue; | ||
} | ||
|
||
$newPath = "data/upload/$fileName"; | ||
$size = $upload->getSize(); | ||
echo "Uploaded to $newPath (size $size)", PHP_EOL; | ||
} |
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,47 @@ | ||
<?php | ||
/** | ||
* This example should be served and accessed within a web browser. | ||
* To serve using PHP, open up the example directory in a terminal and run: | ||
* php -S 0.0.0.0:8080 and then visit http://localhost:8080/02-multiple- | ||
*/ | ||
use Gt\Input\Input; | ||
|
||
require __DIR__ . "/../../vendor/autoload.php"; | ||
|
||
if(empty($_POST)) { | ||
goto website; | ||
} | ||
|
||
ini_set("display_errors", true); | ||
$input = new Input($_GET, $_POST, $_FILES); | ||
|
||
echo "<pre>"; | ||
echo "Your name is: ", $input->getString("name"), PHP_EOL; | ||
|
||
$colourString = implode(", ", $input->getMultipleString("colour")); | ||
echo "Colours chosen: ", $colourString, PHP_EOL; | ||
|
||
echo "Files uploaded: ", PHP_EOL; | ||
foreach($input->getMultipleFile("upload") as $fileName => $upload) { | ||
echo "$fileName is size: ", $upload->getSize(), PHP_EOL; | ||
} | ||
|
||
website:?> | ||
<!doctype html> | ||
<form method="post" enctype="multipart/form-data"> | ||
<label> | ||
<span>Your name:</span> | ||
<input name="name" /> | ||
</label><br> | ||
<label> | ||
<input type="checkbox" name="colour[]" value="red" /> Red | ||
</label><br> | ||
<label> | ||
<input type="checkbox" name="colour[]" value="green" /> Green | ||
</label><br> | ||
<label> | ||
<input type="checkbox" name="colour[]" value="blue" /> Blue | ||
</label><br> | ||
<input type="file" multiple name="upload[]" /> | ||
<button name="do" value="upload">Upload!</button> | ||
</form> |
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
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
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
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