【发布时间】:2015-01-04 21:37:52
【问题描述】:
这是我偶然发现的一段代码,它允许用户将多个文件上传到您的网站。它工作得很好,但是我如何才能使上传的文件类型受到限制?
例如,用户可以上传他们想要的任何文件(.txt、.php、.png、.jpg 等),但假设我只希望他们上传(.html、.png、.jpg),怎么会我这样做?
$target_dir = "";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
//Loop through each file
for($i=0; $i<count($_FILES['fileToUpload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['fileToUpload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "" . $_FILES['fileToUpload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
【问题讨论】:
标签: php image file-upload upload