Php 使用foreach组合并上载多个输入
我有一个提交多个文件和标题的代码,我正在尝试合并结果以上载到我的数据库,并检查标题[]是否为空并打印自定义值,但我对标题[]有问题,我需要与s_upload[]数组合并:Php 使用foreach组合并上载多个输入,php,Php,我有一个提交多个文件和标题的代码,我正在尝试合并结果以上载到我的数据库,并检查标题[]是否为空并打印自定义值,但我对标题[]有问题,我需要与s_upload[]数组合并: <?php if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){ foreach ($_FILES["s_upload"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp
<?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>
如果标题存在,我需要此结果:
1111 1.jpg
2222 2.jpg
如果标题为空,则会出现以下结果:
Title 1.jpg
2222 2.jpg
这个代码很糟糕,但我只是让它工作,请至少让它更可读
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";
}
}
}
}
?>
只要看看你的代码,试试这个 它可能有效,也可能无效,我自己还没有测试过
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.
}
}
由于您没有指定
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
不打算告诉OP你改变或修正了什么,也不打算解释你为什么做这样的改变?嗯。。修复了OP代码中最小注入量的问题。如果他问我,我会解释每一步。你仍然应该习惯于告诉人们他们应该学习什么。在他们面前扔代码永远不会教他们任何东西。我不会和你争论,但首先-这是我主要学习的方式,其次-好的,我会尝试更频繁地练习(看看我的答案-它们并不总是那么糟糕:)tx对于你的代码很有帮助,是的,我每天都在学习,很抱歉,如果代码不是那么可读,但你们得到了tx的想法!
1111和2222基于什么?文件名?在之前缺少一个括号,否则
无法告诉您这样的事情耽误了我自己的项目多少次。
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.
}
}
<?
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 />";
}
}
}
?>