So at the moment I have the following code in my html <input type="file" required required="required" name="image" multiple="">
and then before I added the mutliple=""
tag, this always worked for me,
if(isset($_POST['Submit']))
{
$current_image=$_FILES['image']['name'];
$extension = substr(strrchr($current_image, '.'), 1);
if (($extension!= "png") && ($extension != "jpg"))
{
die('Unknown extension');
}
$time = date("fYhis");
$new_image = $time . "." . $extension;
$destination="./../img/treatments/".$new_image;
$action = copy($_FILES['image']['tmp_name'], $destination);
but now that i am trying to upload multiple files I think I need to add an array to name them but I cannot figure it out, I don't want to change my code very much if I can.
Also at the moment the img is only one field on my database, how much of an issue is this?
EDIT
I found this code but cant seem to figure out how to implement it and make it work...
After trying dozens of ways that are supposed to fix the wonkyness of the $_FILES array I didn't find any that could work with a input name like: userfile[christiaan][][][is][gaaf][]
So I came up with this class
<?php
/**
* A class that takes the pain out of the $_FILES array
* @author Christiaan Baartse <christiaan@baartse.nl>
*/
class UploadedFiles extends ArrayObject
{
public function current() {
return $this->_normalize(parent::current());
}
public function offsetGet($offset) {
return $this->_normalize(parent::offsetGet($offset));
}
protected function _normalize($entry) {
if(isset($entry['name']) && is_array($entry['name'])) {
$files = array();
foreach($entry['name'] as $k => $name) {
$files[$k] = array(
'name' => $name,
'tmp_name' => $entry['tmp_name'][$k],
'size' => $entry['size'][$k],
'type' => $entry['type'][$k],
'error' => $entry['error'][$k]
);
}
return new self($files);
}
return $entry;
}
}
?>
This allows you to access a file uploaded using the following inputtype
<input type="file" name="userfile[christiaan][][][is][gaaf][]" />
like
<?php
$files = new UploadedFiles($_FILES);
var_dump($files['userfile']['christiaan'][0][0]['is']['gaaf'][0]);
// or
foreach($files['userfile']['christiaan'][0][0]['is']['gaaf'] as $file) {
var_dump($file);
}
?>