Jump to content

Multiple image upload:-


billy_111

Recommended Posts

Hey,

 

I am trying to upload a number of images to my server at the same time in the same code, but am having problems. My code is shown below:-

 

	<?php
	if (isset($_POST['btn_update']))
	{
		if($_POST['txt_c_name'] == "" || $_POST['txt_name'] == "" || $_POST['txt_desc'] == "" || $_POST['txt_price'] == "")
		{
		echo "<p style='color:red;padding:4px'>Please fill in all the fields.</p>";
		}
		elseif($_POST['thing'] == "0")
		{
		echo "<p style='color:red;padding:4px'>Please select a category.</p>";
		}
		else
		{				
			include("conn.php");

			$path1= 'garments/'.$_FILES['ufile'][0]['name'];
			$path2= 'garments/'.$_FILES['ufile'][1]['name'];
			$path3= 'garments/'.$_FILES['ufile'][2]['name'];
			$path4= 'garments/'.$_FILES['ufile'][3]['name'];
			$path5= 'garments/'.$_FILES['ufile'][4]['name'];

			copy($_FILES['ufile'][0]['tmp_name'], $path1);
			copy($_FILES['ufile'][1]['tmp_name'], $path2);
			copy($_FILES['ufile'][2]['tmp_name'], $path3);
			copy($_FILES['ufile'][3]['tmp_name'], $path4);
			copy($_FILES['ufile'][4]['tmp_name'], $path5);

			$filesize1=$_FILES['ufile'][0]['size'];
			$filesize2=$_FILES['ufile'][1]['size'];
			$filesize3=$_FILES['ufile'][2]['size'];
			$filesize4=$_FILES['ufile'][3]['size'];
			$filesize5=$_FILES['ufile'][4]['size'];

[u][b]				echo "<pre>";

			print_r($_FILES); [/b][/u]

			$insert = "INSERT INTO category_selection (cat_id, selection_name, item_name, item_description, item_price, main_image, image_1, image_2, image_3, image_4, date_uploaded) VALUES 
			(
			'".$_POST['thing']."', 
			'".$_POST['txt_c_name']."', 
			'".$_POST['txt_name']."', 
			'".$_POST['txt_desc']."', 
			'".$_POST['txt_price']."', 
			$path1,
			$path2,
			$path3,
			$path4,
			$path5,
			now()
			)";
			$add_member = mysql_query($insert);

			if($filesize1 && $filesize2 && $filesize3 && $filesize4 && $filesize5 != 0)
			{
			echo "We have recieved your files";
			}

			else {
			echo "ERROR.....";
			}

			if($filesize1==0) {
			echo "There're something error in your first file";
			echo "<BR />";
			}

			if($filesize2==0) {
			echo "There're something error in your second file";
			echo "<BR />";
			}

			if($filesize3==0) {
			echo "There're something error in your third file";
			echo "<BR />";
			}

			if($filesize4==0) {
			echo "There're something error in your fourth file";
			echo "<BR />";
			}

			if($filesize5==0) {
			echo "There're something error in your fifth file";
			echo "<BR />";
			}
		}

 

If you look at the underlined code, i tried to pull out what the array was returning, when i try to upload i get this message:-

 

Warning: copy() [function.copy]: Filename cannot be empty in /domains/skindeepapparel.com/http/lou/admin.php on line 148

Warning: copy() [function.copy]: Filename cannot be empty in /domains/skindeepapparel.com/http/lou/admin.php on line 149

Warning: copy() [function.copy]: Filename cannot be empty in /domains/skindeepapparel.com/http/lou/admin.php on line 150

Warning: copy() [function.copy]: Filename cannot be empty in /domains/skindeepapparel.com/http/lou/admin.php on line 151

Warning: copy() [function.copy]: Filename cannot be empty in /domains/skindeepapparel.com/http/lou/admin.php on line 152

Array
(
    [ufile] => Array
        (
            [name] => Array
                (
                    [0] => company_profile.jpg
                    [1] => company_profile.jpg
                    [2] => company_profile.jpg
                    [3] => company_profile.jpg
                    [4] => company_profile.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                    [3] => image/jpeg
                    [4] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpk1ayDp
                    [1] => /tmp/phpTwFEKs
                    [2] => /tmp/phpL2Hfmw
                    [3] => /tmp/phpRL2fEA
                    [4] => /tmp/phpI8ctvF
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                )

            [size] => Array
                (
                    [0] => 6504
                    [1] => 6504
                    [2] => 6504
                    [3] => 6504
                    [4] => 6504
                )

        )

)

 

Sorry for pasting loads of code, but i think it was the best way to explain what i am trying to acheive..

 

Hope someone can help.

 

Regards

 

 

Link to comment
https://forums.phpfreaks.com/topic/170867-multiple-image-upload/
Share on other sites

Hey,

 

I have managed to upload the file to the server but i need to add the value to the specific rows in the database. I have 5 image uploads.. Now i have the following code:-

 

			include("conn.php");

			$insert = "INSERT INTO category_selection (cat_id, selection_name, item_name, item_description, item_price, main_image, image_1, image_2, image_3, image_4, date_uploaded) VALUES 
			(
			'".$_POST['thing']."', 
			'".$_POST['txt_c_name']."', 
			'".$_POST['txt_name']."', 
			'".$_POST['txt_desc']."', 
			'".$_POST['txt_price']."', 
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			now()
			)";
			$add_member = mysql_query($insert);

			//places files into same dir as form resides
			foreach ($_FILES["ufile"]["error"] as $key => $error) 
			{
			   if ($error == UPLOAD_ERR_OK) 
			   {
			       echo"$error_codes[$error]";
			       move_uploaded_file(
			         $_FILES["ufile"]["tmp_name"][$key], 
			         'garments/'.$_FILES["ufile"]["name"][$key]
			       ) or die("Problems with upload");
			   }
			}

 

Take a look at my insert statement, i want it to add each image file, but how can i do this?

 

Regards

 

 

if (is_post() && !empty($_FILES)) {
    $files = $_FILES['userfiles'];
    if (is_array($files['tmp_name'])) {
        $sizeof = sizeof($files['tmp_name']);
        for ($i = 0; $i < $sizeof; ++$i) {
            $tmp_name = $files['tmp_name'][$i];
            $name = $files['name'][$i];
            //..
        }
    } else {
        $tmp_name = $files['tmp_name'];
        $name = $files['name'];
        //..
    }
}

function is_post() {
    return 'POST' === $_SERVER['REQUEST_METHOD'];
}

Hey,

 

Thanks for that, i have tried with the code in this way:-

 

	<?php
	if (isset($_POST['btn_update']))
	{
		if($_POST['txt_c_name'] == "" || $_POST['txt_name'] == "" || $_POST['txt_desc'] == "" || $_POST['txt_price'] == "")
		{
		echo "<p style='color:red;padding:4px'>Please fill in all the fields.</p>";
		}
		elseif($_POST['thing'] == "0")
		{
		echo "<p style='color:red;padding:4px'>Please select a category.</p>";
		}
		else
		{			
			if (is_post() && !empty($_FILES)) 
			{
			    $files = $_FILES['ufile'];
			    
			    if (is_array($files['tmp_name'])) 
			    {
			       $sizeof = sizeof($files['tmp_name']);
			        for ($i = 0; $i < $sizeof; ++$i) 
			        {
			            $tmp_name = $files['tmp_name'][$i];
			            $name = $files['name'][$i];
			        }
			    } 
			    else 
			    {
			        $tmp_name = $files['tmp_name'];
			        $name = $files['name'];
			    }
			}

			function is_post() {
			    return 'POST' === $_SERVER['REQUEST_METHOD'];
			}

			include("conn.php");

			$insert = "INSERT INTO category_selection (cat_id, selection_name, item_name, item_description, item_price, main_image, image_1, image_2, image_3, image_4, date_uploaded) VALUES 
			(
			'".$_POST['thing']."', 
			'".$_POST['txt_c_name']."', 
			'".$_POST['txt_name']."', 
			'".$_POST['txt_desc']."', 
			'".$_POST['txt_price']."', 
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			'".$_FILES['ufile']."',
			now()
			)";
			$add_member = mysql_query($insert);

			//places files into same dir as form resides
			foreach ($_FILES["ufile"]["error"] as $key => $error) 
			{
			   if ($error == UPLOAD_ERR_OK) 
			   {
			       echo"$error_codes[$error]";
			       move_uploaded_file(
			         $_FILES["ufile"]["tmp_name"][$key], 
			         'garments/'.$_FILES["ufile"]["name"][$key]
			       ) or die("Problems with upload");
			   }
			}
		}
	}
	?>

 

I presumed that where you suggested $files = $_FILES['userfiles']; you were refering to $_FILES['ufile']..?

 

Anyway i receive this error, now the insert AND the upload does not work..

 

Fatal error: Call to undefined function is_post() in /domains/skindeepapparel.com/http/lou/admin.php on line 140

 

Line 140 is the start of the code you suggested:-

 

if (is_post() && !empty($_FILES)) {...

 

Regards

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.