Afbeeldings url in database opslaan
Ik zit met het volgende:
Hoe kan ik hetvoor elkaar krijgen als ik een afbeelding upload naar mijn server (via een formulier) dat hij de url naar het plaatje in de database zet.
Absolute links zoals 'http://www.domein.nl/plaatje.png' moet je nooit in de database plaatsen. Mocht je domein ooit veranderen (en zeg nooit dat het nooit gebeurt ;-) ), dan moet je een hoop aanpassen.
Ikzelf hernoem bij een upload met move_uploaded_files() de bestanden naar een timestamp met mktime(). Soms wil je liever niet dat ze makkelijk te raden zijn....
Gewijzigd op 27/07/2013 15:05:12 door - Ariën -
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
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
echo "Upload: " . $_FILES["file3"]["name"] . "<br>";
echo "Type: " . $_FILES["file3"]["type"] . "<br>";
echo "Size: " . ($_FILES["file3"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file3"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
if (file_exists("upload/" . $_FILES["file2"]["name"])) {
echo $_FILES["file2"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file2"]["tmp_name"],
"upload/" . $_FILES["file2"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file2"]["name"];
}
if (file_exists("upload/" . $_FILES["file3"]["name"])) {
echo $_FILES["file3"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file3"]["tmp_name"],
"upload/" . $_FILES["file3"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file3"]["name"];
}
echo "Type: " . $_FILES["file3"]["type"] . "<br>";
echo "Size: " . ($_FILES["file3"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file3"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
if (file_exists("upload/" . $_FILES["file2"]["name"])) {
echo $_FILES["file2"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file2"]["tmp_name"],
"upload/" . $_FILES["file2"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file2"]["name"];
}
if (file_exists("upload/" . $_FILES["file3"]["name"])) {
echo $_FILES["file3"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file3"]["tmp_name"],
"upload/" . $_FILES["file3"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file3"]["name"];
}
Gewijzigd op 27/07/2013 15:41:31 door Obelix Idefix
Gewijzigd op 27/07/2013 15:47:01 door Shamrock Modelbouw
te snel gereageerd.
Die word gestored in de variabele $pid.
dan ipv mysql_insert_id(), $_POST['p_id'] ?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
$path = $_FILES['file3']['name'];
if (file_exists("upload/" . $path)) {
echo $path . " already exists. <br>";
} else {
$ext = pathinfo($path, PATHINFO_EXTENSION);
move_uploaded_file($path,
"upload/" . $_POST['p_id'] . "F.".$ext);
echo "Stored in: " . "upload/" . $_POST['p_id'] . "F.".$ext."<br>";
}
if (file_exists("upload/" . $path)) {
echo $path . " already exists. <br>";
} else {
$ext = pathinfo($path, PATHINFO_EXTENSION);
move_uploaded_file($path,
"upload/" . $_POST['p_id'] . "F.".$ext);
echo "Stored in: " . "upload/" . $_POST['p_id'] . "F.".$ext."<br>";
}
niet getest, maar zoiets zou moeten werken
Gewijzigd op 27/07/2013 15:59:23 door Shamrock Modelbouw
werkt niet, de afbeelding komt niet in de map te staan.
krijg je een fout? of echood hij Stored in: " . "upload/" . $_POST['p_id'] . "F.".$ext."<br>
hij echood dat hij wel opgeslagen is idd gewoon met de filename. Maar hij komt niet in de map terecht.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$path = $_FILES['file3']['name'];
if (file_exists("upload/" . $path)) {
echo $path . " already exists. <br>";
} else {
$ext = pathinfo($path, PATHINFO_EXTENSION);
if(move_uploaded_file($path,
"upload/" . $_POST['p_id'] . "F.".$ext))
{
echo "Stored in: " . "upload/" . $_POST['p_id'] . "F.".$ext."<br>";
}
else
{
echo "Fout";
}
}
if (file_exists("upload/" . $path)) {
echo $path . " already exists. <br>";
} else {
$ext = pathinfo($path, PATHINFO_EXTENSION);
if(move_uploaded_file($path,
"upload/" . $_POST['p_id'] . "F.".$ext))
{
echo "Stored in: " . "upload/" . $_POST['p_id'] . "F.".$ext."<br>";
}
else
{
echo "Fout";
}
}
Gewijzigd op 27/07/2013 16:11:46 door Shamrock Modelbouw
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
$path3 = $_FILES["file3"]["name"];
if (file_exists("upload/" . $path3)) {
echo $path3 . " already exists. <br>";
} else {
$ext3 = pathinfo($path3, PATHINFO_EXTENSION);
move_uploaded_file($_FILES["file3"]["tmp_name"],
"upload/" . $_POST['p_id'] . "F.".$ext3);
echo "Stored in: " . "upload/" . $_POST['p_id'] . "F.".$ext3."<br>";
}
if (file_exists("upload/" . $path3)) {
echo $path3 . " already exists. <br>";
} else {
$ext3 = pathinfo($path3, PATHINFO_EXTENSION);
move_uploaded_file($_FILES["file3"]["tmp_name"],
"upload/" . $_POST['p_id'] . "F.".$ext3);
echo "Stored in: " . "upload/" . $_POST['p_id'] . "F.".$ext3."<br>";
}