Skip to content

Instantly share code, notes, and snippets.

@birowo
Last active November 24, 2016 06:17
Show Gist options
  • Save birowo/ff9033697a326c48177d07393ad92270 to your computer and use it in GitHub Desktop.
Save birowo/ff9033697a326c48177d07393ad92270 to your computer and use it in GitHub Desktop.
upload file secara binary ajax dengan fallback secara multipart form . contoh untuk upload gambar. akses http://localhost/binaryUpload.html
<?php
$mime = ['jpg'=>'image/jpeg; charset=binary', 'png'=>'image/png; charset=binary'];
$buffer = file_get_contents('php://input');
$validExt = array_keys($mime);
$fileExt = array_search(finfo_buffer(finfo_open(FILEINFO_MIME), $buffer, FILEINFO_MIME), $mime);
if(false === in_array($fileExt, $validExt)){
die("INVALID TYPE, type file yang valid: ".implode(' / ', $validExt));
}
$uploadFileID = uniqid('binaryAjax', true).'.'.$fileExt;
file_put_contents($uploadFileID, $buffer);
echo $uploadFileID;
<html>
<head>
<title></title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="multipartForm.php" onsubmit="upload(this);return false" target="_blank">
gambar: <input name="gambar" type="file"><br>
<input type="submit" value="upload">
</form>
<script>
function fileUint8Array(callback){
var salah = {}
if(window.FileReader){
var fr = new FileReader();
fr.onload = function(evt){
if(window.Uint8Array) return callback(null, new Uint8Array(evt.target.result));
salah.pesan = 'browser tidak support Uint8Array !!!';
return callback(salah);
}
return fr.readAsArrayBuffer(this.files[0]);
}
salah.pesan = 'browser tidak support FileReader !!!';
return callback(salah);
}
function ajax(method, url, kueri, callback){
var salah = {}
if(-1 != 'HEAD,GET,DELETE'.indexOf(method)){
if(kueri) url += "?"+kueri;
kueri = null;
}else if(-1 != 'POST,PUT'.indexOf(method)){
salah.pesan = 'method salah';
return salah;
}
try{ var xhr=new XMLHttpRequest();}catch(err){
salah.pesan = 'tidak support ajax';
return salah;
}
xhr.onreadystatechange = function(){
if(4 == xhr.readyState && 200 == xhr.status){
callback&&callback(xhr.responseText);
}
}
xhr.open(method, url);
!(kueri instanceof Uint8Array)&&kueri&&xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(kueri);
}
function upload(_form){
//return _form.submit();//test multipartForm
var gmbr = _form.gambar;
if(gmbr.files && gmbr.files[0]){
return fileUint8Array.call(gmbr, function(error, result){
if(error) return _form.submit();
error = ajax(_form.method, 'binaryAjax.php', result, function(urlImage){
if(-1 != urlImage.indexOf('INVALID TYPE')) return alert(urlImage);
open(urlImage, '_blank');
});
if(error){
if('method salah' == error.pesan) return alert(error.pesan);
_form.submit();
}
});
}
alert('inputan gambar belum diisi')
}
</script>
</body>
</html>
<?php
include 'upload.php';
$err = [];
$uploadId = uniqid('multipartForm', true);
$mime = ['jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'png'=>'image/png'];
$maxSize = 1000000;
$dir = '.';
$img = upload('gambar', $uploadId, array_keys($mime), $maxSize, $dir, $err);
if(empty($err)){
header("Content-Type: {$mime[$img['ext']]}");
readfile("$dir/$uploadId.{$img['ext']}");
die;
}
print_r($err);
<?php
function upload($name, $fileId, $validExt, $maxSize, $folderPath, &$err){
if(empty($_FILES[$name])){
$err[] = "tidak ada upload file";
return 0;
}
$file = $_FILES[$name];
//$file_type = $file['type'];
//$file_path = $folder_path.$file['name'];
$file_part = pathinfo($file['name']);
$fileExt = $file_part['extension'];
if(false === in_array($fileExt, $validExt)){
$err[] = "ekstensi tidak diijinkan, silahkan pilih file: ".implode(' / ', $validExt);
return 0;
}
if($file['size'] > $maxSize){
$err[] = "ukuran file maks.: $maxSize";
return 0;
}
move_uploaded_file($file['tmp_name'], "$folderPath/$fileId.$fileExt");
return ['name'=>$file_part['filename'], 'ext'=>$fileExt];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment