1. input file tag on event
$('#tmp_widget_file').on('change', prepareUpload);
function prepareUpload(event){files = event.target.files}
$('#twf').on('submit', uploadFiles);
2. uploadFiles function
function uploadFiles(event){
event.stopPropagation();
event.preventDefault();
var data = new FormData();
$.each(files, function(key, value){
data.append(key, value);
});
$.ajax({
url: './page/getWidgetInfo.php?files',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(data, textStatus, jqXHR){
if(typeof data.error === 'undefined'){
console.log(data);
}else{
console.log('ERRORS1: ' + data.error);
}
},
error: function(jqXHR, textStatus, errorThrown){
console.log('ERRORS2: ' + textStatus);
}
});
}
3. upload exe php
$data = array();
if(isset($_GET['files'])){
$error = false;
$filevar = "";
$error_msg = "There was an error uploading your files";
$uploaddir = '/home/namo/public_html/tmp/';
foreach($_FILES as $file){
if(move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))){
$filevar = $file['name'];
}else{
$error = true;
}
}
$data = ($error) ? array('error' => $error_msg) : array('files' => $filevar);
}
'웹프로그래밍 > PHP' 카테고리의 다른 글
정규식 정리 (0) | 2015.06.14 |
---|---|
[php] 파일 이동, 복사 함수 (0) | 2015.04.15 |
PHP XML CDATA Parsing (0) | 2015.04.10 |
PHP DOMDocument getting Attribute of Tag (0) | 2015.04.10 |
임시로 PHP오류 출력할 때 (0) | 2015.04.10 |