简体   繁体   English

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

[英]PHP multi upload files with custom title

I'm having a problem with my MySQL insert. 我的MySQL插入出现问题。 When I upload a file, data like "filename", "time", and "gid" are inserted correctly. 当我上传文件时,将正确插入“文件名”,“时间”和“ gid”之类的数据。 But "title" is empty. 但是“标题”为空。

Why is the "title" not being inserted? 为什么未插入“标题”?

<?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.
    }
}

}
}

Just as your script handles multiple uploaded images, each image has a corresponding title input. 正如您的脚本处理多个上传的图像一样,每个图像都有相应的标题输入。

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

So, the "title" data will be posted as an array: 因此,“ title”数据将作为数组发布:

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

You'll need to handle the array of titles appropriately, by using a key that corresponds to the array of uploaded images to reference each associated title. 您需要使用对应于上载图像数组的键来适当地处理标题数组,以引用每个相关标题。 From the looks of your code, you may be able to use the $key variable that is defined in your while loop, like so: 从代码的外观来看,您可以使用在while循环中定义的$key变量,如下所示:

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

Note: There also seem to be a couple of extra closing braces } in your PHP code. 注意:您的PHP代码中似乎还有两个额外的右花括号}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM