プログラムは上から順番に実行されていくのでまずメソッド private function _validateUpload() {
}を定義して後の行で呼び出すの
PHP{
// error check
$this->_validateUpload();ではないでしょうか?今回のプログラムはぎゃくになっていないでしょうか? 理由がききたいです。宜しくお願い致します。
ImageUploader.php
php
1 2<?php 3 4namespace MyApp; 5 6class ImageUploader { 7 8 public function upload() { 9 try { 10 // error check 11 $this->_validateUpload(); 12 13 // type check 14 // save 15 // create thumbnail 16 } catch (\Exception $e) { 17 echo $e->getMessage(); 18 exit; 19 } 20 // redirect 21 header('Location: http://' . $_SERVER['HTTP_HOST']); 22 exit; 23 } 24 25 private function _validateUpload() { 26 27 } 28} 29
index.php
php
1?php 2 3ini_set('display_errors', 1); 4define('MAX_FILE_SIZE', 1 * 1024 * 1024); // 1MB 5define('THUMBNAIL_WIDTH', 400); 6define('IMAGES_DIR', __DIR__ . '/images'); 7define('THUMBNAIL_DIR', __DIR__ . '/thumbs'); 8 9if (!function_exists('imagecreatetruecolor')) { 10 echo 'GD not installed'; 11 exit; 12} 13 14function h($s) { 15 return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); 16} 17 18require 'ImageUploader.php'; 19 20$uploader = new \MyApp\ImageUploader(); 21 22if ($_SERVER['REQUEST_METHOD'] === 'POST') { 23 $uploader->upload(); 24} 25 26?> 27<!DOCTYPE html> 28<html lang="ja"> 29<head> 30 <meta charset="utf-8"> 31 <title>Image Uploader</title> 32 <style> 33 body { 34 text-align: center; 35 font-family: Arial, sans-serif; 36 } 37 </style> 38</head> 39<body> 40 41 <form action="" method="post" enctype="multipart/form-data"> 42 <input type="hidden" name="MAX_FILE_SIZE" value="<?php h(MAX_FILE_SIZE); ?>"> 43 <input type="file" name="image"> 44 <input type="submit" value="upload"> 45 </form> 46 47</body> 48</html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/14 15:16 編集
2017/05/14 15:26
2017/05/14 15:28 編集
2017/05/15 12:35