我的php文件上传器出了问题。它可以很好地上传小文件但是当我尝试上传大约8mb这样的大文件时它不起作用。它提出来了 不允许使用文件类型这没有任何意义,因为当文件较小时,相同的fileformat工作正常。
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
header("location: http://rmaltsaar.net/rfile");
die();
}
define("UPLOAD_DIR", "uploads/");
// process file upload
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_FILES["file"])) {
$file = $_FILES["file"];
if ($file["error"] !== UPLOAD_ERR_OK) {
echo "<p>Error</p>";
}
}
// verify the file is a PDF
$mimetype = mime_content_type($_FILES['file']['tmp_name']);
if(in_array($mimetype, array('image/jpeg', 'image/gif', 'image/png', 'video/webm', 'text/plain', 'audio/mpeg', 'application/zip')))
echo "<p>File type is okay</p>";
else {
echo "<p>File type is not permitted.</p>";
echo "<p>Redirecting back to R-FILE.</p>";
echo '<meta http-equiv="refresh" content="3; url=http://rmaltsaar.net/rfile">';
exit;
}
// ensure a random filename
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$name = generateName().".$ext";
$redirect = "http://rmaltsaar.net/rfile/uploads/" . $name;
// preserve file from temporary directory
$success = move_uploaded_file($file["tmp_name"], UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
} else {
echo "<p>Your file has been uploaded successfully!</p>";
echo "<p>Link to the file is: <a href='http://rmaltsaar.net/rfile/uploads/" . $name . "'>http://rmaltsaar.net/rfile/uploads/" . $name . "</a></p>";
}
function generateName($max = 6) {
$i = 0;
$possible_keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$keys_length = strlen($possible_keys);
$str = "";
while($i < $max) {
$rand = mt_rand(1, $keys_length-1);
$str .= $possible_keys[$rand];
$i++;
}
return $str;
}
?>
我甚至尝试更改php.ini值,它们也应该没问题
post_max_size = 30M
upload_max_filesize = 20M
答案 0 :(得分:0)
而不是:
$ext = pathinfo($filename, PATHINFO_EXTENSION);
尝试:
$ext = substr(strrchr($filename, "."), 1);
答案 1 :(得分:0)
不使用mime_content_type(),而是使用此
$ext = pathinfo($filename, PATHINFO_EXTENSION);
检查可能的扩展名。