我目前使用這個PHP代碼上傳功能。我需要知道如何爲1)擴展添加一些文件類型驗證,2)文件大小限制,以及3)檢查是否確實選擇了要上載的文件。PHP上傳文件類型,大小和存在?
感謝您的任何幫助。
<?php
if(isset($_POST['submit'])){
if(count($_FILES['upload']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != "tmp/"){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = "uploads/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
}
}
}
}
header('Location: http://localhost/FloodMap/report.html');
exit;
}
?>
你可以檢查這個獲得擴展http://stackoverflow.com/questions/10368217/how-to-get-the-file-extension-in-php 一邊你只是puth $ _FILES ['upload '] ['size'] – vibol
看到這個鏈接,你會得到你所有的答案:http://www.w3schools.com/php/php_file_upload.asp – Avishake