2015-02-12 83 views
1
function addFlyer($db) { 
if (isset($_FILES['file_array'])) { 
    $name_array = $_FILES['file_array']['name']; 
    $tmp_name_array = $_FILES['file_array']['tmp_name']; 
    $type_array = $_FILES['file_array']['type']; 
    $size_array = $_FILES['file_array']['size']; 
    $error_array = $_FILES['file_array']['error']; 
    for ($i = 0; $i < count($tmp_name_array); $i++) { 
     if (file_exists($name_array[$i])) { 
      echo "Sorry file already exists. "; 
      $uploadOk = 0; 
     } 
     else { 
      if (move_uploaded_file($tmp_name_array[$i], "images/" . $name_array[$i])) { 
       echo $name_array[$i] . " upload is complete<br>"; 
      } else { 
       echo "move_uploaded_file function failed for " . $name_array[$i] . "<br>"; 
      } 
     } 
    } 
} 

在我上传多个文件此代码到一个文件夹中的时刻,把它插入到数据库中(我排除的代码插入。)如何从数组中的文件名分解文件扩展名?

但是,如果我想使用file_exists正常工作,我需要从文件中分离扩展名和名称。但我不知道如何爆炸一个数组。

有没有人可以帮助我呢?

回答

2

你不需要爆炸或别的东西。您可以通过path_info()PHP函数获取图像扩展。请看下面的内容。

$path = $_FILES['file_array']['name']; 
$ext = pathinfo($path, PATHINFO_EXTENSION); 

如果要分解文件名。您可以使用下面的技巧

$file = $_FILES['file_array']['name']; 
$image_info = explode(".", $file); 
$image_type = end($image_info) 
+0

你好,谢谢你的回答。问题是,我想使用函数file_exists,并且该函数用它发送扩展..所以它不能正常工作。 – Patrick 2015-02-12 09:08:52

+0

如果你想分解一个文件名。您可以使用以下技巧 $ file = $ _FILES ['file_array'] ['name']; $ image_info = explode(“。”,$ file); $ image_type = end($ image_info); – Vineet 2015-02-12 09:12:39

1
foreach ($name_array as $value) { 
if (file_exists($value)) { 
    echo "Sorry file already exists. "; 
    $uploadOk = 0; 
} else { 
    if (move_uploaded_file($value, "images/" . $value)) { 
     echo $value . " upload is complete<br>"; 
    } else { 
     echo "move_uploaded_file function failed for " . $value . "<br>"; 
    } 
} 

}

这可能工作。使用foreach()循环而不是for()循环。