Вместо
<input type="file" name="userfile[]" id="input1">
<input type="file" name="userfile[]" id="input2">
Вы можете сделать следующее различие между двумя
<input type="file" name="userfile[desiredNameOfFile1]" id="input1">
<input type="file" name="userfile[desiredNameOfFile2]" id="input2">
С PHP обработки это следующим образом:
foreach($_FILES['userFile']['name'] AS $desiredNameOfFile => $fileInfo) {
//Get the temp file path
$tmpFilePath = $_FILES['userfile']['tmp_name'][$desiredNameOfFile];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = $local_path .'images/' . $desiredNameOfFile . pathInfo($_FILES['userfile']['tmp_name'][$desiredNameOfFile],PATHINFO_EXTENSION);
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
Будьте в курсе: этот код будет перезаписывать файлы, которые уже есть это имя
Редактировать
Если вы хотите несколько файлов выбирает
<input type="file" name="userfile[desiredNameOfFile1][]" id="input1" multiple>
<input type="file" name="userfile[desiredNameOfFile2][]" id="input2" multiple>
Php
foreach($_FILES['userfile']['name'] AS $desiredNameOfFile => $fileInfo) {
for($i = 0; $i < count($fileInfo); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['userfile']['tmp_name'][$desiredNameOfFile][$i];
// Make sure we have a filepath
if ($tmpFilePath != ""){
// Setup our new file path
$newFilePath = $local_path .'images/' . $desiredNameOfFile . $i . pathInfo($_FILES['userfile']['tmp_name'][$desiredNameOfFile][$i],PATHINFO_EXTENSION);
// Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
// Handle other code here
}
}
}
}
}
Вы можете добавить случайный сгенерированный ключ с каждым файлом. –
'лучший способ разрешить мне переименовать имена файлов' почему? Я имею в виду, какова проблема, с которой вы сталкиваетесь сейчас? – Jigar
Несколько подробностей с изображениями diff, и я просто суетливый, и я хочу, чтобы все было в порядке, я знаю, что, если какой-либо из кодовых разрывов –