-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
executable file
·66 lines (56 loc) · 1.47 KB
/
index.php
File metadata and controls
executable file
·66 lines (56 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Created by: Denis Zajchenko (denis.zaichenko@gmail.com)
* Date: 07.10.13 20:56
*/
require 'vendor/autoload.php';
require 'helpers/Converter.php';
$app = new \Slim\Slim(array(
'templates.path' => 'templates',
));
/**
* index page
*/
$app->get('/', function() use ($app) {
$app->render('index.php');
});
/**
* upload file action
*/
$app->post('/', function() use ($app) {
$type = $app->request()->post('type');
$file = $_FILES['file'];
$extensions = array('json', 'xml', 'csv');
// check is file uploaded
if($file['error'] !== 0){
$error = array(
'file' => 'File is empty. Please choose a file',
);
$app->render('index.php', array('error' => $error));
$app->stop();
}
// check file mime type
$ext = end(explode(".", $file['name']));
if(!in_array($ext, $extensions)){
$error = array(
'file' => 'Wrong file type. Please choose json, xml or csv file type',
);
$app->render('index.php', array('error' => $error));
$app->stop();
}
$filename = '';
$converter = new Converter($file);
switch($type) {
case 'xml':
$filename = $converter->toXml();
break;
case 'csv':
$filename = $converter->toCsv();
break;
case 'json':
$filename = $converter->toJson();
break;
}
$app->render('index.php', array('filename' => $filename));
});
$app->run();