我制作了一个模板,在其中我选择了多个文件,我创建了php页面,在其中我上传了文件,但是当我上传这些文件时,它给了我错误,比如
Warning: pathinfo() expects parameter 1 to be string, array given in C:\xampp\htdocs\jobboard\system\user-scripts\classifieds\apply_now.php on line 67这是我的代码:
<input type="file" name="file_tmp[]" multiple />这是我的apply_now.php:
if (!empty($_FILES['file_tmp']['name'])){
$fileFormats = explode(',',SJB_System::getSettingByName('file_valid_types'));
foreach ( $_FILES['file_tmp']['name'] as $file ) {
$fileInfo = pathinfo($file);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}
}
}发布于 2014-03-12 15:30:31
错误是由这样一个事实引起的:您将数组作为参数,而不是字符串,正如错误消息告诉您的那样。
可以通过将foreach代码更改为以下代码来解决这一问题:
foreach ( $_FILES['file_tmp']['name'] as $key => $file ) {
$fileInfo = pathinfo($_FILES['file_tmp']['name'][$key]);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}
}还请参阅我在回答您上一个问题时的代码:https://stackoverflow.com/a/22355746/2539335
发布于 2014-03-12 12:22:11
如果您上传了许多文件,那么$_ files‘FILES_tmp’将是一个数组。你应该做个前程循环
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}在您的代码中,替换:
$fileInfo = pathinfo($_FILES['file_tmp']['name']);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}通过以下方式:
foreach ( $_FILES['file_tmp']['name'] as $file ) {
$fileInfo = pathinfo($file);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}
}https://stackoverflow.com/questions/22350876
复制相似问题