只允许 zip 文件上传

我已经做了一个上传表格。但我希望用户能够上传 .zip 文件和 .rar 文件。我已经尝试过以下方法:


$ftp_server = "myserver";

$ftp_user_name = "myuser";

$ftp_user_pass = "mypass";

$source_file = $_FILES['file']['tmp_name'];

$fileType = $_FILES['file']['type'];

$fileExt = explode('.', $filename);

$fileActualExt = strtolower(end($fileExt));

$destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/FilesUpload";

$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);

$conn_id = ftp_connect($ftp_server);

// login with username and password

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

ftp_pasv($conn_id, true); 


$allowed = array('zip', 'rar');

if(in_array($fileActualExt, $allowed)) {


//my upload code


}else {

    echo "You are not allowed to upload this file.";

}

它没有显示任何错误。它只进入 else 内部。当我上传 .zip 文件时它也会这样做,我不知道出了什么问题。


holdtom
浏览 270回答 1
1回答

catspeake

1.您需要使用pathinfo()来获取文件扩展名。2.$filename在你的代码中没有定义,所以它会给你警告,并且扩展检查代码总是会失败。修改代码如下:$ftp_server = "myserver";$ftp_user_name = "myuser";$ftp_user_pass = "mypass";$source_file = $_FILES['file']['tmp_name'];$fileType = $_FILES['file']['type'];/*get file extension [*/  $fileExt = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);  $fileExt = strtolower($fileExt);/* ] */$destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/FilesUpload";$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);$conn_id = ftp_connect($ftp_server);$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); ftp_pasv($conn_id, true); $allowed = array('zip', 'rar');/*use $extension variable to check allowed or not [*/if(in_array($fileExt, $allowed)) {//my upload code}else {    echo "U mag dit type bestand niet uploaden.";}/* ] */
打开App,查看更多内容
随时随地看视频慕课网APP