-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_image.php
More file actions
92 lines (73 loc) · 2.46 KB
/
upload_image.php
File metadata and controls
92 lines (73 loc) · 2.46 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
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
<?php
include 'init.php';
if (!logged_in()){
header('Location: index.php');
exit ();
}
include 'template/header.php';
if(isset ($_FILES['image'], $_POST['album_id'])){
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.', $image_name)));
// end function takes end part of the file name
$album_id = $_POST['album_id'];
$errors = array();
if (empty ($image_name) || empty ($album_id)){
$errors[] = 'Something is missing';
} else {
// in array checks extension against allowed extension array, $allowed_ext
if (in_array($image_ext, $allowed_ext) === FALSE){
$errors[] = 'File type not allowed';
}
// 2097152 bytes = 2mb
if ($image_size > 2097152){
$errors[] = 'Maximum file size is 2mb';
}
// check album belongs to this user
if(album_check($album_id) === FALSE){
$errors[] = 'Couldn\'t upload to that album';
}
}
if (!empty ($errors)){
foreach ($errors as $error){
echo $error, '<br />';
}
} else {
upload_image($image_temp, $image_ext, $album_id);
header('Location: view_album.php?album_id='.$album_id);
exit ();
}
}
$user_id = $_SESSION['user_id'];
$albums = get_albums($user_id);
if(empty ($albums)){
echo '<p>You don\'t have any albums. <a href="create_album.php">Create an album</a></p>';
} else {
?>
<section id="main_section">
<article class="transparent">
<h2>Upload image</h2><br/>
<form id="image_upload" action="" method="post" enctype="multipart/form-data">
<p>Choose a file: <input type="file" name="image"/></p>
<p>
Choose an album: <br />
<select name="album_id">
<?php
foreach ($albums as $album){
echo '<option value="', $album['id'],'">',$album['name'], '</option>';
}
?>
</select>
</p>
<p><input type="submit" value="Upload"/></p>
<!--<p><a id="add_more" href="#"/>Add more... </a> -->
</form>
</article>
</section>
<?php include 'widgets/photo_options.php' ?>
<?php
}
include 'template/footer.php';
?>