如何在php(wordpress)中上载自定义文件(.txt、.rar、.xml、.zip)?

如何在php(wordpress)中上载自定义文件(.txt、.rar、.xml、.zip)?,php,wordpress,file-upload,Php,Wordpress,File Upload,我想启用直接将文件上载到根目录。支持的文件扩展名应为:.txt、.rar、.xml、.zip。我正试图用w3schools的代码来实现这一点,因为我是php和wordpress插件开发的初学者。插件可以工作,左侧的管理栏上有一个菜单图标,管理页面上有一个上载选项,但是当我“上载”文件时,上载文件夹中没有文件(我第一次尝试这样做,就像W3上的代码一样。代码是: HTML <div class="wrap"> <h2>Test file upload page</h2&

我想启用直接将文件上载到根目录。支持的文件扩展名应为:.txt、.rar、.xml、.zip。我正试图用w3schools的代码来实现这一点,因为我是php和wordpress插件开发的初学者。插件可以工作,左侧的管理栏上有一个菜单图标,管理页面上有一个上载选项,但是当我“上载”文件时,上载文件夹中没有文件(我第一次尝试这样做,就像W3上的代码一样。代码是:

HTML

<div class="wrap">
<h2>Test file upload page</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</div>

但是,wordpress目录中没有php.ini文件。有没有办法用wordpress插件编辑php.ini文件?也许这就是问题所在。猜猜看。

您的代码有以下问题:

  • 不需要
    upload.php
    文件,所有内容都应该放在一个文件中,在本例中是您的主插件文件

  • 由于没有
    upload.php
    文件,表单操作应该是空的

  • PHP代码中的所有内容都应该在
    if(isset($\u POST[“submit”]){}
    中,因为如果没有提交任何内容,则不需要执行上载脚本中的任何内容

  • 您的
    $target\u dir
    应该是一个,类似于:

    • /home/user/public\u html/wp content/
    • /Users/username/Sites/wp/wp admin
  • 下面是一个基于您发布的代码的小示例,我使用它设置目标目录。在我的测试中,图像被上传到
    http://localhost/wp-content/2018/10/image.png

    <?php
    /**
     * Plugin Name: (SO) Testing upload
     */
    add_action('admin_menu', function () {
        add_menu_page( 
            'Upload', 
            'Upload', 
            'read', 
            'so_upload', 
            'so_upload_callback', 
            null, 
            6 // position, just after Posts
        );
    });
    
    function so_upload_callback() 
    {
        ?>
        <div class="wrap">
        <h2>Test file upload page</h2>
        <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
        </form>
        </div>
        <?php
        // Check if image file is a actual image or fake image
        if(isset($_POST["submit"])) {
            $target_dir = wp_upload_dir();
            $target_file = $target_dir['path'] . '/' . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
            $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
            if($check !== false) {
                echo "File is an image - " . $check["mime"] . ".";
                $uploadOk = 1;
            } else {
                echo "File is not an image.";
                $uploadOk = 0;
            }
            // Check if file already exists
            if (file_exists($target_file)) {
                echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["fileToUpload"]["size"] > 500000) {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != 
                "jpeg"
            && $imageFileType != "gif" ) {
                echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                echo "Sorry, your file was not uploaded.";
                // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been 
                        uploaded.";
                } else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
    
        }
    }
    
    
    测试文件上传页面
    选择要上载的图像:
    
    哇。谢谢。可以这样做吗?上传的文件可以放在网站的根目录上(以及wp内容、wp管理员等)。另外,我可以添加另一个菜单页面,例如(上传的文件)然后,只有当上传了文件时才会显示该页面,因此我可以从中删除文件?我不是要求你为我这样做,而是给我一些建议以及我应该去哪里查看。再次感谢你以如此出色和有用的方式回答我的问题。是的,请查看关于“绝对路径”的链接当然,只需使用
    add_submenu_page()
    创建一个页面,在该页面中,您将列出目标目录的内容和图像。在Google上,使用“what you want site:stackoverflow.com”你会发现很多可以使用的代码。请检查此处和处的高级搜索功能,也可以查找内容。祝你好运!如果有人有类似的问题,并且希望将上载目标发送到根目录,你可以使用:
    get\u home\u path()
    而不是
    wp\u upload\u dir()
    ,然后将此行更改为
    $target\u file=$target\u dir['path']./'.basename($\u FILES[“fileToUpload”][“name”]);
    $target\u file=get\u home\u path()./'.basename($\u FILES[“fileToUpload”][“name”]);
    file_uploads = On
    
    <?php
    /**
     * Plugin Name: (SO) Testing upload
     */
    add_action('admin_menu', function () {
        add_menu_page( 
            'Upload', 
            'Upload', 
            'read', 
            'so_upload', 
            'so_upload_callback', 
            null, 
            6 // position, just after Posts
        );
    });
    
    function so_upload_callback() 
    {
        ?>
        <div class="wrap">
        <h2>Test file upload page</h2>
        <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
        </form>
        </div>
        <?php
        // Check if image file is a actual image or fake image
        if(isset($_POST["submit"])) {
            $target_dir = wp_upload_dir();
            $target_file = $target_dir['path'] . '/' . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
            $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
            if($check !== false) {
                echo "File is an image - " . $check["mime"] . ".";
                $uploadOk = 1;
            } else {
                echo "File is not an image.";
                $uploadOk = 0;
            }
            // Check if file already exists
            if (file_exists($target_file)) {
                echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["fileToUpload"]["size"] > 500000) {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != 
                "jpeg"
            && $imageFileType != "gif" ) {
                echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                echo "Sorry, your file was not uploaded.";
                // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been 
                        uploaded.";
                } else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
    
        }
    }