如何使用wp_Handle_Upload上传图片

时间:2015-09-04 作者:Hafiz Usman aftab

您好,我是WordPress的新手,有人能告诉我如何使用wp\\U handle\\U upload在循环中上传多个图像吗

for($i=1;$i<count($_FILES[\'myfile\'][\'name\']);$i++){

    $uploadedfile = $_FILES[\'myfile\'].$i;

    $upload_overrides = array( \'test_form\'.$i => false );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

    if ( $movefile && !isset( $movefile[\'error\'] ) ) {
      //  echo "File is valid, and was successfully uploaded.\\n";

        $dir_path= $movefile[\'url\'];


    } else {

        echo $movefile[\'error\'];
    }
}//end loop

2 个回复
最合适的回答,由SO网友:Maikal 整理而成

下面是我如何上载多个图像的示例:

add_action( \'add_meta_boxes\',   \'my_test_metabox\' );

function my_test_metabox() {
    add_meta_box( \'my_test_metabox\', \'File upload\', \'my_test_metabox_out\', \'post\' );
}

add_action(\'post_edit_form_tag\', \'update_edit_form\' );
function update_edit_form() {
    echo \' enctype="multipart/form-data"\';
}

function my_test_metabox_out( $post ) {

    $files = get_post_meta( $post->ID, \'my_files\', true );
    if( ! empty( $files ) ) {
        echo \'Files uploaded:\' . "\\r\\n";
        foreach( $files as $file ) {
            echo \'<img src="\' . $file[\'url\'] . \'" width="100" height="100" />\';
        }
        echo "\\r\\n";
    }
    echo \'Upload files:\' . "\\r\\n";
    echo \'<input type="file" name="my_files[]" multiple />\';

}

add_action( \'save_post\', \'my_files_save\' );

function my_files_save( $post_id ) {

    if( ! isset( $_FILES ) || empty( $_FILES ) || ! isset( $_FILES[\'my_files\'] ) )
        return;

    if ( ! function_exists( \'wp_handle_upload\' ) ) {
        require_once( ABSPATH . \'wp-admin/includes/file.php\' );
    }
    $upload_overrides = array( \'test_form\' => false );

    $files = $_FILES[\'my_files\'];
    foreach ($files[\'name\'] as $key => $value) {
      if ($files[\'name\'][$key]) {
        $uploadedfile = array(
            \'name\'     => $files[\'name\'][$key],
            \'type\'     => $files[\'type\'][$key],
            \'tmp_name\' => $files[\'tmp_name\'][$key],
            \'error\'    => $files[\'error\'][$key],
            \'size\'     => $files[\'size\'][$key]
        );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

        if ( $movefile && !isset( $movefile[\'error\'] ) ) {
            $ufiles = get_post_meta( $post_id, \'my_files\', true );
            if( empty( $ufiles ) ) $ufiles = array();
            $ufiles[] = $movefile;
            update_post_meta( $post_id, \'my_files\', $ufiles );

        }
      }
    }

}

SO网友:dev

您好,这是我在插件中使用的代码,希望您理解,如果有任何问题,您可以问我:-

if(isset($_FILES))
    {
        for ($i=1; $i < count($_FILES) ; $i++) 
        { 

            $uploadedfile = $_FILES[\'question_\'.$i];
            ?>
            <script type="text/javascript">
            $(document).ready(function(){
                var nam = <?php echo "question".$i; ?>
                var namm = $("."+nam).prev().attr(\'name\');

            });
            </script>
            <?php


        $upload_overrides = array( \'test_form\' => false );

        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

        if ( $movefile && !isset( $movefile[\'error\'] ) ) {
            //echo "File is valid, and was successfully uploaded.\\n";
            $filename = $movefile[\'file\'];
        } else {
            /**
             * Error generated by _wp_handle_upload()
             * @see _wp_handle_upload() in wp-admin/includes/file.php
             */
            echo $movefile[\'error\'];
        }

        // $filename should be the path to a file in the upload directory.
        //$filename = \'/path/to/uploads/2013/03/filename.jpg\';


        // Get the path to the upload directory.
        $wp_upload_dir = wp_upload_dir();

        $url = $wp_upload_dir[\'url\'] . \'/\' . basename( $filename );
        echo $url."<br>";

    }           
    }
这是我的html代码:-

    $question =get_post_meta($trivia->ID,\'question\',true);

     $c = 0;
     $id = uniqid();
    if ( count( $question ) > 0 ) {
        if(is_array($question)){

        foreach( $question as $questions ) {
            if ( isset( $questions[\'question\'] ) ) {


                printf(\'<p>Question: <input type="text" name="question[%1$s][question]" value="%2$s"> -- <span class="remove_question">%3$s</span><span class="add_answer">   Add Answer</span></p>\',   $c,$questions[\'question\'],__(\'Remove Question\'));
                for($i = 1; $i<=count($questions[\'answers\']); $i++)
                {
                    if($questions[\'answers\'][$i][\'image\'])
                    {
                        printf(\'<p>Option %5$s <input type="text" value="%1$s" name="question[%4$s][answers][%5$s][answer]"> Status <input type="text" value="%2$s" name="question[%4$s][answers][%5$s][status]"> Image <input type="text" name="question[%4$s][answers][%5$s][image]" value="%6$s"><img src="%6$s" height="100" widht="100"> -- <span class="remove_answer">%3$s</span></p>\', $questions[\'answers\'][$i][\'answer\'] ,$questions[\'answers\'][$i][\'status\'], __(\'Remove Answer\'), $c, $i,$questions[\'answers\'][$i][\'image\']);
                    }
                    else
                    {
                        printf(\'<p>Option %5$s <input type="text" value="%1$s" name="question[%4$s][answers][%5$s][answer]"> Status <input type="text" value="%2$s" name="question[%4$s][answers][%5$s][status]"> -- <span class="remove_answer">%3$s</span></p>\', $questions[\'answers\'][$i][\'answer\'] ,$questions[\'answers\'][$i][\'status\'], __(\'Remove Answer\'), $c, $i);
                    }
                }
                $c = $c +1;
            }
        }
    }
}
 ?>
<span id="question_here"></span>
<span class="add_question"><?php _e(\'Add Question\'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
         var countt = 0;
        $(".add_question").click(function() {
            count = count + 1;
            countt = 0;
            $(\'#question_here\').append(\'<p>Question: <input type="text" name="question[\'+count+\'][question]" value=""> -- <span class="remove_question">Remove question</span><span class="add_answer"> Add Answer</span></p>\' );
            return false;
        });
        $(".remove_question").live(\'click\', function() {
            $(this).parent().remove();
        });


        $(".add_answer").live(\'click\',function(){
            countt = countt + 1;
            $(\'#question_here\').append(\'<p>Answer <input type="text" name="question[\'+count+\'][answers][\'+countt+\'][answer]"> Status <input type="text" name="question[\'+count+\'][answers][\'+countt+\'][status]"><input id="question_\'+count+\'_answers_\'+countt+\'_image" type="text" size="36" name="question[\'+count+\'][answers][\'+countt+\'][image]" value="" /><input class="upload_image_button" type="button" value="Upload Image" /><span class="remove_answer">Remove</span></p>\');
        })
         $(\'.upload_image_button\').live(\'click\',function() {
         formfield = $(\'#upload_image\').attr(\'name\');
         tb_show(\'\', \'media-upload.php?type=image&amp;TB_iframe=true\');

         return false;
        });

         window.send_to_editor = function(html) {
         imgurl = $(\'img\',html).attr(\'src\');
         $(\'#question_\'+count+\'_answers_\'+countt+\'_image\').val(imgurl);
         tb_remove();
        }
        $(".remove_answer").live(\'click\', function() {
            $(this).parent().remove();
        });

    });
    </script>
</div><?php

相关推荐

Vimeo froogaloop

我是个新手,但我想用this idea 在Wordpress站点上嵌入的Vimeo剪辑中禁用正向搜索我的职能。我添加了phpfunction frogaloop_scripts() { wp_register_script(\'snippet\', \'https://siteurl/wp-content/themes/themename/js/snippet.js\'); wp_register_script(\'frogaloop\',\'https://f.vimeo