Hi All,

Need help in uploading multiple files and renaming them. I want the file to rename to corrensponding text file while uploading. Below is the messy code on which I'm working.

Regards
BW

HTML code

<html>

<head>
<title>Multi</title>
</head>


<body bgcolor="#c2c2c2">
<form method=post action=upload.php enctype='multipart/form-data'><table border='1' width='400' cellspacing='0' cellpadding='0' align=center><tr>
  <td><input name="cast[]" type="text"></td><td>
	<input type=file name='images[]' class='bginput'>
	<br></td></tr><tr><td><input name="cast[]" type="text"></td><td>
	<input type=file name='images[]' class='bginput'>
	<br></td></tr><tr><td><input name="cast[]" type="text"></td><td>
	<input type=file name='images[]' class='bginput'>
	<br></td></tr><tr><td><input name="cast[]" type="text"></td><td>
	<input type=file name='images[]' class='bginput'>
	<br></td></tr><tr><td><input name="cast[]" type="text"></td><td>
	<input type=file name='images[]' class='bginput'>
<br></td></tr><tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr></form> </table>
</body>

</html

Handling file

<?php
$cast = $_POST['cast'];	
foreach($cast as $casts){
		//echo $casts. "<br />";	
	}


//Variable for file
	$name = $_FILES["images"]["name"];
	$type = $_FILES["images"]["type"];
	$size = $_FILES["images"]["size"];
	$tmp_name = $_FILES["images"]["tmp_name"];
	$error = $_FILES["images"]["error"];
	//$filename1 = $cast .".jpeg";
	//$path = "upimg/" . $filename1;
	
while(list($key,$value) = each($name))
		{
			if(!empty($value))
			{
				$filename = $value;
					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line

					$add = "upimg/$filename";
                       //echo $_FILES['images']['type'][$key];
			     // echo "<br>";
					copy($tmp_name[$key], $add);
					//echo $filename. "<br />";
					echo $cast . $filename . "<br />";
					
			}
		}
?>

You have minor errors in your html code (you start form and then table but you close first the form and then the table) . Also the use of while(list($key,$value) = each($name)) is really not clean code since you could do the same with a simple for($i=0;$i<count($name); $i++) , in that way you could check at same time if $cast[$i] is “” or has value.

But the real problem isn’t all that is that you use copy instead of move_uploaded_file . In case a file is uploaded via PHP's HTTP POST upload mechanism then the move_uploaded_file is the way to go (as PHP manual states). The reason for that has to do with security reasons (accessing the server’s tmp directory).