user11716766
user11716766

Reputation:

Only allow zip file uploads

I have already made an upload form. But I want users to be able to upload .zip files and .rar files. I already have tried the following:

$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.";
}

It does not show any errors. It only enters inside the else. It also does this when I am uploading .zip files, I do not know what is wrong.

Upvotes: 2

Views: 609

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

1.You need to use pathinfo() to get file extension.

2.$filename is no where defined in your code, so it will give you warning as well as extension checking code will always fail.

Modify code like below:

$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.";
}
/* ] */

Upvotes: 1

Related Questions