Commit 8affd160 authored by danmcquillan's avatar danmcquillan
Browse files

add image upload example

parent c9f8d284
<?php
class Uploader {
private $filename;
private $fileData;
private $destination;
public function __construct( $key ) {
$this->filename = $_FILES[$key]['name'];
$this->fileData = $_FILES[$key]['tmp_name'];
}
public function saveIn( $folder ) {
$this->destination = $folder;
}
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;
}
}
/* code listing for css/layout.css */
h1{color:red;}
/* code listing for css/navigation.css*/
nav {
background-color: #CCCCDE;
padding-top: 10px;
}
nav a{
display:inline-block;
text-decoration:none;
color: #000;
margin-left: 10px;
}
nav a:hover{text-decoration: underline;}
<?
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$title = "oo image upload";
include 'templates/header.php';
include 'views/navigation.php';
$userClicked = isset($_GET['page']);
if ( $userClicked ) {
$fileToLoad = $_GET['page'];
} else {
$fileToLoad = "gallery";
}
include_once "views/$fileToLoad.php";
include "templates/footer.php";
?>
<?
$out = "
</body>
</html>";
echo $out;
?>
<?
$out = "<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<link href='css/layout.css' rel='stylesheet' />
<link href='css/navigation.css' rel='stylesheet' />
</head>
<body>";
echo $out;
?>
<?php
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;
}
?>
<nav>
<a href='index.php?page=gallery'>Gallery</a>
<a href='index.php?page=upload'>Upload new image</a>
</nav>
<?php
include_once "classes/Uploader.class.php";
$content = "";
//$newImageSubmitted is TRUE if form was submitted, otherwise FALSE
$newImageSubmitted = isset( $_POST['new-image'] );
if ( $newImageSubmitted ) {
$uploader = new Uploader( "image-data" );
$uploader->saveIn("img");
$fileUploaded = $uploader->save();
if ( $fileUploaded ) {
$content .= "new file uploaded";
} else {
$content .= "something went wrong";
}
}
$content .= "<h1>Upload new jpg images</h1>
<form method='post' action='index.php?page=upload' enctype='multipart/form-data' >
<label>Find a jpg image to upload</label>
<input type='file' name='image-data' accept='image/jpeg'/>
<input type='submit' value='upload' name='new-image' />
</form>";
echo $content;
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment