Looping through array in PHP to post several multipart form-data
- by Léon Pelletier
I'm trying in an asp web application to code a function that would loop through a list of files in a multiple upload form and send them one by one.
Is this something that can be done in ASP? Because I've read some posts about how to attach several files together, but saw nothing about looping through the files. I can easily imagine it in C# via HttpWebRequest or with socket, but in php, I guess there are already function designed to handle it?
// This is false/pseudo-code :)
for (int index = 0; index < number_of_files; index++)
{
postfile(file[index]);
}
And in each iteration, it should send a multipart form-data POST.
postfile(TheFileInfos) should make a POST like it:
POST /afs.aspx?fn=upload HTTP/1.1
[Header stuff]
Content-Type: multipart/form-data; boundary=----------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
[Header stuff]
------------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
Content-Disposition: form-data; name="Filename" myimage1.png
------------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
Content-Disposition: form-data; name="fileid"
58e21ede4ead43a5201206101806420000007667212251
------------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
Content-Disposition: form-data; name="Filedata"; filename="myimage1.png"
Content-Type: application/octet-stream
[Octet Stream]
[Edit]
I'll try it:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="processFiles.php">
<p>
<?
// start of dynamic form
$uploadNeed = $_POST['uploadNeed'];
for($x=0;$x<$uploadNeed;$x++){
?>
<input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>">
</p>
<?
// end of for loop
}
?>
<p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>