uploaden van meerdere foto's (Opgelost)
Gust - 12/06/2009 13:19 (laatste wijziging 12/06/2009 13:20)
MySQL interesse
Ik heb een formulier gemaakt waar ook meerdere foto's worden toegevoegd. Het is de bedoeling dat deze foto's in een map worden geplaatst. Het is ook de bedoeling dat de url naar die map in de database wordt geplaatst.
Maar de foto's worden maar niet toegevoegd. Wat doe ik verkeerd?
index.php
<form name="form1" method="post" action="verwerk.php">
<table width="99" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>titel<br>
<input type="text" name="titel2" id="titel2" size="100"/></td>
</tr>
<tr>
<td height="20"> </td>
</tr>
<tr>
<td>foto's<br>
<input type="file" name="pictures[]" id="pictures[]"/><br>
<input type="file" name="pictures[]3" id="pictures[]3"/><br>
<input type="file" name="pictures[]4" id="pictures[]4"/><br>
<input type="file" name="pictures[]5" id="pictures[]5"/><br>
</td>
</tr>
</table>
<input type="submit" name="button" id="button" value="Doorsturen" />
</form>
<form name = "form1" method = "post" action = "verwerk.php" > <table width = "99" border = "0" cellspacing = "0" cellpadding = "0" > <input type = "text" name = "titel2" id = "titel2" size = "100" / ></ td > <td height = "20" > </ td > <input type = "file" name = "pictures[]" id = "pictures[]" / ><br > <input type = "file" name = "pictures[]3" id = "pictures[]3" / ><br > <input type = "file" name = "pictures[]4" id = "pictures[]4" / ><br > <input type = "file" name = "pictures[]5" id = "pictures[]5" / ><br >
<input type = "submit" name = "button" id = "button" value = "Doorsturen" / >
verwerk.php
<?php
$username = "geheim";
$password = "geheim";
$host="localhost";
$dbnaam="geheim";
$files = array();
if ($_FILES["pictures"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "images/$name");
array_push($files, $name);
}
$db=mysql_connect($host, $username, $password) or die (mysql_error());
mysql_select_db($dbnaam, $db) or die (mysql_error());
echo "
<table width=\"400\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#CC0000\">
<tr>
<td>
<div align=\"center\"><b><font color=\"#CC0000\" face=\"Helvetica, Arial, sans-serif\" size=\"2\">
<br>";
if(!strlen($_POST['titel'])) {
echo ' Je hebt geen titel ingevuld <br><br>';
echo '<a href="javascript:history.back(-1)">Terug</a><br><br>';
echo '
<br>
</font></b></div>
<br>
</td>
</tr>
</table>
<br>
<br>
';
} else
{
$query="INSERT INTO tabel (titel, file1, file2, file3, file4) VALUES (\"$titel\", \"$files[0]\", \"$files[1]\", \"$files[2]\", \"$files[3]\");";
$file1 = $_POST["file1"];
$file2 = $_POST["file2"];
$file3 = $_POST["file3"];
$file4 = $_POST["file4"];
$result = mysql_query($query) or die(mysql_error());
?>
<html>
<head>
<meta http-equiv="REFRESH" content="5;URL=index.htm">
</head>
<body>
<center>
<?php
echo
"
De tour is succesvol toegevoegd in de lijst</span></p>
<p><span class=\"tekstrood\"> U wordt zometeen verdergestuurd<br>
<a href=\"index.htm\">of klik hier indien dit niet gebeurt</a></span><br><br>
";
}
?>
</center>
</body>
</html>
<?php
$username = "geheim" ;
$password = "geheim" ;
$host = "localhost" ;
$dbnaam = "geheim" ;
if ( $_FILES [ "pictures" ] [ "error" ] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES [ "pictures" ] [ "tmp_name" ] [ $key ] ;
$name = $_FILES [ "pictures" ] [ "name" ] [ $key ] ;
}
<table width=\" 400\" border=\" 1\" align=\" center\" cellpadding=\" 0\" cellspacing=\" 0\" bordercolor=\" #CC0000\" >
<tr>
<td>
<div align=\" center\" ><b><font color=\" #CC0000\" face=\" Helvetica, Arial, sans-serif\" size=\" 2\" >
<br>" ;
if ( ! strlen ( $_POST [ 'titel' ] ) ) { echo ' Je hebt geen titel ingevuld <br><br>' ; echo '<a href="javascript:history.back(-1)">Terug</a><br><br>' ;
<br>
</font></b></div>
<br>
</td>
</tr>
</table>
<br>
<br>
' ;
} else
{
$query = "INSERT INTO tabel (titel, file1, file2, file3, file4) VALUES (\" $titel \" , \" $files[0] \" , \" $files[1] \" , \" $files[2] \" , \" $files[3] \" );" ;
$file1 = $_POST [ "file1" ] ;
$file2 = $_POST [ "file2" ] ;
$file3 = $_POST [ "file3" ] ;
$file4 = $_POST [ "file4" ] ;
?>
<html>
<head>
<meta http-equiv="REFRESH" content="5;URL=index.htm">
</head>
<body>
<center>
<?php
"
De tour is succesvol toegevoegd in de lijst</span></p>
<p><span class=\" tekstrood\" > U wordt zometeen verdergestuurd<br>
<a href=\" index.htm\" >of klik hier indien dit niet gebeurt</a></span><br><br>
" ;
}
?>
</center>
</body>
</html>
10 antwoorden
Gesponsorde links
Wim - 12/06/2009 13:28 (laatste wijziging 12/06/2009 13:43)
Crew algemeen
<input type="file" name="pictures[]" id="pictures[]"/><br>
<input type="file" name="pictures[]3" id="pictures[]3"/><br>
<input type="file" name="pictures[]4" id="pictures[]4"/><br>
<input type="file" name="pictures[]5" id="pictures[]5"/><br>
< input type= "file" name= "pictures[]" id= "pictures[]" />< br>
< input type= "file" name= "pictures[]3" id= "pictures[]3" />< br>
< input type= "file" name= "pictures[]4" id= "pictures[]4" />< br>
< input type= "file" name= "pictures[]5" id= "pictures[]5" />< br>
moet zijn:
<input type="file" name="pictures[]" /><br>
<input type="file" name="pictures[]" /><br>
<input type="file" name="pictures[]" /><br>
<input type="file" name="pictures[]" /><br>
< input type= "file" name= "pictures[]" />< br>
< input type= "file" name= "pictures[]" />< br>
< input type= "file" name= "pictures[]" />< br>
< input type= "file" name= "pictures[]" />< br>
verder gebruik je op lijn 11 en 12 "$key", maar deze wordt nergens gedefineert?
$_POST['pictures'] is nu een array. Je kan die doorlopen met PHP.net: foreach . Bvb:
foreach ($_POST['pictures'] as $picture)
{
print_r($picture); // deze is hier hetzelfde dan $_POST['picutre'] bij een single upload zou zijn
}
foreach ( $_POST [ 'pictures' ] as $picture )
{
print_r ( $picture ) ; // deze is hier hetzelfde dan $_POST['picutre'] bij een single upload zou zijn }
Gust - 12/06/2009 13:38
MySQL interesse
Dus in de plaats van:
if ($_FILES["pictures"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "images/$name");
array_push($files, $name);
}
if ( $_FILES [ "pictures" ] [ "error" ] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES [ "pictures" ] [ "tmp_name" ] [ $key ] ;
$name = $_FILES [ "pictures" ] [ "name" ] [ $key ] ;
}
Moet ik dit doen:
foreach ($_POST['pictures'] as $picture)
{
print_r($picture); // deze is hier hetzelfde dan $_POST['picutre'] bij een single upload zou zijn
}
foreach ( $_POST [ 'pictures' ] as $picture )
{
print_r ( $picture ) ; // deze is hier hetzelfde dan $_POST['picutre'] bij een single upload zou zijn }
Dan zie ik alleen in de output het volgende:
C:\Users\pc7\Desktop\boot_2.jpg maar intussen wordt er niets toegevoegd aan de database
Wim - 12/06/2009 13:45
Crew algemeen
Bestanden op jouw pc kunnen wij niet bekijken... "C:\Users\pc7\Desktop\boot_2.jpg" is voor ons dus nutteloos...
Verder, je moet de code niet klakkeloos overnemen. Ik zette in de foreach-loop "print_r($picture)" zodat het duidelijk zou zijn dat het een array was, en dat deze gelijk is aan een normale array van een FILE-veld. (maw met tmp_name, name, error, ...)
Gust - 12/06/2009 13:47
MySQL interesse
Maar weet je dan hoe het wel moet?
marten - 12/06/2009 14:32
Beheerder
ipv $_POST['pictures'] moet je $_FILES['pictures'] gebruiken.
Gust - 12/06/2009 15:06
MySQL interesse
Ik heb een var_dump($_FILES); gedaan en zie dit:
array(1) { ["pictures"]=> array(5) { ["name"]=> array(4) { [0]=> string(12) "E-ticket.jpg" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" } ["type"]=> array(4) { [0]=> string(11) "image/pjpeg" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" } ["tmp_name"]=> array(4) { [0]=> string(18) "/usr/tmp/phpp5gsrv" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" } ["error"]=> array(4) { [0]=> int(0) [1]=> int(4) [2]=> int(4) [3]=> int(4) } ["size"]=> array(4) { [0]=> int(421702) [1]=> int(0) [2]=> int(0) [3]=> int(0) } } }
Ik heb ook een print_r($_FILES['pictures']) gedaan en zie dit:
Array ( [name] => Array ( [0] => E-ticket.jpg [1] => [2] => [3] => ) [type] => Array ( [0] => image/pjpeg [1] => [2] => [3] => ) [tmp_name] => Array ( [0] => /usr/tmp/phpp5gsrv [1] => [2] => [3] => ) [error] => Array ( [0] => 0 [1] => 4 [2] => 4 [3] => 4 ) [size] => Array ( [0] => 421702 [1] => 0 [2] => 0 [3] => 0 ) )
Wim - 12/06/2009 15:25
Crew algemeen
Ja... Is toch duidelijk? Het is een array van meerdere FILE-array's. (2dimensionele array)
Gust - 12/06/2009 15:39
MySQL interesse
Maar wil dat dan zeggen dat ik fout bezig ben of is dat normaal?
marten - 12/06/2009 15:47
Beheerder
Dat is normaal. Nu ga je in de foreach zoals je al had, de functie move_uploaded_file gebruiken om de bestanden te kopieren naar je eigen server vanuit de temp dir.
Gust - 12/06/2009 16:11 (laatste wijziging 12/06/2009 16:52)
MySQL interesse
Dit bedoel je?
(move_uploaded_file($_FILES['pictures']['tmp_name'], $uploadfile));
Even een stand van zaken:
index.php
<form name="form1" method="post" action="verwerk.php">
<table width="99" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>titel<br>
<input type="text" name="titel2" id="titel2" size="100"/></td>
</tr>
<tr>
<td height="20"> </td>
</tr>
<tr>
<td>foto's<br>
<input type="file" name="pictures[]" id="pictures[]"/><br>
<input type="file" name="pictures[]" id="pictures[]"/><br>
<input type="file" name="pictures[]" id="pictures[]"/><br>
<input type="file" name="pictures[]" id="pictures[]"/><br>
</td>
</tr>
</table>
<input type="submit" name="button" id="button" value="Doorsturen" />
</form>
<form name = "form1" method = "post" action = "verwerk.php" > <table width = "99" border = "0" cellspacing = "0" cellpadding = "0" > <input type = "text" name = "titel2" id = "titel2" size = "100" / ></ td > <td height = "20" > </ td > <input type = "file" name = "pictures[]" id = "pictures[]" / ><br > <input type = "file" name = "pictures[]" id = "pictures[]" / ><br > <input type = "file" name = "pictures[]" id = "pictures[]" / ><br > <input type = "file" name = "pictures[]" id = "pictures[]" / ><br >
<input type = "submit" name = "button" id = "button" value = "Doorsturen" / >
verwerk.php
<?php
$username = "geheim";
$password = "geheim";
$host="localhost";
$dbnaam="geheim";
$files = array();
if ($_FILES["pictures"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "images/$name");
array_push($files, $name);
}
foreach ($_FILES['pictures'] as $picture)
(move_uploaded_file($_FILES['pictures']['tmp_name'], $uploadfile));
$db=mysql_connect($host, $username, $password) or die (mysql_error());
mysql_select_db($dbnaam, $db) or die (mysql_error());
echo "
<style>
body {
background-color: #FFFFCC;
}
</style>
<div align=\"center\"><b><font color=\"#CC0000\" face=\"Helvetica, Arial, sans-serif\" size=\"4\">Tours</font></b></div><br><br>
<table width=\"400\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#CC0000\">
<tr>
<td>
<div align=\"center\"><b><font color=\"#CC0000\" face=\"Helvetica, Arial, sans-serif\" size=\"2\">
<br>";
#Deze functie gaat na of er een geldig e-mail adres werd opgegeven.
if(!strlen($_POST['titel'])) {
echo ' Je hebt geen titel ingevuld <br><br>';
echo '<a href="javascript:history.back(-1)">Terug</a><br><br>';
echo '
<br>
</font></b></div>
<br>
</td>
</tr>
</table>
<br>
<br>
';
} else
{
$file1 = basename($_FILES["pictures"]['name'][0]);
$file2 = basename($_FILES["pictures"]['name'][1]);
$file3 = basename($_FILES["pictures"]['name'][2]);
$file4 = basename($_FILES["pictures"]['name'][3]);
(move_uploaded_file($_FILES['pictures']['tmp_name'], $uploadfile));
$query="INSERT INTO tabel (titel, file1, file2, file3, file4) VALUES (\"$titel\", \"$files[0]\", \"$files[1]\", \"$files[2]\", \"$files[3]\");";
(move_uploaded_file($_FILES['pictures']['tmp_name'], $uploadfile));
$result = mysql_query($query) or die(mysql_error());
//var_dump($_FILES);
?>
<html>
<head>
<title></title>
<style>
body {
background-color: #FFFFCC;
}
.tekstrood {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color:#CC0000;
}
.tekstzwart {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#000000;
}
</style>
</head>
<body>
<center>
<?php
//<meta http-equiv="REFRESH" content="1;URL=index.htm">
echo
"
De tour is succesvol toegevoegd in de lijst</span></p>
<p><span class=\"tekstrood\"> U wordt zometeen verdergestuurd<br>
<a href=\"index.htm\">of klik hier indien dit niet gebeurt</a></span><br><br>
";
}
//print_r($_FILES['pictures'])
?>
</center>
</body>
</html>
<?php
$username = "geheim" ;
$password = "geheim" ;
$host = "localhost" ;
$dbnaam = "geheim" ;
if ( $_FILES [ "pictures" ] [ "error" ] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES [ "pictures" ] [ "tmp_name" ] [ $key ] ;
$name = $_FILES [ "pictures" ] [ "name" ] [ $key ] ;
}
foreach ( $_FILES [ 'pictures' ] as $picture )
<style>
body {
background-color: #FFFFCC;
}
</style>
<div align=\" center\" ><b><font color=\" #CC0000\" face=\" Helvetica, Arial, sans-serif\" size=\" 4\" >Tours</font></b></div><br><br>
<table width=\" 400\" border=\" 1\" align=\" center\" cellpadding=\" 0\" cellspacing=\" 0\" bordercolor=\" #CC0000\" >
<tr>
<td>
<div align=\" center\" ><b><font color=\" #CC0000\" face=\" Helvetica, Arial, sans-serif\" size=\" 2\" >
<br>" ;
#Deze functie gaat na of er een geldig e-mail adres werd opgegeven.
if ( ! strlen ( $_POST [ 'titel' ] ) ) { echo ' Je hebt geen titel ingevuld <br><br>' ; echo '<a href="javascript:history.back(-1)">Terug</a><br><br>' ;
<br>
</font></b></div>
<br>
</td>
</tr>
</table>
<br>
<br>
' ;
} else
{
$file1 = basename ( $_FILES [ "pictures" ] [ 'name' ] [ 0 ] ) ; $file2 = basename ( $_FILES [ "pictures" ] [ 'name' ] [ 1 ] ) ; $file3 = basename ( $_FILES [ "pictures" ] [ 'name' ] [ 2 ] ) ; $file4 = basename ( $_FILES [ "pictures" ] [ 'name' ] [ 3 ] ) ;
$query = "INSERT INTO tabel (titel, file1, file2, file3, file4) VALUES (\" $titel \" , \" $files[0] \" , \" $files[1] \" , \" $files[2] \" , \" $files[3] \" );" ;
//var_dump($_FILES);
?>
<html>
<head>
<title></title>
<style>
body {
background-color: #FFFFCC;
}
.tekstrood {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color:#CC0000;
}
.tekstzwart {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#000000;
}
</style>
</head>
<body>
<center>
<?php
//<meta http-equiv="REFRESH" content="1;URL=index.htm">
"
De tour is succesvol toegevoegd in de lijst</span></p>
<p><span class=\" tekstrood\" > U wordt zometeen verdergestuurd<br>
<a href=\" index.htm\" >of klik hier indien dit niet gebeurt</a></span><br><br>
" ;
}
//print_r($_FILES['pictures'])
?>
</center>
</body>
</html>
databasegegevens:
titel, file1, file2, file3, file4
Gesponsorde links
Dit onderwerp is gesloten .