-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_upload.php
More file actions
58 lines (58 loc) · 2.22 KB
/
file_upload.php
File metadata and controls
58 lines (58 loc) · 2.22 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
<?php
echo "File upload in PHP";
if(isset($_POST['submit'])) {
$permitted_extensions = ['png', 'jpg', 'jpeg', 'gif'];
$file_name = $_FILES['upload']['name'];
if(!empty($file_name)) {
//print_r($_FILES);
$file_size = $_FILES['upload']['size'];
$file_tmp_name = $_FILES['upload']['tmp_name'];
$generated_file_name = time().'-'.$file_name;
$destination_path = "uploads/${generated_file_name}";
$file_extension = explode('.', $file_name);
$file_extension = strtolower(end($file_extension));
//echo "$file_name, $file_size, $file_extension, $destination_path";
//validate file extension permitted
if(in_array($file_extension, $permitted_extensions)) {
if($file_size <= 1000000) {
//ok, move from temp folder to /uploads
//original file name and uploaded file name
//must be DIFFERENT !, why ?
move_uploaded_file($file_tmp_name, $destination_path);
$message = '<p style="color:green;">
File is uploaded</p>';
} else {
$message = '<p style="color:red;">
File is too big</p>';
}
} else {
$message = '<p style="color:red;">
Invalid file type</p>';
}
} else {
$message = '<p style="color:red;">
No file selected, please try again</p>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>File upload in PHP</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post"
enctype="multipart/form-data"
>
Choose your image to upload
<input type="file" name="upload">
<input type="submit" value="submit" name="submit">
</form>
<?php echo $message ?? '' ?>
</body>
</html>