我有这个代码提交多个文件和标题我试图将结果组合上传到我的数据库并检查title []是否为空并打印自定义值但我有标题问题[]我需要结合使用s_upload []数组:
<?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>
当我提交时,结果如下:
Array ( [0] => 11111 [1] => 22222 ) 1.jpgArray ( [0] => 11111 [1] => 22222 ) 2.jpg
如果标题存在,我需要这个结果:
1111 1.jpg
2222 2.jpg
如果title为空,则会产生此结果:
Title 1.jpg
2222 2.jpg
答案 0 :(得分:3)
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){
$i = 0;
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'][$i]==''){
echo 'Title '.$name;
}else{
echo $_POST['title'][$i] . ' ' . $name."\n";
}
}
$i++;
}
}
?>
这段代码不好,但我只是让它工作,请至少让它更具可读性。
PsyK编辑: 我更新了代码以删除$ i的需要,因为该数字已经存储在$ key中。 你所缺少的只是将标题引用为一个数组,就像你上传的文件一样。
<?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'][$key]==''){
echo 'Title '.$name;
}else{
echo $_POST['title'][$key] . ' ' . $name."\n";
}
}
}
}
?>
答案 1 :(得分:2)
由于您没有指定1111
的来源,我会给您一些选择:
<?
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){
foreach ($_FILES["s_upload"]["name"] as $key => $name) {
if ($_FILES["s_upload"]["error"][$key] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["s_upload"]["tmp_name"][$key];
if ($_POST['title'][$key]==''){
// No title was specified: construct default title
// This defaults the title to the filename of the file that was uploaded
$title = $name;
// This defaults the title to some random 32-character hex string
$title = md5(time()+rand());
}
else{
// A title was specified in the input box: use it
$title = $_POST['title'][$key];
}
echo "$title $name<br />";
}
}
}
?>
以下是我改变的内容:
[]
)命名输入,因此您必须在代码中指定您所引用的输入。这意味着您需要始终使用$key
。答案 2 :(得分:1)
只要看看你的代码就试试吧......
它可能有效,或者可能没有,我自己没有测试过它。
if (is_array($_FILES) && !empty($_FILES)) {
foreach ($_FILES['s_upload'] as $key => $file) {
if ($file['error']!=UPLOAD_ERR_OK)
continue;
$tmp_name = $file['tmp_name'];
$name = $file['name'];
if (isset($_POST['title'][$key])) { # Standard method.
$title = $_POST['title'][$key];
} else {
$title = "Default title";
}
// Do what you need to do with the stuff.
}
}