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

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低