Wordpress 插件开发 - 文件上传:如何允许指定的文件类型?

Posted

技术标签:

【中文标题】Wordpress 插件开发 - 文件上传:如何允许指定的文件类型?【英文标题】:Wordpress plugin development - File upload: How to allow specified file types? 【发布时间】:2019-03-16 01:45:04 【问题描述】:

这是允许将图像上传到网站根目录的代码。是管理区的子菜单页面,调用一个函数,函数体如下:

 <div class="wrap">
<h2>Upload files</h2><br><br><br>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" class="button" name="fileToUpload" id="fileToUpload">
<input type="submit" class="button button-primary" value="Upload File" name="submit">
</form>
</div>
<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) 
    $target_dir = get_home_path();//wp_upload_dir();
    $target_file = get_home_path() . '/' . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) 
        ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
        <strong>File is an image</strong>
        </div>
        <?php
        $uploadOk = 1;
     else 
         ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
        <strong>File is not an image</strong>
        </div>
        <?php
        $uploadOk = 0;
    
    // Check if file already exists
    if (file_exists($target_file)) 
         ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
        <strong>File already exists.</strong>
        </div>
        <?php
        $uploadOk = 0;
    
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) 
          ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
        <strong>Your file is too large.</strong>
        </div>
        <?php
        $uploadOk = 0;
    
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != 
        "jpeg"
    && $imageFileType != "gif" ) 
          ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
        <strong>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</strong>
        </div>
        <?php
        $uploadOk = 0;
    
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) 
          ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
        <strong>Sorry, your file was not uploaded.</strong>
        </div>
        <?php
        // if everything is ok, try to upload file
     else 
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))                  
             ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
        <strong>The file has been uploaded.</strong>
        </div>
        <?php

         else 
            echo "Sorry, there was an error uploading your file.";
        
    


我想允许这些文件类型而不是图像:.xml、.rar、.zip、.txt。我尝试使用函数getfilesize() 更改函数getimagesize() 并更改// Allow certain file formats 注释下方的文件扩展名。但它不起作用。您知道我应该怎么做才能允许上传这些文件类型而不是当前允许上传的文件类型吗?

【问题讨论】:

【参考方案1】:

基本上...

$allowed_extenstions=array('jpg','png','pdf','zip');

    $ext = pathinfo(esc_attr($_FILES['file']['name']), PATHINFO_EXTENSION);
            if (!in_array($ext,$allowed_learning_extenstions))
            
                echo "invalid_file_type";
                die();
            

`

【讨论】:

谢谢,我会试试的。但是如果 $check 是由 getimagesize() 设置的,如果文件不是图片,如何获取 $check 呢? ***.com/questions/15408125/…。应该可以帮助你解决这个问题。

以上是关于Wordpress 插件开发 - 文件上传:如何允许指定的文件类型?的主要内容,如果未能解决你的问题,请参考以下文章

wordpress上传图片时重命名--修改插件时遇到的一些问题

wordpress上传主题出错

为啥我的wordpress总是能上传所有类型文件?

WordPress REST API - 如何在没有插件的情况下进行身份验证

使用 WordPress 插件从 pdf 创建 jpgs

如何允许单个 Wordpress 站点接受我的 .ttf 文件上传?