Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve unknown $_post variables

Status
Not open for further replies.

Kurt111780

Technical User
Nov 20, 2003
235
GB
Hello,

I need to retrieve file names from a form submission but I don't know how many fields are submitted. Each field has a name of file1, file2, file3, etc. I'm trying to do something like this but it’s not working.
Code:
$i = 1;
while ($_POST['file' . $i] != "")
	{
		echo $i;
		$i++;
	}//end while

Then I have to put the file names into an array. Is there a way to use a variable within $POST or is there a different way to do this?

Thanks,
Kurt


It's only easy when you know how.
 
$_POST['file' . $i] is perfectly legal.

Another way to do this is to change the way your fields are named. Instead of naming your form fields "file1", "file2", etc., name them "file[1]", "file[2]", etc. Thatt way, $_POST['file'] will itself be an array.

You've said, "retrieve file names from a form submission". Are these form inputs of type "file"? If so, you need to be looking in $_FILES, not $_POST.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I just need to retrieve the name of the files which are already stored on the server. But I just realized why its not working. I had this on the previous page for the user to select files (file names) but it obviously won't work.

Code:
<?php
$folder	=  $_GET['folder'];
$dir 	= "D:/inetpub/promessinc/php/" . $folder . "/";
$path	= "files/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
   if ($dh = opendir($dir)) 
   {	$i = 0;
       while (($file = readdir($dh)) !== false) 
	   {
			if($file != "." && $file != "..")
			{
		   		?>
  				<tr>
				<td><input type="checkbox" value="<?= $file ?>" name="<?= "file" . $i ?>" /></td>
				<td><?= $file ?></td>
  				</tr> 
<?php			$i++;
 			}//end if . ..
 		}//end while
		closedir($dh);
	}//end if $dh
}//end if is_dir
?>

It's only easy when you know how.
 
Hi sleipnir214,

I did what you said and its works good. easier than I thought it would be.

changed this line
Code:
				<td><input type="checkbox" value="<?= $file ?>" name="file[]" /></td>

And added this on the next page
Code:
$fileName = $_POST['file'];
	foreach ($fileName as $value)
	{
		echo "File Name: " . $value . "<br />";
	}

Now I just gota loop through each one adding it to the DB with the random numbers.

Thanks,
Kurt

It's only easy when you know how.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top