-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
59 lines (50 loc) · 1.64 KB
/
index.php
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
<?php
/**
* Created by PhpStorm.
* User: dmitrij.rebrov
* Date: 07/03/2018
* Time: 16:28
*/
// Remove not actual CSV file
unlink('expenses_summary.csv');
$target_dir = "/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
}
// Allow CSV file formats
else if(!empty($FileType) && $FileType != "csv") {
echo "Sorry, only CSV files are allowed.";
}
// Last check and build the table
else if (isset ($_FILES['fileToUpload'])) {
$uploaded_csv = fopen($_FILES["fileToUpload"]["tmp_name"], 'r');
$saved_csv_with_summary = fopen('expenses_summary.csv', 'w');
print "<table style='border-collapse: collapse;'>";
while (($line = fgetcsv($uploaded_csv)) !== FALSE) {
$summary = $line[1] * $line[2];
print "<tr style='width: 100px'>";
print "<td style='border: 1px solid black; width: 100px;'>" .
$line[0] .
"</td>" .
"<td style='border: 1px solid black; width: 100px;'>" .
$summary .
"</td>";
print "<tr>";
fputcsv($saved_csv_with_summary, [$line[0], $summary]);
}
fclose($saved_csv_with_summary);
fclose($uploaded_csv);
print "</table>";
print "Download this report as CSV -
<a href=\"/expenses_summary.csv\" target=\"_blank\">click here</a>";
}
?>
<div><br /><br /><br /></div>
<form action="index.php" method="post" enctype="multipart/form-data">
Upload a new CSV file<br />
<input type="file" name="fileToUpload" id="csvToUpload"><br />
<input type="submit" value="Upload" name="Upload">
</form>