This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
105 lines (74 loc) · 1.92 KB
/
upload.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
print_r($_FILES);
$uploadOk = 1;
$error = 0;
$message = false;
/*
Errors :
0 : aucune erreur
1 : erreur extension
2 : erreur de lecture
3 : erreur de taille
4 : fichier déjà existant
5 : ok
6 : ok
7 : ok
8 : ok
9 : ok
*/
//Traitement des fichiers en un seul
print_r($_FILES);
//SI >1 fichiers
if (sizeof($_FILES['fileToUpload']['name']) > 0){
//Chemin du fichier de merge
//$filemerge = "product/exe/temp/" . uniqid('file_') . ".csv";
//chemins des fichiers importés
$files = [];
foreach ($_FILES["fileToUpload"]["error"] as $key => $error) {
//print_r($_FILES["fileToUpload"]);
if ($error == UPLOAD_ERR_OK) {
//Si fichier bon
$fileType = strtolower(pathinfo($_FILES["fileToUpload"]["name"][$key],PATHINFO_EXTENSION));
echo('File extension : ' . $fileType);
if($fileType == 'csv'){
//Deplacer le fichier en temporaire
$files[] = movetoTemp($_FILES["fileToUpload"]["tmp_name"][$key]);
} else {
$error = 1;
}
}
}
if($error == 0){
//Fichier de merge
$namemerge = uniqid('file_') . ".csv";
$filemerge = "product/exe/temp/" . $namemerge;
echo('Filemerge : ' . $filemerge . '<br>');
$merge = fopen($filemerge, "w");
foreach($files as $file){
$in = fopen($file, "r");
fgets($in);
while ($line = fgets($in)){
fwrite($merge, $line);
}
fclose($in);
//Suppression
unlink($file);
}
//Then clean up
fclose($merge);
//Redirection
header('Location: preview.php?file=' . $namemerge);
} else {
echo('<br>Stop all : ERROR ' . $error);
header('Location: index.php?e=' . $error);
}
}
function movetoTemp($file){
//Chemin temp
$tempmove = "product/exe/merge/";
$newid = uniqid('merge_');
$path = $tempmove . $newid . ".tmp";
move_uploaded_file($file, $path);
return($path);
}
?>