duanlv1366 2019-08-09 05:12
浏览 189
已采纳

php在数据库中输出数组值

I entered the image files into the database through the input form. The database contains elements of name, hash, and time. When I upload multiple files, only 'Array' is outputted to the name. I have to show the images on the view page. I want to store elements of the array separately when storing them in a database, or to output elements of the array separately when outputting them. Please help me

upfile.php :

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input name="upload[]" type="file" multiple="multiple" />
    <input type="submit">
</form>

upload.php :

if(!$_FILES['upload']['name'])
{
    echo "<script>alert('Emtpty');";
    echo "history.back();</script>";
    exit;
}

if(strlen($_FILES['upload']['name']) > 255)
{
    echo "<script>alert('Too long');";
    echo "history.back();</script>";
    exit;
}

$date = date("YmdHis", time());
$dir = "./dir";
$file_hash = $date.$_FILES['upload']['name'];
$file_hash = md5($file_hash);
$upfile = $dir.$file_hash;

$total = count($_FILES['upload']['name']);

for($i=0; $i<$total; $i++) { 
    if(is_uploaded_file($_FILES['upload']['tmp_name'])) {
        if(!move_uploaded_file($_FILES['upload']['tmp_name'], $upfile))
        {
                echo "upload error";
                exit;
        }
    }
}

@ $db = new mysqli('localhost', 'DBid', 'Password', 'DBname');
if(mysqli_connect_errno())
{
    echo "DB error";
    exit;
}

$query = "insert into ftp (name, hash, time) 
          values('".$_FILES['upload']['name']."', 
          '".$file_hash."', '".$date."')";
$result = $db->query($query);
if(!$result)
{
    echo "DB upload error";
    exit;
}

$db->close();

echo "<script>alert('Upload');";
echo("location.href='./view.php';</script>");

If output the name, nothing is generated.

name | hash | time

Array | 6933b06c625e6900e1570298e0e2baf8 | 2019-08-09 01:35:47

Array | cecef12bb58ef31885bd1a0625909269 | 2019-08-09 02:28:51

  • 写回答

1条回答 默认 最新

  • duanpo1498 2019-08-09 05:17
    关注

    Hey that is because when uploading multiple files, the $_FILES variable become as array:

    Array
    (
        [name] => Array
            (
                [0] => foo.txt
                [1] => bar.txt
            )
    
        [type] => Array
            (
                [0] => text/plain
                [1] => text/plain
            )
    
        [tmp_name] => Array
            (
                [0] => /tmp/phpYzdqkD
                [1] => /tmp/phpeEwEWG
            )
    
        [error] => Array
            (
                [0] => 0
                [1] => 0
            )
    
        [size] => Array
            (
                [0] => 123
                [1] => 456
            )
    )
    

    and you are inserting as string and you should do a loop first.

    foreach($_FILES['upload']['name'] as $file){
        $query = "insert into ftp (name, hash, time) values('".file."',..... 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!