所以目前我的html中有以下代码<input type="file" required required="required" name="image" multiple="">
然后在我添加mutliple=""
标签之前,这总是对我有用,
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);
但是现在我正在尝试上传多个文件,我想我需要添加一个数组来命名它们,但我无法弄清楚,如果可以的话,我不想改变我的代码。
同样,目前 img 只是我数据库中的一个字段,这有多大问题?
编辑
我找到了这段代码,但似乎无法弄清楚如何实现它并使其工作......
在尝试了几十种应该修复 $_FILES 数组的不稳定的方法之后,我没有找到任何可以使用输入名称的方法,例如:userfile[christiaan][][][is][gaaf][]
所以我想出了这门课
<?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;
}
}
?>
这允许您访问使用以下输入类型上传的文件,
<input type="file" name="userfile[christiaan][][][is][gaaf][]" />
例如
<?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);
}
?>