Jump to content

Recommended Posts

Hi,

Can someone please help, i want to upload 3 images, this is a code i got off the net but only uploads one file, (photo)

I have 2 new fields in my sql file called photo2 and photo3, the image names and all other information from the form get sent successfully , but i cannot work out how to upload the other 2 files,

My field is called photo in my sql database but the code below uses the word (pic) i dont know why as i never wrote it but it works.

 

i have this code i use in my add property file...

 

<?php

 

//This is the directory where images will be saved

$target = "../images/";

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

 

 

//This gets all the other information from the form

$name=$_POST['name'];

$suburb=$_POST['suburb'];

$price=$_POST['price'];

$content=$_POST['content'];

$photo2=$_FILES['photo2']['name'];

$photo3=$_FILES['photo3']['name'];

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

 

 

// Connects to your Database

mysql_connect("localhost", "root", "**********") or die(mysql_error()) ;

mysql_select_db("gcproperty") or die(mysql_error()) ;

 

//Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$pic', '$photo2', '$photo3')") ;

 

//Writes the photo to the server

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

{

 

 

//Tells you if its all ok

print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";

}

else {

 

//Gives and error if its not

echo "Sorry, there was a problem uploading your file.";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/70315-upload-files/
Share on other sites

it only works on the first phot because that's the only one specificed to move to your target folder, found here in your code...

 

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 

 

the code you have now is rather vulerable to mysql injection among other things, because you have no sort of filters for your photos and data input. but i'm too tired to go into that. try the code below. if it doesn't work, just copy the if else statement above (the WHOLE thing, including the else, "sorry...problem", and closing brace. and just change 'photo' to 'photo2', etc..

 

 

 



<?php

//This is the directory where images will be saved
$target = "../images/";
$target = $target . basename( $_FILES['photo']['name']);


//This gets all the other information from the form
$name=$_POST['name'];
$suburb=$_POST['suburb'];
$price=$_POST['price'];
$content=$_POST['content'];
$photo2=$_FILES['photo2']['name'];
$photo3=$_FILES['photo3']['name'];
$pic=($_FILES['photo']['name']);


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

//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$pic', '$photo2', '$photo3')") ;

//Writes the photo to the server
foreach($_FILES as $val){
  if(!move_uploaded_file($_FILES[$val]['tmp_name'], $target)){
    $error = yes;
  }
}

if($error){
  //Tells you if its all ok
  print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
} else {
  //Gives and error if its not
  echo "Sorry, there was a problem uploading your file.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/70315-upload-files/#findComment-353254
Share on other sites

Hey ben

I Know it only works on the first phot because that's the only one specificed to move to your target folder, do you know an easy way to alter the code to include the other 2 files (photo2 and photo3) i will add some filters after i get the code working.

Thanks for your help, is one of the the last problems i have in this project im doing, if you can help me i am willing to pay you, or donate to this site.

Really want to wrap this up.

 

it only works on the first phot because that's the only one specificed to move to your target folder, found here in your code...

 

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 

 

the code you have now is rather vulerable to mysql injection among other things, because you have no sort of filters for your photos and data input. but i'm too tired to go into that. try the code below. if it doesn't work, just copy the if else statement above (the WHOLE thing, including the else, "sorry...problem", and closing brace. and just change 'photo' to 'photo2', etc..

 

 

 



<?php

//This is the directory where images will be saved
$target = "../images/";
$target = $target . basename( $_FILES['photo']['name']);


//This gets all the other information from the form
$name=$_POST['name'];
$suburb=$_POST['suburb'];
$price=$_POST['price'];
$content=$_POST['content'];
$photo2=$_FILES['photo2']['name'];
$photo3=$_FILES['photo3']['name'];
$pic=($_FILES['photo']['name']);


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

//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$pic', '$photo2', '$photo3')") ;

//Writes the photo to the server
foreach($_FILES as $val){
  if(!move_uploaded_file($_FILES[$val]['tmp_name'], $target)){
    $error = yes;
  }
}

if($error){
  //Tells you if its all ok
  print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";
} else {
  //Gives and error if its not
  echo "Sorry, there was a problem uploading your file.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/70315-upload-files/#findComment-353311
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.