带有自定义标题的PHP多上传文件

时间:2015-08-05 15:35:26

标签: php mysql

我的MySQL插件有问题。当我上传文件时,正确插入“filename”,“time”和“gid”等数据。但“标题”是空的。

为什么没有插入“标题”?

<?php

$max_no_img = 10; // Maximum number of images value to be set here
echo "<form method=post action='' enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";

for ($i = 1; $i <= $max_no_img; $i++) {
    echo " <tr><td><input type='text' name='title[]'></td><td><input type=file name='images[]' class='bginput'></td></tr>";
}

echo "<input type='hidden' name='gid' value='$info[id]'>";
echo "</table>";
echo "<br /> <center><input type=submit value='Dodaj sliku'></center>";
echo "</form>";

while (list($key, $value) = each($_FILES['images']['name'])) {

    // echo $key;
    // echo "<br />";
    // echo $value;
    // echo "<br />";

    if (!empty($value)) { // this will check if any blank field is entered
        $time = time();
        $gid = addslashes($_POST['gid']);
        $title = $_POST['title'];
        $filename = rand(1, 100000) . $value; // filename stores the value
        $filename = str_replace(" ", "_", $filename); // Add _ inplace of blank space in file name, you can remove this line
        $add = "slike/$filename"; // upload directory path is set

        // echo $_FILES['images']['type'][$key];     // uncomment this line if you want to display the file type
        // echo "<br />";                             // Display a line break

        copy($_FILES['images']['tmp_name'][$key], $add);
        mysql_query("INSERT INTO slike (title,slika,time,gid) VALUES ('$title','$filename','$time','$gid')") or die(mysql_error());
        echo "Uspesno pritisnite <a href='/admin/?page=gall&id=$info[id]'>ovde</a>";

        //  upload the file to the server

        chmod("$add", 0777); // set permission to the file.
    }
}

}
}

1 个答案:

答案 0 :(得分:1)

正如您的脚本处理多个上传的图像一样,每个图像都有相应的标题输入。

<input type='text' name='title[]'>

因此,“标题”数据将作为数组发布:

Array (
  [0] => title1
  [1] => title2
  [2] => title3
  [3] => title4
)

您需要通过使用与上传图像数组相对应的键来适当地处理标题数组,以引用每个相关标题。从代码的外观来看,您可以使用$key循环中定义的while变量,如下所示:

$title = $_POST['title'][$key];

注意:PHP代码中似乎还有一些额外的结束括号}