$_FILES["imgs"] meerdere bestanden
Ik heb een pagina waar een berichtje plus een of meerdere foto's geupload kunnen worden.
Nu zit ik echter met het probleem dat ik bij selecteren van 2 foto's, de eerste 5x doorlopen wordt (bij de 1e keer succesvol geupload wordt en bij de 4x erna vastloopt omdat het TMP bestand er neit meer is (duh)) en de tweede niet.
Hieronder de code, ik denk dat de fout 'm zit in regel 21.
Code (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
106
107
108
109
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
106
107
108
109
<?php
include_once("errors.php"); echo "BOE";
session_start();
if($_SESSION['username'] != "HankyPanky" ) { //test setup, als gebruiker niet is ingelogd moet 'ie weg
header("Location: index.php");
}
$page="Submit";
if(isset($_POST['check']) && $_POST['check'] == "TRUE") {
include_once("database.php"); //configs for querying to database
$maxID = "SELECT MAX(messageID) FROM Messages";
$num = mysqli_fetch_assoc(query($maxID));
$messageID = $num["MAX(messageID)"] + 1;
$messageQuery = ("INSERT INTO Messages (messageID, uploaded, message)
VALUES (" . $messageID . ", '". date("Y-m-d H:i:s") ."', '" . $_POST['message'] . "')");
query($messageQuery);
for($i=0; $i<count($_FILES["imgs"]); $i++) {
$target_file = date("Y-m-d_H:i:s") ."_". basename($_FILES["imgs"]["name"]);
echo "<p>".$i.": File ".$target_file."</p>";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["imgs"]["tmp_name"]);
if($check !== FALSE) {
echo "<p>".$i.": File is an image - " . $check["mime"] . ".</p>";
$uploadOk = 1;
} else {
echo "<p>".$i.": File is not an image.</p>";
$uploadOk = 0;
}
}
// Check file size
if ($_FILES["imgs"]["size"] > 50000000) { //50000000 = 50.000.000 B = 50.000 KB = 50 MB
echo "<p>".$i.": Sorry, your file is too large.</p>";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists("images/".$target_file)) {
echo "<p>".$i.": Sorry, file already exists.</p>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<p>".$i.": Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<p>".$i.": Sorry, your file was not uploaded.</p>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["imgs"]["tmp_name"], "images/".$target_file)) {
echo "<p>".$i.": The file ". basename( $_FILES["imgs"]["name"]). " has been uploaded.</p>";
$maxID = "SELECT MAX(id) FROM Photos";
$num = mysqli_fetch_assoc(query($maxID));
$photoID = $num["MAX(id)"] + 1;
$imageQuery = "INSERT INTO Photos (id, name, messageID) VALUES (".$photoID.", '".$target_file."', ".$messageID.")";
query($imageQuery);
} else {
echo "<p>".$i.": Sorry, there was an error uploading your file ".basename($_FILES["imgs"]["name"]).".</p>";
}
}
}
//header("Location: index.php"); //after submitting a story, go to the front page to check it out.
}
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="menubar.css">
<link rel="shortcut icon" type="image/png" href="images/favico.png" />
<title>BLACK WIDOW | IDP 8</title>
</head>
<body>
<?php include_once("menu.php"); ?>
<center>
<h3>Submit a new story:</h3>
<form id="newMsg" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="check" value="TRUE">
<p><textarea name="message" form="newMsg" rows=7 cols=50>News message...</textarea></p>
<p>Photo's: <input type="file" name="imgs" id="imgs" accept="image/*" multiple></p>
<p><input type="submit" name="submit" value="Send!"></p>
</form>
</center>
<?php include_once("foot.php"); ?>
</body>
</html>
include_once("errors.php"); echo "BOE";
session_start();
if($_SESSION['username'] != "HankyPanky" ) { //test setup, als gebruiker niet is ingelogd moet 'ie weg
header("Location: index.php");
}
$page="Submit";
if(isset($_POST['check']) && $_POST['check'] == "TRUE") {
include_once("database.php"); //configs for querying to database
$maxID = "SELECT MAX(messageID) FROM Messages";
$num = mysqli_fetch_assoc(query($maxID));
$messageID = $num["MAX(messageID)"] + 1;
$messageQuery = ("INSERT INTO Messages (messageID, uploaded, message)
VALUES (" . $messageID . ", '". date("Y-m-d H:i:s") ."', '" . $_POST['message'] . "')");
query($messageQuery);
for($i=0; $i<count($_FILES["imgs"]); $i++) {
$target_file = date("Y-m-d_H:i:s") ."_". basename($_FILES["imgs"]["name"]);
echo "<p>".$i.": File ".$target_file."</p>";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["imgs"]["tmp_name"]);
if($check !== FALSE) {
echo "<p>".$i.": File is an image - " . $check["mime"] . ".</p>";
$uploadOk = 1;
} else {
echo "<p>".$i.": File is not an image.</p>";
$uploadOk = 0;
}
}
// Check file size
if ($_FILES["imgs"]["size"] > 50000000) { //50000000 = 50.000.000 B = 50.000 KB = 50 MB
echo "<p>".$i.": Sorry, your file is too large.</p>";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists("images/".$target_file)) {
echo "<p>".$i.": Sorry, file already exists.</p>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<p>".$i.": Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<p>".$i.": Sorry, your file was not uploaded.</p>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["imgs"]["tmp_name"], "images/".$target_file)) {
echo "<p>".$i.": The file ". basename( $_FILES["imgs"]["name"]). " has been uploaded.</p>";
$maxID = "SELECT MAX(id) FROM Photos";
$num = mysqli_fetch_assoc(query($maxID));
$photoID = $num["MAX(id)"] + 1;
$imageQuery = "INSERT INTO Photos (id, name, messageID) VALUES (".$photoID.", '".$target_file."', ".$messageID.")";
query($imageQuery);
} else {
echo "<p>".$i.": Sorry, there was an error uploading your file ".basename($_FILES["imgs"]["name"]).".</p>";
}
}
}
//header("Location: index.php"); //after submitting a story, go to the front page to check it out.
}
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="menubar.css">
<link rel="shortcut icon" type="image/png" href="images/favico.png" />
<title>BLACK WIDOW | IDP 8</title>
</head>
<body>
<?php include_once("menu.php"); ?>
<center>
<h3>Submit a new story:</h3>
<form id="newMsg" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="check" value="TRUE">
<p><textarea name="message" form="newMsg" rows=7 cols=50>News message...</textarea></p>
<p>Photo's: <input type="file" name="imgs" id="imgs" accept="image/*" multiple></p>
<p><input type="submit" name="submit" value="Send!"></p>
</form>
</center>
<?php include_once("foot.php"); ?>
</body>
</html>
Gewijzigd op 22/04/2016 15:42:51 door Kevin Zegikniet
Doe eens een print_r($_FILES) dan zie je hoe het in elkaar zit.
Dus wat ik bedacht is om bij elke $_Files["imgs"][ding] een [$i] (teller) erachter te zetten.
Wat dat doet is over elke letter van de filename loopen...
Sorry, maar ik zie het niet :(
- SanThe - op 22/04/2016 17:15:15:
Doe eens een print_r($_FILES) dan zie je hoe het in elkaar zit.
Laat het resultaat eens zien.
Het hoort ook niet op de sessie te beginnen nadat je output hebt verzonden..
Met dit stukje tekst krijg ik de output:
Code (php)
1
2
2
Size array: 1
Array ( [imgs] => Array ( [name] => foto.jpg [type] => image/jpeg [tmp_name] => /tmp/phpBT0baw [error] => 0 [size] => 1344938 ) )
Array ( [imgs] => Array ( [name] => foto.jpg [type] => image/jpeg [tmp_name] => /tmp/phpBT0baw [error] => 0 [size] => 1344938 ) )
Vul ik bij de eerste regel echter count($_FILES["imgs"]), dan wordt de size 5 (namelijk: de 5 dingen name, type, tmpname, error en size). Tot zover begrijp ik het.
Wat ik niet begrijp, is waarom array size 1 is waar het 2 moet zijn - oh toch wel.
Een array begint op index 0, en count geeft de laatste index terug dus moet ik loopen over
Nope. werkt niet. (Die teller ook geprobeerd ergens te zetten waar de filename geprint wordt om zo te kijken waar die in de rest van de code staan moet, maar dit was geen succes...
Gewijzigd op 25/04/2016 09:25:02 door Kevin Zegikniet