3

我目前正在使用此代码进行 PHP 文件上传(直接在 wordpress 页面上找到):

<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form> 

上传.php

    $allowedExts = array("jpg", "jpeg", "gif", "png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/gif")
         || ($_FILES["file"]["type"] == "image/jpeg")
         || ($_FILES["file"]["type"] == "image/png")
         || ($_FILES["file"]["type"] == "image/pjpeg"))
        && ($_FILES["file"]["size"] < 20000)
        && in_array($extension, $allowedExts))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

            if (file_exists("upload/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                                   "upload/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
            }
        }
    }
    else
    {
        echo "Invalid file";
    }

即使我知道 100% 它应该可以工作,它也总是返回“无效文件”。

我一直在网上四处寻找,我认为 WP 不能做 enctype="multipart/form-data" 所以这就是它不起作用的原因。

有没有人有解决方法或知道为什么这不起作用?

4

5 回答 5

9

我知道这篇文章有点旧,但希望能对其他人有所帮助。

我正在做一些非常相似的事情(自定义上传以显示在自定义页面上)并且还使用与objectiveccoder001. 我不断收到“无效文件”。和写权限错误。我最终选择了这个:

if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
    echo "File is valid, and was successfully uploaded.\n";
    //var_dump( $movefile);
} else {
    echo "Possible file upload attack!\n";
}

如果您不希望使用 Wordpress 的媒体上传器并且只需要简单的文件上传,则效果很好。它仍然使用内置上传器等过时的文件结构上传它。然后你可以只使用$movefile数组来获取文件的数据。

参考:http ://codex.wordpress.org/Function_Reference/wp_handle_upload

于 2014-05-23T18:26:19.950 回答
3

您可以使用此代码使用 wordpress 默认媒体文件上传器,只需在 jquery 中检索图像链接

<label for="upload_image">
        <input id="upload_image" type="text" size="36" name="ad_image" value="http://" /> 
        <input id="upload_image_button" class="button" type="button" value="Upload Image" />
        <br />Enter a URL or upload an image
    </label>

<?php
add_action('admin_enqueue_scripts', 'my_admin_scripts');

function my_admin_scripts() {
    if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') {
        wp_enqueue_media();
        wp_register_script('my-admin-js', WP_PLUGIN_URL.'/my-plugin/my-admin.js', array('jquery'));
        wp_enqueue_script('my-admin-js');
    }
}

?>

<script>
    jQuery(document).ready(function($){


    var custom_uploader;


    $('#upload_image_button').click(function(e) {

        e.preventDefault();

        //If the uploader object has already been created, reopen the dialog
        if (custom_uploader) {
            custom_uploader.open();
            return;
        }

        //Extend the wp.media object
        custom_uploader = wp.media.frames.file_frame = wp.media({
            title: 'Choose Image',
            button: {
                text: 'Choose Image'
            },
            multiple: true
        });

        //When a file is selected, grab the URL and set it as the text field's value
        custom_uploader.on('select', function() {
            console.log(custom_uploader.state().get('selection').toJSON());
            attachment = custom_uploader.state().get('selection').first().toJSON();
            $('#upload_image').val(attachment.url);
        });

        //Open the uploader dialog
        custom_uploader.open();

    });


});
    </script>
于 2015-06-19T09:30:34.447 回答
3

在新版本的 WordPress 中上传文件非常容易。只需一行代码:

$uploaded_file = wp_upload_bits( $_FILES['file']['name'], null, @file_get_contents( $_FILES['file']['tmp_name'] ) ); 

$uploaded_file是一个数组,它将返回所有信息,包括 url、文件类型等

于 2019-09-26T10:18:00.900 回答
2

如何在 WORDPRESS 中将文件上传到自定义目录

此示例上传wp-content/uploads/customDirectory文件夹中的 PDF 文档

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submittheform">
</form>

global $wp_filesystem;
WP_Filesystem();

$content_directory = $wp_filesystem->wp_content_dir() . 'uploads/';
$wp_filesystem->mkdir( $content_directory . 'CustomDirectory' );

$target_dir_location = $content_directory . 'CustomDirectory/';
 
if(isset($_POST["submittheform"]) && isset($_FILES['fileToUpload'])) {
 
    $name_file = $_FILES['fileToUpload']['name'];
    $tmp_name = $_FILES['fileToUpload']['tmp_name'];
 
    if( move_uploaded_file( $tmp_name, $target_dir_location.$name_file ) ) {
        echo "File was successfully uploaded";
    } else {
        echo "The file was not uploaded";
    }
 
}
于 2021-01-17T15:34:15.903 回答
-5

为什么不使用 WordPress 内置上传器来上传您的文件?
这是一个关于如何实现WordPress Uploader的快速教程

WordPress 上传器

于 2013-03-15T04:17:48.753 回答