i have this code that submits multiple files and titles im trying to combine the results to upload to my DB and check if title[] is empty and print a custom value but im having problems with the title[] i need to combine with the s_upload[] array:
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){
foreach ($_FILES["s_upload"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["s_upload"]["tmp_name"][$key];
$name = $_FILES["s_upload"]["name"][$key];
// move_uploaded_file($tmp_name, "data/$name");
if ($_POST['title']==''){
echo 'Title';
}else{
print_r ($_POST['title']);
echo $name;
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<div class='file_upload' id='f1'>
<input type="text" name="title[]" id="t1">
<input size="14" name='s_upload[]' id="i1" type='file'/>
</div>
<div class='file_upload' id='f2'>
<input type="text" name="title[]" id="t2">
<input size="14" name='s_upload[]' id="i2" type='file'/>
</div>
<input type="submit"/>
</form>
</body>
</html>
when i submit this is are the results:
Array ( [0] => 11111 [1] => 22222 ) 1.jpgArray ( [0] => 11111 [1] => 22222 ) 2.jpg
i need this results if title exists:
1111 1.jpg
2222 2.jpg
and this results if title is empty:
Title 1.jpg
2222 2.jpg
Since you didn’t specify where the
1111came from, I’ll give you a few options:Here’s what I changed:
[]) in the input name, you have to specify which one you’re referring to in your code. This means you need to use$keyconsistently throughout.