我有一个提交多个文件和标题的代码,我试图将结果组合起来上传到我的数据库,检查标题[]是否为空并打印自定义值,但标题[]有问题,我需要与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 1.jpg
2222 2.jpg
<?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";
}
}
}
}
?>
由于您没有指定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
- 你说你想要一个没有指定的默认标题,但没有解释你想要如何构建它。我在里面随便投了两个主意。第一个默认为上传文件的文件名。第二个只是一个随机字符串。你可以根据当前时间、上传者的IP等将其更改为其他内容。无论如何,你都会在那里这样做
看看你的代码,试试这个。。。
它可能有效,也可能无效,我自己还没有测试过。
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.
}
}