-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_photo.php
48 lines (43 loc) · 1.38 KB
/
upload_photo.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
<?php
$files = array();
$keys = array_keys($_FILES);
for ($i = 0; $i < count($_FILES); $i++) {
$params = elabora_immagine($_FILES[$keys[$i]]);
array_push($files, $params);
}
echo json_encode($files);
function elabora_immagine($file) {
$name = $file["name"];
$tmp_name = $file["tmp_name"];
$path_info = pathinfo($name);
$estensione = $path_info["extension"];
$file_name = $path_info["filename"];
$new_name = generate_random_filename("files/", $estensione);
$new_path = "files/$new_name";
// Spostare il file temporaneo nella directory files
$moved = rename($tmp_name, $new_path);
chmod($new_path, 0644);
$params = array(
"file_path" => $new_path,
"file_name" => $file_name,
"tmp_name" => $tmp_name,
"moved" => $moved
);
return $params;
}
function get_filecount($directory) {
$filecount = 0;
$files = glob($directory . "*");
if ($files) {
$filecount = count($files);
}
return $filecount;
}
function generate_random_filename($directory, $extension) {
while (true) {
$filename = uniqid('file', true) . '.' . $extension;
if (!file_exists($directory . $filename))
return $filename;
}
}
?>