-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileUploadBackend.php
More file actions
38 lines (30 loc) · 938 Bytes
/
fileUploadBackend.php
File metadata and controls
38 lines (30 loc) · 938 Bytes
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
<?php
define("UPLOAD_DIR", "fileUploads/");
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>An error occurred.</p>";
exit;
}
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
header('Location: testFileUpload.php');
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
}
header('Location: testFileUpload.php');
?>