мне нужно загрузить два файла с разными внутр Например:Как переименовать имя файла перед загрузкой с PHP
file_1.txt и file_image.png
, когда пользователь отправил два файла, используя закачать форму :
file_1.txt и file_image.png
перед тем move_uploaded_file мне нужно переименовать изображение, чтобы быть, как TXT. Имя файла:
file_1.txt file_1.jpeg
Я просто хочу, чтобы переименовать имя файла изображения, а не расширение
код что-то вроде этого:
switch($_REQUEST['action']) {
case "upload":
$Imagefile_temp = $_FILES['file']['tmp_name'];
$Textfile_temp = $_FILES['file']['tmp_name'];
$Imagefile_name = $_FILES['file']['name'];
$Textfile_name = $_FILES['file']['name'];
$ImageFileType = array('png' ,'jpg');
$TextFileType = array('txt');
$Image_extension = pathinfo($Imagefile_name, PATHINFO_EXTENSION); // holds the file extension of the file
$Text_extension = pathinfo($Textfile_name, PATHINFO_EXTENSION); //holds the file extension of the file
// //Declaring Path for uploaded images
$file_path = "uploads";
//checks for duplicate files
if((!file_exists($file_path."/".$Imagefile_name)) || (!file_exists($file_path."/".$Textfile_name))) {
$j = 0; //Variable for indexing uploaded image
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
if(in_array($Image_extension,$ImageFileType)) {
$Image_extension = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
$file_extension = end($Image_extension); //store extensions in the variable
$target_path = $target_path . md5(uniqid()) . "." . $Image_extension[count($Image_extension) - 1];//set the target path with a new name of image
$j = $j + 1;//increment the number of uploaded images according to the files in array
$Imagefilestatus = move_uploaded_file($Imagefile_temp,$file_path."/"."image_".$Imagefile_name) ;//if file moved to uploads folder
}
if(in_array($Text_extension,$TextFileType)) {
$Text_extension = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
$file_extension = end($Text_extension); //store extensions in the variable
$target_path = $target_path . md5(uniqid()) . "." . $Text_extension[count($Text_extension) - 1];//set the target path with a new name of image
$j = $j + 1;//increment the number of uploaded images according to the files in array
$Textfilestatus = move_uploaded_file($Textfile_temp,$file_path."/".$Textfile_name) ;//if file moved to uploads folder
}
}
}
}
Хотя этот фрагмент кода приветствуется и может оказать некоторую помощь, он будет [значительно улучшен, если он содержит объяснение] (// meta.stackexchange.com/q/114762) из * how * и * why * this решает проблему. Помните, что вы отвечаете на вопрос читателей в будущем, а не только на человека, который спрашивает сейчас! Пожалуйста, отредактируйте свой ответ, чтобы добавить объяснение, и укажите, какие ограничения и допущения применяются. –