Jump to content

image upload and retrieve


jnrng2000

Recommended Posts

I tried to upload and display an image from a particular row in mysql. The upload was ok cause the image file was sent to the folder. However, the image would not display instead  X was diplayed at the image position. Other informations were displayed correctly.Could it be that my path was not correctly defined. Could someone help me .Thanks in anticipation. Here is the code.

<?php 

//This is the directory where images will be saved 
$ran = rand () ;
$ran2 = $ran.".";

$target = "images/"; 
$target = $target .$ran2. basename( $_FILES['photo']['name']); 
$ok=1;

//This gets all the other information from the form 

$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=($_FILES['photo']['name']); 

// Connects to your Database 
mysql_connect("localhost", "###", "####") or die(mysql_error()) ; 
mysql_select_db("####") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `table` (`name`, `email`,`phone`,`photo`) VALUES ('$name', '$email', '$phone', '$pic')") ; 

$id= mysql_insert_id(); 

//This is our size condition 
if ($uploaded_size > 35000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 

echo " Your Registration Form Number is . $id ";}
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file.";}

$query = "SELECT * FROM table WHERE id=$id";
$result = mysql_query($query)or die(mysql_error());  
while($row = mysql_fetch_array($result)) {
echo "<p>FORM NUMBER :62RRI/ <b>".$id."</b><br><br>";
echo "<img src=http://localhost/army/$target".$row['photo'].">  <br>"or die(mysql_error());    
echo "<p>Name: <b>".$row['name']."</b><br>"; 
echo "<p>E-Mail: <b>".$row['email']."</b><br>"; 
echo "<p>Phone Number: <b>".$row['phone']."</b><p>";
echo "<p>Your Photo: <b>".$row['photo']."</b><p>";  

} 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/98115-image-upload-and-retrieve/
Share on other sites

while storing the photo name in db it should add the random number you are generating while storing the pic

 

$target = $target .$ran2. basename( $_FILES['photo']['name']); 
$ok=1;

//This gets all the other information from the form 

$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=$ran2. basename( $_FILES['photo']['name']); 

 

cross check the pic name in db and the name of the file uploaded  ;)

 

The image file is suppose to be generated from what the visitors to the site uploaded and display their information when the file is uploaded. I made a little modification on the code. Still having problem. Some images displayed while some didnt. Here is the code.Thanks


<?php 

//This is the directory where images will be saved 
$ran = rand () ;
$ran2 = $ran.".";

$target = "images/"; 
$target = $target .$ran2. basename( $_FILES['photo']['name']); 
$ok=1;

//This gets all the other information from the form 

$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=$ran2.($_FILES['photo']['name']); 
$pic= $_POST['photo'];
// Connects to your Database 
mysql_connect("localhost", "###", "###") or die(mysql_error()) ; 
mysql_select_db("root") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `table` (`name`, `email`,`phone`,`photo`) VALUES ('$name', '$email', '$phone', '$photo')") ; 

$id= mysql_insert_id(); 

//This is our size condition 
if ($uploaded_size > 35000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 

echo " Your Registration Form Number is . $id ";}
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file.";}

$query = "SELECT * FROM table WHERE id=$id";
$result = mysql_query($query)or die(mysql_error());  
while($row = mysql_fetch_array($result)) {
echo "<p>FORM NUMBER :62RRI/ <b>".$id."</b><br><br>";
echo "<img src=http://localhost/army/$target".$row['photo'].">  <br>";    
echo "<p>Name: <b>".$row['name']."</b><br>"; 
echo "<p>E-Mail: <b>".$row['email']."</b><br>"; 
echo "<p>Phone Number: <b>".$row['phone']."</b><p>";
echo "<p>Your Photo: <b>".$row['photo']."</b><p>";  

} 

?>
[code]

[/code]

you are assigning $pic twice and you are not assigning value for photo

 

$phone=$_POST['phone'];

$pic=$ran2.($_FILES['photo']['name']);

$pic= $_POST['photo'];

 

New code try this


<?php 

//This is the directory where images will be saved 
$ran = rand () ;
$ran2 = $ran.".";

$target = "images/"; 
$target = $target .$ran2. basename( $_FILES['photo']['name']); 
$ok=1;

//This gets all the other information from the form 

$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$photo=$ran2.($_FILES['photo']['name']); 
$pic= $_POST['photo'];
// Connects to your Database 
mysql_connect("localhost", "###", "###") or die(mysql_error()) ; 
mysql_select_db("root") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `table` (`name`, `email`,`phone`,`photo`) VALUES ('$name', '$email', '$phone', '$photo')") ; 

$id= mysql_insert_id(); 

//This is our size condition 
if ($uploaded_size > 35000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 

echo " Your Registration Form Number is . $id ";}
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file.";}

$query = "SELECT * FROM table WHERE id=$id";
$result = mysql_query($query)or die(mysql_error());  
while($row = mysql_fetch_array($result)) {
echo "<p>FORM NUMBER :62RRI/ <b>".$id."</b><br><br>";
echo "<img src=http://localhost/army/$target".$row['photo'].">  <br>";    
echo "<p>Name: <b>".$row['name']."</b><br>"; 
echo "<p>E-Mail: <b>".$row['email']."</b><br>"; 
echo "<p>Phone Number: <b>".$row['phone']."</b><p>";
echo "<p>Your Photo: <b>".$row['photo']."</b><p>";  

} 

?>

Thank so much. It works. However i tried to query the datebase with the id  to display the information. Once again the image position was marked with X .Looks like there is problem with the code below.

  <?php 


// Connects to your Database 
mysql_connect("localhost", "###", "###") or die(mysql_error()) ; 
mysql_select_db("####") or die(mysql_error()) ;
//query the db

$id= $_POST['id'];


$query = "SELECT * FROM table WHERE id=$id";
$result = mysql_query($query)or die(mysql_error());  
while($row = mysql_fetch_array($result)) {
echo "<p>FORM NUMBER :62RRI/ <b>".$id."</b><br><br>";
echo "<img src=http://localhost/army/images/".$row['photo'].">  <br>";    
echo "<p>Name: <b>".$row['name']."</b><br>"; 
echo "<p>E-Mail: <b>".$row['email']."</b><br>"; 
echo "<p>Phone Number: <b>".$row['phone']."</b><p>";

}

?>   

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.