Upload imagini in mysql

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
MelecaCristian
Mesaje:176

Upload imagini in mysql

buna ... se poate uploada imagini pe un server mysql adica nu imi trebuie neaparat in mysql lar la fiecare utilizator sa ii apara pozele ce si le uploadeaza ... adica daca eu sunt conectat ca admin el sa gaseasca sursa cu pozele lui admin si sa imi arate o poza cu mine care o selectez eu :) ... se poate ... multumesc (pentru inregistrare folosesc scriptul de pe site )
succes tuturor!
„Uneori o greşeală poate fi tot ce este necesar pentru o realizare valoroasă.” — Henry Ford

MarPlo Mesaje:4343
Asa cum este scriptul acela nu se poate.
Trebuie mai multe modificari:
1. O coloana noua in tabel, in care sa fie adaugata adresa /adresele cu pozele.
2. Un camp de Upload in formularul de inregistrare, prin care sa fie trimisa poza.
3. Instructiuni in scriptul php care face inregistrarea, prin care sa preia poza, sa o copie intr-un director pt. poze, si sa adauge calea si numele in coloaana din tabel.
Cine vrea sa faca, spor la treaba.

MelecaCristian Mesaje:176
am incercat cu acest script si nu imi merge :

baza de date :

Cod: Selectaţi tot

CREATE TABLE `images` (
`img` INT NOT NULL AUTO_INCREMENT ,
`data` LONGTEXT NOT NULL ,
PRIMARY KEY ( `img` ) 
);
script simplu de upload :

Cod: Selectaţi tot

<?php 
//connect to database. Username and password need to be changed 
mysql_connect("localhost", "username", "password"); 

//Select database, database_name needs to be changed 
mysql_select_db("database_name"); 

if (!$_POST['uploaded']){ 
//If nothing has been uploaded display the form 
?> 

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post"  
ENCTYPE="multipart/form-data"> 
Upload:<br><br> 
<input type="file" name="image"><br><br> 
<input type="hidden" name="uploaded" value="1"> 
<input type="submit" value="Upload"> 
</form> 

<? 
}else{ 
//if the form hasn't been submitted then: 

//from here onwards, we are copying the file to the directory you made earlier, so it can then be moved  
//into the database. The image is named after the persons IP address until it gets moved into the database 

//get users IP 
$ip=$REMOTE_ADDR; 

//don't continue if an image hasn't been uploaded 
if (!empty($image)){ 

//copy the image to directory 
copy($image, "./temporary/".$ip.""); 

//open the copied image, ready to encode into text to go into the database 
$filename1 = "./temporary/".$REMOTE_ADDR; 
$fp1 = fopen($filename1, "r"); 

//record the image contents into a variable 
$contents1 = fread($fp1, filesize($filename1)); 

//close the file 
fclose($fp1); 

//encode the image into text 
$encoded = chunk_split(base64_encode($contents1));  

//insert information into the database 
mysql_query("INSERT INTO images (img,data)"."VALUES ('NULL', '$encoded')"); 

//delete the temporary file we made 
unlink($filename1); 
} 

//end 
} 
?>
si script pentru vizualizare dupa incarcare :

Cod: Selectaţi tot

<?php 
//connect to database. Username and password need to be changed 
$connection=mysql_connect("localhost", "username", "password"); 

//Select database, database_name needs to be changed 
mysql_select_db("database_name"); 

//get decoded image data from database 
$result=mysql_query("SELECT * FROM images WHERE img='".$_GET['img']."'"); 

//fetch data from database 
$data=mysql_fetch_array($result); 
$encoded=$data['data']; 

//note: "$data['data']" is the row "data" in the table we made. 
//The image ID would be "$data['img']" for example 


//close connection 
mysql_close($connection); 

//decode and echo the image data 
echo base64_decode($encoded); 

//end 
?>
si mai e si un director 'temporary'
si nu stiu unde am gresit de nu imi adauga datele in baza de date si inclusiv nu pot vedea imaginea daca nu o adauga :( unde e gresit scriptul ??
scz pentru bataia de cap
„Uneori o greşeală poate fi tot ce este necesar pentru o realizare valoroasă.” — Henry Ford

Subiecte similare