0

Hi everyone, I am new to PHP, and I am trying to upload 10 files with differnt input names. From reading forums and such, I came up with the following code, but it doesn't seem to be working. Can anyone tell me where I went wrong?

if (isset($_POST[submit])) { 

$uploadArray= array();
$uploadArray[] = $_POST['image01'];
$uploadArray[] = $_POST['image02'];
$uploadArray[] = $_POST['image03'];
$uploadArray[] = $_POST['image04']; 
$uploadArray[] = $_POST['image05']; 
$uploadArray[] = $_POST['image06']; 
$uploadArray[] = $_POST['image07']; 
$uploadArray[] = $_POST['image08']; 
$uploadArray[] = $_POST['image09']; 
$uploadArray[] = $_POST['image10']; 

foreach($uploadArray as $file) {  

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


if (
(($_FILES[$file]["type"] == "image/gif")
|| ($_FILES[$file]["type"] == "image/jpg")
|| ($_FILES[$file]["type"] == "image/jpeg")
|| ($_FILES[$file]["type"] == "image/pjpeg"))
&& ($_FILES[$file]["size"] < 5000000)

)
{ 

if(move_uploaded_file($_FILES[$file]['tmp_name'], $target)) 
	{   
		
		// redirect to upload success url
    	header('Location: ' . SUCCESS_URL);
    	die();
	
    	break;
	
	}	
	else 
		{
		
		echo "Sorry, there was a problem uploading your file.";
		}

}
else
  {
  echo "Invalid file";
  }

} 
}

Edited by olunde: n/a

4
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by network18
0

Use array subscripted identifier for name attribute.

<body>
Select file <input name="ufile[]" type="file" />
Select file <input name="ufile[]" type="file" />
Select file <input name="ufile[]" type="file" />
Select file <input name="ufile[]" type="file" />
</body>
....

Take a look at - http://www.phpeasystep.com/workshopview.php?id=2

2

Do what adatapost says above and use your improved code below -

<?
if (isset($_POST[submit]))
{ 

	foreach($uploadArray as $file)
	{  
		//This is the directory where images will be saved
		$target = "orders/";
		$target = $target . basename( $_FILES[$file]['name'] );
		
		$arr_allowed_types = array("image/gif","image/jpg","image/jpeg","image/pjpeg");
		
		if(	in_array($_FILES[$file]["type"],$$arr_allowed_types) && $_FILES[$file]["size"] < 5000000)
		{ 
			if(move_uploaded_file($_FILES[$file]['tmp_name'], $target)) 
			{   
				// redirect to upload success url
				header('Location: ' . SUCCESS_URL);
			}	
			else 
			{
				echo "Sorry, there was a problem uploading your file.";
			}
		
		}
		else
		{
		  echo "Invalid file";
		}
	
	} 
}
?>
Votes + Comments
Great!
0

Hey.

I just wanted to point out a few minor things in network18's code:

  • Short tags are baaad! ;-)
  • Where does the $uploadArray variable for the foreach loop come from? Wouldn't it be better to just use $_FILES ?
  • Line #2. $_POST[submit] . The 'submit' should be quoted, right?
  • Line #13. You doubled up on the $ chars for the $arr_allowed_types variable.
  • O, and you might want to pull the $arr_allowed_types array creation out of the foreach loop. No point re-creating it each loop.

Otherwise I like it! Very dynamic. (I love dynamic code!) :)

0

Hey.

I just wanted to point out a few minor things in network18's code:

  • Short tags are baaad! ;-)
  • Where does the $uploadArray variable for the foreach loop come from? Wouldn't it be better to just use $_FILES ?
  • Line #2. $_POST[submit] . The 'submit' should be quoted, right?
  • Line #13. You doubled up on the $ chars for the $arr_allowed_types variable.
  • O, and you might want to pull the $arr_allowed_types array creation out of the foreach loop. No point re-creating it each loop.

Otherwise I like it! Very dynamic. (I love dynamic code!) :)

Sorry for the inconvinience if any caused due to it, and thanks for the quick notes in the code.
Below are the changes -

if (isset($_POST['submit']))
{ 
	//$uploadArray[] is the name of the file input element
	//This code is there in adapost's post above my previous post and I assumed it will be noted by just mentioning it
	foreach($uploadArray as $file)
	{  
		//This is the directory where images will be saved
		$target = "orders/";
		$target = $target . basename( $_FILES[$file]['name'] );
		
		$arr_allowed_types = array("image/gif","image/jpg","image/jpeg","image/pjpeg");
		
		if(	in_array($_FILES[$file]["type"],$arr_allowed_types) && $_FILES[$file]["size"] < 5000000)
		{ 
			if(move_uploaded_file($_FILES[$file]['tmp_name'], $target)) 
			{   
				// redirect to upload success url
				header('Location: ' . SUCCESS_URL);
			}	
			else 
			{
				echo "Sorry, there was a problem uploading your file.";
			}
		
		}
		else
		{
		  echo "Invalid file";
		}
	
	} 
}

At the same moment would like to comment few important things.
- using short tags is totally programmar's own choice, it just need the small setting in the ini to get it working.Personally i just love using short tags, they are very handy:)
- For "$uploadArray" see the comment in the code, like i said its a name of the file inout element.
any more improvements to get it working in the best possible way:)

0

- For "$uploadArray" see the comment in the code, like i said its a name of the file inout element.

Ahh ok, sorry. I see now where your coming from on that one.
Although I think you may have confused posts. I only see it in the OPs post. (Maybe I'm missing something. Caffeine hasn't quite kicked in :-P)

- using short tags is totally programmar's own choice, it just need the small setting in the ini to get it working.Personally i just love using short tags, they are very handy:)

True, it is very simple for those of us that actually know how, to turn them on.
However, they are disabled by default, and it is extremely confusing for people that don't know about this to suddenly see their code in the markup.

Using short_tags does make your code a lot less portable, and by that argument alone they should be discouraged.
Especially in situations where you know your code is going to be used by other people, who may not even be aware of the difference. (Like say: when posted in a public forum ;-)

And all this just to save a few keystrokes :)

Not trying to sound argumentative or anything.
I just had this teacher a few years back that taught us to use the short tags. I wasted hours trying to figure out why the code that worked perfectly on the school computers didn't work on my laptop.
I could have done without the added stress :)

0

Not trying to sound argumentative or anything.
I just had this teacher a few years back that taught us to use the short tags. I wasted hours trying to figure out why the code that worked perfectly on the school computers didn't work on my laptop.
I could have done without the added stress

Agree on it:)
@olunde: what is the status now..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.