简体   繁体   中英

php Output array values ​in the database

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

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."',..... 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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