... | ... | @@ -43,4 +43,112 @@ define a method to execute an sql query: something like ```public function execu |
|
|
|
|
|
write a script to instantiate your class. open a database connection and execute a query to check it's all working properly.
|
|
|
|
|
|
## 3. oo upload
|
|
|
|
|
|
we're going to create an object oriented script that uploads images and displays them as a gallery. this is a stand-alone script and not part of the recordstore.
|
|
|
|
|
|
3.1 using the same process as the recordstore index page, create a minimal index page that can load two views; gallery and upload. the gallery should be the default.
|
|
|
|
|
|
3.1.1 the page will have two links; you can include these as a navigation template or view. make the links of the form ```<a href='index.php?page=gallery'>Gallery</a>```
|
|
|
|
|
|
3.1.2 the index page will route to a view depending on which one was clicked.
|
|
|
you can use code like this:
|
|
|
```
|
|
|
$userClicked = isset($_GET['page']);
|
|
|
if ( $userClicked ) {
|
|
|
$fileToLoad = $_GET['page'];
|
|
|
} else {
|
|
|
$fileToLoad = "gallery";
|
|
|
}
|
|
|
include_once "views/$fileToLoad.php";
|
|
|
```
|
|
|
create placeholders for the gallery and upload views to check it works.
|
|
|
|
|
|
3.2 as in our previous lab, the script for the upload view will display a form and process the form if $_POST is set. for now, just create the form to upload an image.
|
|
|
|
|
|
3.3 now we'll create an Uploader class.
|
|
|
|
|
|
3.3.1 this will have the following private attributes
|
|
|
```
|
|
|
private $filename;
|
|
|
private $fileData;
|
|
|
private $destination;
|
|
|
```
|
|
|
|
|
|
3.3.1 an important bit of information is the name that the upload form assigns to the uploaded file.
|
|
|
if that form element was ```<input type='file' name='image-data' accept='image/jpeg'/>``` then 'image-data' is the key to the information stored in the $_POST array.
|
|
|
|
|
|
we'll use that in the __contstruct method to access the other information we need:
|
|
|
so the constructor will look like:
|
|
|
```
|
|
|
public function __construct( $key ) {
|
|
|
$this->filename = $_FILES[$key]['name'];
|
|
|
$this->fileData = $_FILES[$key]['tmp_name'];
|
|
|
}
|
|
|
```
|
|
|
|
|
|
3.3.2 as before, we'll need a folder to save the image in. you can create a new one (remember permissions) or use the same one that the recordstore is using (remember relative paths).
|
|
|
|
|
|
3.3.3 set the file destination in the Uploader class to be this directory: e.g. with a method like
|
|
|
```
|
|
|
public function saveIn( $folder ) {
|
|
|
$this->destination = $folder;
|
|
|
}
|
|
|
|
|
|
```
|
|
|
3.3.4 now we'll create a method to move the uploaded file, using ```move_uploaded_file```. we'll also make use of the handy ```is_writable``` function to check we got the directory permissions right.
|
|
|
here it is, wrapped up as a method:
|
|
|
```
|
|
|
public function save(){
|
|
|
$folderIsWritAble = is_writable( $this->destination );
|
|
|
if( $folderIsWritAble ){
|
|
|
$name = "$this->destination/$this->filename";
|
|
|
$success = move_uploaded_file( $this->fileData, $name );
|
|
|
} else {
|
|
|
trigger_error("cannot write to $this->destination");
|
|
|
$success = false;
|
|
|
}
|
|
|
return $success;
|
|
|
}
|
|
|
```
|
|
|
3.4 now you can go back to your upload view and write the code to process the form.
|
|
|
you'll need to
|
|
|
* instantiate a new uploader object using 'image-data'
|
|
|
* tell it where to save the image
|
|
|
* execute the save method
|
|
|
|
|
|
3.5 now we've uploaded some images, we need the gallery view to display them.
|
|
|
we can make use of one of php's built in classes, called DirectoryIterator, which you can read about here: http://php.net/manual/en/class.directoryiterator.php
|
|
|
|
|
|
here's a full version of the gallery view; experiment with it to make sure you understand how it works
|
|
|
```
|
|
|
return showImages();
|
|
|
|
|
|
function showImages(){
|
|
|
$out = "<h1>Image Gallery</h1>";
|
|
|
$out .= "<ul id='images'>";
|
|
|
$folder = "img";
|
|
|
$filesInFolder = new DirectoryIterator( $folder);
|
|
|
while ( $filesInFolder->valid() ) {
|
|
|
$file = $filesInFolder->current();
|
|
|
$filename = $file->getFilename();
|
|
|
$src = "$folder/$filename";
|
|
|
$fileInfo = new Finfo( FILEINFO_MIME_TYPE );
|
|
|
$mimeType = $fileInfo->file( $src );
|
|
|
|
|
|
if ( $mimeType === 'image/jpeg' ) {
|
|
|
$out .= "<li><img src='$src' /></li>";
|
|
|
}
|
|
|
$filesInFolder->next();
|
|
|
}
|
|
|
$out .= "</ul>";
|
|
|
echo $out;
|
|
|
|
|
|
}
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|