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.
$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;
}
This
$_FILES['file']['name']
is not a string. You have to loop this to get the value you need, to be able to append it to rand(1000,100000)."-".
For $file_size
it stands the same.
I suggest you loop it like this:
foreach($_FILES['file']['name'] as $k => $item) {
$fileInfo[$k]['file'] = rand(1000,100000)."-".$item['name'][$k];
$fileInfo[$k]['file_loc'] = $item['tmp_name'];
$fileInfo[$k]['file_size'] = $item['size'];
$fileInfo[$k]['file_type'] = $item['type'];
$fileInfo[$k]['folder']= "../admin/gambar/";
$fileInfo[$k]['new_size'] = $item['size']/1024;
}
Example: Access your third file type like this:
$fileType = $fileInfo[2]['filetype'];