Pomoc oko smestanja putanje uploadovane slike u bazu podataka!

joca 013

Početnik
Poruka
1
index.php

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" id="file1"><br>
<input type="file" name="file[]" id="file2"><br>
<input type="file" name="file[]" id="file3"><br>
<input type="file" name="file[]" id="file4"><br>
<input type="file" name="file[]" id="file5"><br>
<input type="file" name="file[]" id="file6"><br>
<input type="file" name="file[]" id="file7"><br>
<input type="file" name="file[]" id="file8"><br>
<input type="file" name="file[]" id="file9"><br>
<input type="file" name="file[]" id="file10"><br>
<input type="submit" name="submit" value="Dodaj slike">
</form>



upload_file.php

<?php

for ($i=0;$i<count($_FILES['file']['name']);$i++){
$ran_num = mt_rand().mt_rand().mt_rand().mt_rand().mt_ra nd();
echo "<br>";
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"][$i]);
$extension = end($temp);
if ((($_FILES["file"]["type"][$i] == "image/gif")
|| ($_FILES["file"]["type"][$i] == "image/jpeg")
|| ($_FILES["file"]["type"][$i] == "image/jpg")
|| ($_FILES["file"]["type"][$i] == "image/pjpeg")
|| ($_FILES["file"]["type"][$i] == "image/x-png")
|| ($_FILES["file"]["type"][$i] == "image/png"))
&& ($_FILES["file"]["size"][$i] < 1000000) && ($_FILES["file"]["error"][$i]==0)
&& in_array($extension, $allowedExts)){
echo "Upload: " .$ran_num. $_FILES["file"]["name"][$i] . "<br>";
echo "Type: " . $_FILES["file"]["type"][$i] . "<br>";
echo "Size: " . ($_FILES["file"]["size"][$i] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"][$i]."<br>";
if (file_exists("upload/".$ran_num . $_FILES["file"]["name"][$i])){
echo "<b>".$_FILES["file"]["name"][$i] . " already exists.</b><br> ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"][$i],
"upload/" .$ran_num. $_FILES["file"]["name"][$i]);
echo "<b>Uploaded to</b>: " . "upload/" .$ran_num. $_FILES["file"]["name"][$i]."<br>";
}
}
}
?>

Ovim kodom mogu dodati 10 slika od jednom,i ispred imena dodati random number od jedno 40-tak karaktera.Mislim daje veoma mala verovatnoca da dve slike imaju isto ime.
E sad ako moze mala pomoc!
Kako putanju te slike da smestim u bazu?
Na primer ako je Pera Peric dodao te slike da posle iz baze mogu da pozovem Perine slike i da mi izbaci te slike koje je on dodao?
Hvala svima i pozz!!
 
U bazi cuvas korisnicko ime u jednom polju, u drugom naziv slike...
I kasnije jednostavno pristupas:
Kod:
$korisnik='Pera Peric';
$data=mysql_query("SELECT naziv_slike FROM albumi WHERE korisnik='$korisnik'");
while($niz=mysql_fetch_assoc($data))
{
echo "<img src='upload/".$niz['naziv_slike']."' />";
}

A upis u bazu:
Kod:
else { //deo tvog koda citiram, sa izmenama
move_uploaded_file($_FILES["file"]["tmp_name"][$i],
"upload/" .$ran_num. $_FILES["file"]["name"][$i]);
echo "<b>Uploaded to</b>: " . "upload/" .$ran_num. $_FILES["file"]["name"][$i]."<br>";
mysql_query("INSERT INTO albumi(naziv_slike,korisnik) VALUES('".$ran_num. $_FILES["file"]["name"][$i]."','$username')");
}
 

Back
Top