dpm91915 2015-10-21 05:51
浏览 129
已采纳

允许php上传脚本上的所有文件扩展名

So I am using a raspberry pi and want to use it as a place where I can upload and download any kind of file with any kind of extension.

Here is my 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>

Here is my 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;
                }
            }
        }
    }
}

It works great but only lets me upload .txt and .jpg files. I tried removing the "$allowed" thing all together but broke the script.

Any ideas?

  • 写回答

1条回答 默认 最新

  • douliang1900 2015-10-21 05:53
    关注

    I think you should change code to this :

        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;
                }
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改