0
我很難搞清楚這一點。如何將來自不同輸入的不同圖像放入不同的文件目錄中。獲取多輸入PHP的輸入名稱
例如我想要「$ _FILES ['ma'];」去銷售/ ma/xxxxxxx.jpg和「$ _FILES ['ny'];」去銷售/ ny/xxxxxxx.jpg,我怎麼做到這一點。
非常感謝你的任何幫助。
這是我非常簡單的HTML
<form action="includes/upload.php" method="post" enctype="multipart/form-data">
<label for="ma">ma</label>
<input type="file" name="ma" id="ma" multiple="">
<label for="ma">ny</label>
<input type="file" name="ny" id="ny" multiple="">
<label for="ma">nj</label>
<input type="file" name="nj" id="nj" multiple="">
<label for="va">va</label>
<input type="file" name="va" id="va" multiple="">
<input type="submit" value="Upload Image" name="submit">
</form>
,這裏是PHP。
<?php
$ma_file = $_FILES['ma'];
$ny_file = $_FILES['ny'];
$nj_file = $_FILES['nj'];
$va_file = $_FILES['va'];
$all_files = array($ma_file,$ny_file,$nj_file,$va_file);
foreach($all_files as $file) {
try {
if (
!isset($file['error']) ||
is_array($file['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
// Check $_FILES['upfile']['error'] value.
switch ($file['error']) {
case "0":
break;
case UPLOAD_ERR_NO_FILE:
// throw new RuntimeException('No file sent.');
// it's ok to have no file.
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
if ($file['size'] > 1000000) { // under one mb.
throw new RuntimeException('Exceeded filesize limit.');
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($file['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
if (!move_uploaded_file(
$file['tmp_name'],
sprintf('../sale/%s.%s',
sha1_file($file['tmp_name']),$ext
)
)) {
throw new RuntimeException('Failed to move uploaded file.');
}
}
catch (RuntimeException $e) {
echo $e->getMessage();
}
}
?>
謝謝,這是我第一次處理任何文件上傳。在這裏看着php手冊非常困惑。 – aahhaa 2014-12-01 21:04:56
是的。 PHP的文件處理非常糟糕。如果您對輸入字段的名稱使用'[]'數組符號,則會更糟糕。 PHP爲此構建了一個完全混亂的數組結構。 – 2014-12-01 21:05:49