Skip to content

Instantly share code, notes, and snippets.

@birowo
Last active November 14, 2016 01:50
Show Gist options
  • Save birowo/f1b45999dd996f2610f7d217ddbd9672 to your computer and use it in GitHub Desktop.
Save birowo/f1b45999dd996f2610f7d217ddbd9672 to your computer and use it in GitHub Desktop.
PHP routing . contoh penerapan untuk upload gambar. taruh file .htaccess & rute.php (& file lainnya di kasus ini) di document root (folder htdocs kalau di xampp) . akses browser : http://localhost
RewriteEngine On
RewriteRule ^.*\.php$ rute.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ rute.php [QSA,L]
<html>
<head>
<title></title>
</head>
<body>
<form action="/uploadimg" method="POST" enctype="multipart/form-data" target="_blank">
gambar: <input type="file" name="img" /><br>
slug: http://localhost/gambar//[image_id]/<input name="slug"><br>
<input type="submit" value="upload"/>
</form>
</body>
</html>
<?php
include 'uploadimage.php';
$rute = [
'notfound' => function(){
header('HTTP/1.1 404 Not Found');
echo 'tidak ditemukan';
},
'/' => function(){
readfile('root.html');
},
'/uploadimg' => uploadimage('upload'),
'/gambar' => uploadimage('tampil')
];
$url = explode('//', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
//$urlquery = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
$rute[array_key_exists($url[0], $rute)? $url[0]: 'notfound'](isset($url[1])? explode('/', $url[1]): []);
<?php
function upload($name, $validext, $maxsize, $folder_path, &$err){
if(isset($_FILES[$name])){
$file = $_FILES[$name];
//$file_type = $file['type'];
//$file_path = $folder_path.$file['name'];
$file_part = pathinfo($file['name']);
$file_ext = $file_part['extension'];
if(false === in_array($file_ext, $validext)){
$err[] = "ekstensi tidak diijinkan, silahkan pilih file: ".implode(' / ', $validext);
}
if($file['size'] > $maxsize){
$err[] = "ukuran file maks.: $maxsize";
}
if(true == empty($err)){
$file_id = uniqid('', true);
move_uploaded_file($file['tmp_name'], "$folder_path/$file_id.$file_ext");
return ['id'=>$file_id, 'name'=>$file_part['filename'], 'ext'=>$file_ext];
}
}
}
<?php
include 'upload.php';
function invalidurl($url){
return $url != preg_replace('/[^\w\/\.\-]+/', '', $url);
}
function uploadimage($aksi){
$mime = ['jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'png'=>'image/png'];
$dir = '.';
$img = [
'upload' => function ($param) use ($mime, $dir){
$err = [];
$img = upload('img', array_keys($mime), 1000000, $dir, $err);
$slug = empty($_POST['slug'])? $img['name']: $_POST['slug'];
$redir = "/gambar//{$img['ext']}/{$img['id']}/$slug";
if(invalidurl($redir)) $err[] = 'invalid url';
if(empty($err) && $img) return header('Location: '.$redir);
print_r($err);
},
'tampil' => function($param) use ($mime, $dir){
$file_path = 1 < sizeof($param)? "$dir/{$param[1]}.{$param[0]}": ' ';
if(invalidurl($file_path)) die('invalid url');
header("Content-Type: {$mime[$param[0]]}");
readfile($file_path);
}
];
return $img[$aksi];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment