我正在尝试使用foreach
处理多个文件上传,然后使用随机字符串重命名文件并将文件名存储在一个数组中,这是我当前的代码:
// this variable stores the id of last inserted row in MySQLi DB
$last_shipment_id = mysqli_insert_id($con);
// Array of valid file formats
$valid_formats = array("jpg", "jpeg", "png");
// the upload path
$path = "../uploads/"; // Upload directory
// count variable for foreach counting
$count = 0;
// variable for generated file names to use them later
$new_file_name = array();
foreach ($_FILES['files']['name'] as $f => $name) {
$ext = pathinfo($name, PATHINFO_EXTENSION);
$new_file_name[] = randomNumber(14)."-".$last_shipment_id.'.'.$ext;
if ($_FILES['files']['error'][$f] == 4) {
continue;
}
if ($_FILES['files']['error'][$f] == 0) {
if( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue;
} else {
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$new_file_name)) {
}
}
}
}
我无法找到问题所在,我应该对每个生成的文件名使用foreach
,然后在move_uploaded_file
内使用foreach
吗?
答案 0 :(得分:1)
你完全错了。您已在 <li>
<custom-tooltip id="main-search-tt" placement="'right'" icon-class="'icon-question-circle'">
<a ui-sref="pages">Pages</a>
</custom-tooltip>
</li>
语句中初始化$_FILES['files']['name']
,并尝试在每次迭代中访问foreach
和$_FILES['files']['error']
。由于这是一个数组,所以不可能。
所以解决方法如下,
$_FILES["files"]["tmp_name"]
希望这可以帮到你。