从单一输入上传多张图片
<input type="file" name="files[]" multiple id="files" class="hidde fileInpu"/>
我制作了以下代码来阅读图片上传的详细信息
完整处理代码
$newname = md5(rand() * time());
$file1 = isset($_FILES['files']['name'][0]) ? $_FILES['files']['name'][0] : null;
$file2 = isset($_FILES['files']['name'][1]) ? $_FILES['files']['name'][1] : null;
$file3 = isset($_FILES['files']['name'][2]) ? $_FILES['files']['name'][2] : null;
$file4 = isset($_FILES['files']['name'][3]) ? $_FILES['files']['name'][3] : null;
$file5 = isset($_FILES['files']['name'][4]) ? $_FILES['files']['name'][4] : null;
if (isset($_FILES['files'])) {
$errors = array();
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$file_name = $key . $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['name'][$key];
$extarray = explode('.', $file_type);
$ext = end($extarray);
// png , jpg, jpeg
if ($file_size > 9097152000) {
$errors[] = 'File size must be less than 2 MB';
}
$desired_dir = "user_data/";
if (empty($errors) == true) {
if (is_dir($desired_dir) == false) {
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if (is_dir("$desired_dir/" . $file_name) == false) {
move_uploaded_file($file_tmp, "$desired_dir/" . $newname . $file_name);
} else { // rename the file if another one exist
$new_dir = "$desired_dir/" . $newname . $file_name;
rename($file_tmp, $new_dir);
}
} else {
print_r($errors);
}
}
if (empty($error)) {
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
}
我需要在echo
中显示每个文件的文件扩展名,我需要跳过文件名的文件名,我需要存储$key
以上代码读取图片name,size,file type, tmp name
现在我的问题是
$file_type = $_FILES['files']['type'][$key];
此代码提供的文件类型如image/jpeg
但我需要获得扩展程序 .jpeg
如何在.jpeg中获得扩展程序
并在阅读此主题后使用此方法PHP file extension issue $ext = pathinfo($file_name, PATHINFO_EXTENSION)[$key];
我收到错误所有文件属于同一类型我如何阅读每个文件扩展名
答案 0 :(得分:1)
使用php&#34; pathinfo&#34;功能得到扩展。
$ext = pathinfo($_FILES['news_image']['name'], PATHINFO_EXTENSION);
$file_type = pathinfo($_FILES['files']['name'][$key], PATHINFO_EXTENSION);
参考pathinfo
在这里,我发布完整代码以获得扩展。我添加了pathinfo函数来获取扩展名
<form method="post" action="index.php" enctype="multipart/form-data">
<input type="file" name="files[]" multiple id="files" class="hidde fileInpu"/>
<input title="" type="submit" name="submit" />
</form>
<?php
if(isset($ _ POST [&#39; submit&#39;])){
$newname = md5(rand() * time());
$file1 = isset($_FILES['files']['name'][0]) ? $_FILES['files']['name'][0] : null;
$file2 = isset($_FILES['files']['name'][1]) ? $_FILES['files']['name'][1] : null;
$file3 = isset($_FILES['files']['name'][2]) ? $_FILES['files']['name'][2] : null;
$file4 = isset($_FILES['files']['name'][3]) ? $_FILES['files']['name'][3] : null;
$file5 = isset($_FILES['files']['name'][4]) ? $_FILES['files']['name'][4] : null;
$errors = array();
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$file_name = $key . $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['name'][$key];
echo $file_name;
echo $ext = pathinfo($_FILES['files']['name'][$key], PATHINFO_EXTENSION);
die;
//$extarray = explode('.', $file_type);
// $ext = end($extarray);
// png , jpg, jpeg
if ($file_size > 9097152000) {
$errors[] = 'File size must be less than 2 MB';
}
$desired_dir = "user_data/";
if (empty($errors) == true) {
if (is_dir($desired_dir) == false) {
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if (is_dir("$desired_dir/" . $file_name) == false) {
move_uploaded_file($file_tmp, "$desired_dir/" . $newname . $file_name);
} else { // rename the file if another one exist
$new_dir = "$desired_dir/" . $newname . $file_name;
rename($file_tmp, $new_dir);
}
} else {
print_r($errors);
}
}
if (empty($error)) {
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
echo "FILE : $newname.$ext<br>";
}
}
?>
答案 1 :(得分:0)
尝试使用此示例
$file_type = $_FILES['files']['name'][$key];
$extarray=explode('.',$file_type);
$ext=end($extarray);
// png , jpg, jpeg