Why $_FILES['uploadfiles'] not working and running blank?

297 views Asked by At

I am trying to upload image through custom.php in wordpress when i print the

`$_FILES['uploadfiles'];` 

its not showing any thing the same code i have used before one day in another wordpress site the code is working good over there on ec2 my current wordpress is on ec2 and have bitnami image i have checked all configuration like in php.ini file upload_tmp_dir=/opt/bitnami/php/tmp ,file_uploads=on,upload_max_filesize=40M.

i am not using any form i have just added upload field in one hook that display that field on one page.

I am using thesis theme.so any boday have any idea why this happening with this code on while one copy of same code work on another wordpress with guru theme.

Bellow is the code i am using to upload image.

<input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" />



<?php
$uploadfiles = $_FILES['uploadfiles'];

print_r ($uploadfiles);

  if (is_array($uploadfiles)) {
  echo "1";
    foreach ($uploadfiles['name'] as $key => $value) {

      // look only for uploded files
      if ($uploadfiles['error'][$key] == 0) {

        $filetmp = $uploadfiles['tmp_name'][$key];

        //clean filename and extract extension
        $filename = $uploadfiles['name'][$key];

        // get file info
        // @fixme: wp checks the file extension....
        $filetype = wp_check_filetype( basename( $filename ), null );
        $filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
        $filename = $filetitle . '.' . $filetype['ext'];
        $upload_dir = wp_upload_dir();

        /**
         * Check if the filename already exist in the directory and rename the
         * file if necessary
         */
        $i = 0;
        while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
          $filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
          $i++;
        }
        $filedest = $upload_dir['path'] . '/' . $filename;


        /**
         * Check write permissions
         */
        if ( !is_writeable( $upload_dir['path'] ) ) {
          $this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
          return;
        }

        /**
         * Save temporary file to uploads dir
         */
        if ( !@move_uploaded_file($filetmp, $filedest) ){
          $this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
          continue;
        }

        $attachment = array(
          'post_mime_type' => $filetype['type'],
          'post_title' => $filetitle,
          'post_content' => '',
          'post_status' => 'inherit'
        );

        $attach_id = wp_insert_attachment( $attachment, $filedest );
        require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
        $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
        echo $attach_data; 

        echo $filedest; 


        wp_update_attachment_metadata( $attach_id,  $attach_data );
        //echo wp_get_attachment_url( $attach_id );
        //exit();
        $urlimage=wp_get_attachment_url( $attach_id );
          $_SESSION["oldurl"]=$urlimage; 
        echo  $_SESSION["oldurl"];
      }
    }
  }
   ?>
0

There are 0 answers