Загрузка файла Excel на сервер

Я не могу загрузить файлы msoffice в папку на сервере, я могу загружать файлы .pdf и файлы изображений, но не .docx, .xlsx или даже .txt, весь мой синтаксис выглядит правильным из-за моих ограниченных знаний. Это форма

<form id="Upload" action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="fileSelect">Navigate and choose:</label>
<input type="file" name="file" id="fileSelect"><br><br>
<input class="button" type="submit" name="action" value="Upload to Shared Folder">
</form>

и это upload_file.php

    if(isset($_FILES["file"]["error"])){
if($_FILES["file"]["error"] > 0){
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else{
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/msword", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.ms-excel", "pdf" => "application/pdf", "txt" => "application/txt");
$filename = $_FILES["file"]["name"];
$filetype = $_FILES["file"]["type"];
$filesize = $_FILES["file"]["size"];

// siati faaopoopo
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>");

// siati fua - 10MB
$maxsize = 200000 * 60;
if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>");

// siati MYME
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("general/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>";
} else{
move_uploaded_file($_FILES["file"]["tmp_name"], "general/" . $_FILES["file"]["name"]);
echo "The file was uploaded successfully.</br></br>";
}
}
else{
echo "Error: There was a problem uploading the file - please try again.";
}
}
} else{
echo "Error: Invalid parameters - something is very very very wrong with this upload.";
}

Должно быть, я что-то упустил, но не понял, что. Я попытался найти приложение / msword и другие вещи на stackexchange и google, но все кажется правильным. Я получаю ошибку:

    Error: There was a problem uploading the file - please try again.

Пожалуйста помогите

2

Решение

Вы, вероятно, проверяете неправильный mimetype, попробуйте сбросить все mimetypes, где файл не может быть загружен.

Вот небольшой обзор для mimetypes MS Office: office_mime_types

Если вы хотите, чтобы ваш файл проверялся немного лучше и безопаснее, вы можете проверить это следующим образом.

$allowed = array(
"xls" => array( "application/vnd.ms-excel" ),
"xlsx" => array(
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
);

// replace this
if ( in_array( $filetype, $allowed ) ) {}

// with this
// this checks also if mimetype of xlsx is just a mimetype of xlsx and nothing else
if ( isset( $allowed[$ext] ) && in_array( $filetype, $allowed[$ext] ) ) {}
4

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]