I am looking to create a loop for uploading several documents (49 to be exact!!!). I am completely new to loops so any direction or help would be great. Here is my current code:
$folder="uploads/";
if(isset($_FILES["t1url"])) {
$t1urlextension = end(explode(".", $_FILES["t1url"]["name"]));
$t1urlname = "M1s1t1url";
$t1urldoc_loc = $_FILES['t1url']['tmp_name'];
move_uploaded_file($t1urldoc_loc,$folder.$t1urlname.".".$t1urlextension);
}
if(isset($_FILES["t2url"])) {
$t2urlextension = end(explode(".", $_FILES["t2url"]["name"]));
$t2urlname = "M1s1t2url";
$t2urldoc_loc = $_FILES['t2url']['tmp_name'];
move_uploaded_file($t2urldoc_loc,$folder.$t2urlname.".".$t2urlextension);
}
I have this working for these 2 but the thought of changing this over and over again 49 times seems brutal.
Essentially there are 10 locations that would need to increase by one
if(isset($_FILES["t1url"])) {
"t1url" to "t2url" to "t3url" etc.
$t1urlextension =
"$t1urlextension" to "$t2urlextension" to "$t3urlextension" to etc.
end(explode(".", $_FILES["t1url"]["name"]));
"t1url" to "t2url" to "t3url" to etc.
$t1urlname
"$t1urlname" to "$t2urlname" to "$t3urlname" to etc.
"M1s1t1url"
"M1s1t1url" to "M1s1t2url" to "M1s1t3url" to etc.
$t1urldoc_loc
"$t1urldoc_loc" to "$t2urldoc_loc" to "$t3urldoc_loc" to etc.
$_FILES['t1url']['tmp_name'];
"t1url" to "t2url" to "t3url" to etc.
move_uploaded_file($t1urldoc_loc,$folder.$t1urlname.".".$t1urlextension);
"$t1urldoc_loc" to "$t2urldoc_loc" to "$t3urldoc_loc" to etc.
"$t1urlname" to "$t2urlname" to "$t3urlname" to etc.
"$t1urlextension" to "$t2urlextension" to "$t3urlextension" to etc.
Thanks for the help in advance!
UPDATE
Here is the solution I was able to come up with based on JTC's answer:
$url = array("t1url","t2url");
$extension = array("t1ext","t2ext");
// $name = array("t1name","t2name");
$tab = array("M1s1t1url","M1s1t2url");
$loc = array("t1loc","t2loc");
$folder="Uploads/";
$arrlength = count($url);
for($x = 0; $x < $arrlength; $x++) {
if(isset($_FILES[$url[$x]])) {
$extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
// $name[$x] = $tab[$x];
$loc[$x] = $_FILES[$url[$x]]['tmp_name'];
move_uploaded_file($loc[$x],$folder.$tab[$x].".".$extension[$x]);
}
}
Works perfectly!!! Thanks!
UPDATE
I spoke to soon! I can get this to work for 4 but anything over 4 will not work. Here is the code that works:
$url = array(
"t1url",
"t2url",
"t3url",
"t4url");
$extension = array(
"t1ext",
"t2ext",
"t3ext",
"t4ext");
$tab = array(
"M1s1t1url",
"M1s1t2url",
"M1s1t3url",
"M1s1t4url");
$loc = array(
"t1loc",
"t2loc",
"t3loc",
"t4loc");
$folder="Uploads/";
$arrlength = count($url);
for($x = 0; $x < $arrlength; $x++) {
if(isset($_FILES[$url[$x]])) {
$extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
$loc[$x] = $_FILES[$url[$x]]['tmp_name'];
move_uploaded_file($loc[$x],$folder.$tab[$x].".".$extension[$x]);
}
}
This for some reason does not work:
$url = array(
"t1url",
"t2url",
"t3url",
"t4url",
"t5url");
$extension = array(
"t1ext",
"t2ext",
"t3ext",
"t4ext",
"t5ext");
$tab = array(
"M1s1t1url",
"M1s1t2url",
"M1s1t3url",
"M1s1t4url",
"M1s1t5url");
$loc = array(
"t1loc",
"t2loc",
"t3loc",
"t4loc",
"t5loc");
$folder="Uploads/";
$arrlength = count($url);
for($x = 0; $x < $arrlength; $x++) {
if(isset($_FILES[$url[$x]])) {
$extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
$loc[$x] = $_FILES[$url[$x]]['tmp_name'];
move_uploaded_file($loc[$x],$folder.$tab[$x].".".$extension[$x]);
}
}
Here is the error message I am receiving in the code that does not work:
I get this 5 times:
Strict standards: Only variables should be passed by reference in C:\wamp2\www\phpKiosk\mc1.php on line 535
Here is line 535:
$extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
I get this error once:
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp2\www\phpKiosk\mc1.php:535) in C:\wamp2\www\phpKiosk\mc1.php on line 773
Here is line 773:
header("location: http://localhost/phpkiosk/mc1.php");
Any help would be awesome. I am really new to this and I have to be overlooking something simple.