Junior PHP Junior PHP - 3 months ago 9
MySQL Question

PHP Notice : Array to string conversion in and Unsupported operand types in

I cannot seem to figure this out. I wrote a PHP script and I keep getting a Array to string conversion in...on line 7 and Unsupported operand types in...on line 14 Notice as an error when trying to upload multiple files.

HTML code :

<form class="form-horizontal" action="uploadgambar.php" method="post" enctype="multipart/form-data" multiple>

<div class="form-group form-group-md">
<label class="col-sm-2 control-label" for="sm">Username</label>
<div class="col-sm-4">
<input class="form-control" type="text" name="username" id="sm" value="<?php echo $_SESSION['username']; ?>" readonly>
</div>
</div>

<div class="form-group form-group-md">
<label class="col-sm-2 control-label" for="sm">Nama Gambar/Foto</label>
<div class="col-sm-4">
<input class="form-control" type="text" name="nama" id="sm" placeholder="Nama Gambar/Foto" required>
</div>
</div>

<div class="form-group form-group-md">
<label class="col-sm-2 control-label" for="sm">File</label>
<div class="col-sm-4">
<input class="form-control" type="file" name="file[]" id="sm" placeholder="Keterangan" multiple="multiple" required>
</div>
</div>




PHP code :

$nama = $_POST['nama'];
$username = $_POST['username'];
$file = rand(1000,100000)."-".$_FILES['file']['name']; <- Line 7
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="../admin/gambar/";
$new_size = $file_size/1024; <- Line 14


Thanks.

Answer
$nama = $_POST['nama'];
$username = $_POST['username'];
foreach ($_FILES['file']['name'] as $name){

   $file = rand(1000,100000)."-".$name;  
   $file_loc = $_FILES['file']['tmp_name'];
   $file_size = $_FILES['file']['size'];
   $file_type = $_FILES['file']['type'];
   $folder="../admin/gambar/";
  $new_size = $file_size/1024; 

}