问题
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);
}
?>
回答1:
I found this for you
http://php.net/manual/en/features.file-upload.multiple.php
回答2:
Try this (your problem seems in fact to be the totally missing understanding of how this all works in php, instead of some specific problem/bug/...):
whatever.php:
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>">
First file: <input type="file" name="uploads[]" /><br>
Second File: <input type="file" name="uploads[]" /><br>
Multiple files at once (firefox, chrome, ... not IE < 10): <input type="file" name="uploads_multiple[]" multiple="multiple" /><br>
<input type="submit" value="Go" />
</form>
<?php
print_r($_FILES);
Try this with different inputs (filesizes, missing files, ...) to see what you get from that in php. I think you'll benefit from this way more than if posted some finished code.
Edit: Also, DON'T use copy for this! Most of the time it just won't work, sometimes it might give unexpected results, and sometimes you'll just be opening a can of worms. There is a reason the function move_uploaded_file exists - USE IT!
来源:https://stackoverflow.com/questions/8088735/upload-multiple-files-to-server-and-write-to-database