File upload mislukt steeds
Ik heb een html form waarbij gebruikers foto's kunnen uploaden. Helaas lukt het uploaden niet, hij komt steeds terug op dat het bestand ongeldig is...
Dit is de handler:
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
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
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$connection=mysql_connect ('localhost', '***', '***');
if (!$connection) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('***', $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$file = $_FILES['file'];
$name = $file['name'];
$path = "/home/***/domains/***.nl/public_html/data/fotos/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
// Move succeed.
$newpath = preg_replace('#/home/***/domains/***/public_html/artistone/#', 'http://***.nl/', $path);
$sql = "INSERT INTO fotoboek (url, titel, dataid) VALUES ('" . mysql_real_escape_string($newpath) . "', '" . $_POST['titel'] . "', '" . $_POST['dataid'] . "')";
$result = mysql_query($sql);
// echo "Opgeslagen en toegevoegd aan DB.";
header ('location: ../gallery.php?upload=ok');
} else {
// Move failed. Possible duplicate?
echo "ERROR: Dubbele file";
header ('location: ../addphoto.php?error=dubbel');
}
}
}
else
{
header ('location: ../addphoto.php?error=ongeldig');
}
?>
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$connection=mysql_connect ('localhost', '***', '***');
if (!$connection) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('***', $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$file = $_FILES['file'];
$name = $file['name'];
$path = "/home/***/domains/***.nl/public_html/data/fotos/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
// Move succeed.
$newpath = preg_replace('#/home/***/domains/***/public_html/artistone/#', 'http://***.nl/', $path);
$sql = "INSERT INTO fotoboek (url, titel, dataid) VALUES ('" . mysql_real_escape_string($newpath) . "', '" . $_POST['titel'] . "', '" . $_POST['dataid'] . "')";
$result = mysql_query($sql);
// echo "Opgeslagen en toegevoegd aan DB.";
header ('location: ../gallery.php?upload=ok');
} else {
// Move failed. Possible duplicate?
echo "ERROR: Dubbele file";
header ('location: ../addphoto.php?error=dubbel');
}
}
}
else
{
header ('location: ../addphoto.php?error=ongeldig');
}
?>
Kan iemand mij vertellen waarom hij t niet doet?
Gewijzigd op 14/02/2013 21:17:16 door Rick de Jong
Kijk eens met print_r() naar $_FILES
Array ( )
Waar plaats je die code? En laat je formulier eens zien waarmee je de bestanden uploadt?
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<form action="handlers/add-photo.php" method="POST" class="validate_form">
<fieldset class="label_side top">
<label>Foto naam<span></span></label>
<div>
<input id="required_field" name="titel" type="text" class="required">
<div class="required_tag"></div>
</div>
</fieldset>
<fieldset class="label_side">
<label>Upload de foto<span>PNG, JPG, JPEG en GIF worden geaccepteerd.</span></label>
<div class="clearfix"><br><br>
<input type="file" name="file" />
<div class="required_tag"></div>
</div>
</fieldset>
<div class="button_bar clearfix">
<button class="green" type="submit">
<img height="24" width="24" alt="Bended Arrow Right" src="images/icons/small/white/bended_arrow_right.png">
<span>Upload foto</span>
</button>
<p><b>Opmerking:</b> Als kiest om de foto te uploaden stemt u in de met de voorwaarden die gelden voor afbeeldingen.</p>
</div>
</form>
<fieldset class="label_side top">
<label>Foto naam<span></span></label>
<div>
<input id="required_field" name="titel" type="text" class="required">
<div class="required_tag"></div>
</div>
</fieldset>
<fieldset class="label_side">
<label>Upload de foto<span>PNG, JPG, JPEG en GIF worden geaccepteerd.</span></label>
<div class="clearfix"><br><br>
<input type="file" name="file" />
<div class="required_tag"></div>
</div>
</fieldset>
<div class="button_bar clearfix">
<button class="green" type="submit">
<img height="24" width="24" alt="Bended Arrow Right" src="images/icons/small/white/bended_arrow_right.png">
<span>Upload foto</span>
</button>
<p><b>Opmerking:</b> Als kiest om de foto te uploaden stemt u in de met de voorwaarden die gelden voor afbeeldingen.</p>
</div>
</form>
Gewijzigd op 14/02/2013 22:04:31 door Rick de Jong
<form action="handlers/add-photo.php" method="POST" class="validate_form" enctype="multipart/form-data">
Nick Dijkstra op 14/02/2013 22:07:07:
<form action="handlers/add-photo.php" method="POST" class="validate_form" enctype="multipart/form-data">
Dat is 'm! Bedankt!
jups, enctype="multipart/form-data" moet er wel bij