Wordpress自定义文件上传页面

时间:2013-03-15 02:52:10

标签: wordpress

我目前正在使用此代码进行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> 

upload.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";
    }

即使我知道它应该有效,它总是返回“无效文件”。

我一直在网上寻找,我认为WP不能做enctype =“multipart / form-data”,这就是为什么它不起作用。

有没有人有工作或知道为什么这不起作用?

5 个答案:

答案 0 :(得分:2)

我知道这篇文章有点老了,但希望能帮到别人。

我做了非常相似的事情(自定义页面上显示的自定义上传),并且实际上使用了与objectiveccoder001完全相同的代码。我一直得到&#34;文件无效。&#34;并写入权限错误。我结束了这个:

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

答案 1 :(得分: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";
    }
 
}

答案 2 :(得分:1)

您可以使用此代码使用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>

答案 3 :(得分:0)

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

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

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

答案 4 :(得分:-3)

为什么不使用WordPress内置上传程序上传文件? 这是一个关于如何实现 WordPress Uploader

的快速教程

WordPress Uploader