允许 php 上传脚本的所有文件扩展名
Allow all file extensions on php upload script
所以我正在使用 raspberry pi 并想将它用作我可以上传和下载具有任何扩展名的任何类型的文件的地方。
这是我的html
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="upload">
</form>
</body>
</html>
这是我的php
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('txt', 'jpg');
if(in_array($file_ext, $allowed)) {
if($file_error === 0) {
if($file_size <= 1073741824) {
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'files/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
}
}
}
}
}
效果很好,但只允许我上传 .txt 和 .jpg 文件。我尝试一起删除“$allowed”,但破坏了脚本。
有什么想法吗?
我认为您应该将代码更改为:
if($file_error === 0) {
if($file_size <= 1073741824) {
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'files/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
}
}
}
所以我正在使用 raspberry pi 并想将它用作我可以上传和下载具有任何扩展名的任何类型的文件的地方。
这是我的html
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="upload">
</form>
</body>
</html>
这是我的php
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('txt', 'jpg');
if(in_array($file_ext, $allowed)) {
if($file_error === 0) {
if($file_size <= 1073741824) {
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'files/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
}
}
}
}
}
效果很好,但只允许我上传 .txt 和 .jpg 文件。我尝试一起删除“$allowed”,但破坏了脚本。
有什么想法吗?
我认为您应该将代码更改为:
if($file_error === 0) {
if($file_size <= 1073741824) {
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'files/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
}
}
}