Jump to content

[SOLVED] Multi file upload


emediastudios

Recommended Posts

Hi all.

I have a code that works fine except for one thing, it only uploads (1) image and i want to upload (4).

the code i have does all ehat i want and inserts the file name in to my SQL database along with some other info.

Can someone tell me how to alter my code to upload (4)images, and record there image name into sql.

the code i have below i altered to process (3) pics but it doesnt upload the images and  only inserts there image file name into sql.

 

 

Thanks so much...........

Heres the code i have

 

<?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'];

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

$pic2=($_FILES['photo2']['name']);

$pic3=($_FILES['photo3']['name']);

 

 

// Connects to your Database

mysql_connect("localhost", "root", "5050888202") 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', '$pic2', '$pic3')") ;

 

//Writes the photo to the server

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  --------------My problem needs to sorted here somewhere i think.

{

 

//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.";

}

?>[/color]

 

Link to comment
https://forums.phpfreaks.com/topic/70588-solved-multi-file-upload/
Share on other sites

Evidently it's there, since you only call move_uploaded_file() with 'photo' and never with 'photo2' and 'photo3'. Try:

 

<?php
if ( move_uploaded_file($_FILES['photo']['tmp_name'], $target) && move_uploaded_file($_FILES['photo2']['tmp_name'], $target) && move_uploaded_file($_FILES['photo3']['tmp_name'], $target) )
{
...rest of your code...
?>

 

That way you check for the three uploads, if one fails, all fails.

 

 

Thanks, i gave it a go, still inserts all the image names and other info but only uploads, the first (photo)

Doesnt upload the other images to the directory (photo1 and photo2)

 

This is what im using now

 

------------------------------------------------------------------------

 

<?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'];

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

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

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

 

 

// Connects to your Database

mysql_connect("localhost", "root", "5050888202") 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', '$photo', '$photo2', '$photo3')") ;

 

//Writes the photo to the server

if ( move_uploaded_file($_FILES['photo']['tmp_name'], $target) && move_uploaded_file($_FILES['photo2']['tmp_name'], $target) && move_uploaded_file($_FILES['photo3']['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.";

}

?>

Blah, silly me, forgot an important part. You're basically saving the three photos to the same file, Try this at the beggining of your code:

 

<?php
$target = "../images/";
$target1 = $target . basename( $_FILES['photo']['name']);
$target2 = $target . basename( $_FILES['photo1']['name']); 
$target3 = $target . basename( $_FILES['photo2']['name']);
?>

 

Then at the if():

 

<?php
if ( move_uploaded_file($_FILES['photo']['tmp_name'], $target1) && move_uploaded_file($_FILES['photo2']['tmp_name'], $target2) && move_uploaded_file($_FILES['photo3']['tmp_name'], $target3) )
{
...etc... ?>

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.