Chris
Chris

Reputation: 23

WP plugin image upload

$pid = $wpdb->insert_id; 
$newname = "$pid.jpg";

$dir = plugin_dir_path( __FILE__ );
    echo "<pre>";
    var_dump($dir);
    echo "</pre><br>";  
move_uploaded_file( $_FILES['fileField']['tmp_name'], "images/$newname");


$message ="directoryBase inserted";

The above is my attempt at debugging this Wordpress snippet. Basically I'm creating a mini directory of people.

Right above this snippet of code is my MYSQLI insert statement which upon execution returns my $pid. The ID is inserted into the database then I use that to match and pull url when I'm ready to display the image as the row is called from the database.

Typically I create a directory for images then set them to this directory as done above. The folder images/ is located within the plugin main directory.

The issue I'm having is determining the move_uploaded_file line.

Is there a specific way I can figure out how far up or down to move to find the correct directory. I can't throw any errors and no image is added to my custom plugin directory as intended.

I'm not completely up to snuff with Wordpress, but I do know enough PHP to be dangerous.

Upvotes: 0

Views: 55

Answers (1)

jogesh_pi
jogesh_pi

Reputation: 9782

Before moving the files into any directory Keep following steps in mind:

  1. Check what kind of file you want to upload, like images, pdf, word files etc.
  2. Check the size of files if its neccessary to you.
  3. Check the directory where you want to upload files is exists or not, Make sure you have increased uploaded max size from php.ini file
  4. Check the permission of the directory

If all the above points passed then proceed with move_uploaded_file().
Now with your script check if the images directory exists in your plugin or wherever you want to store.

// http://codex.wordpress.org/Function_Reference/plugin_dir_path
$path = plugin_dir_path(__FILE__);
if( ! is_dir($path.'images') ) {
    mkdir($path.'images', 0755);
} 

Then Use move_uploaded_file like this

// Return true if file moved successfully
if( move_uploaded_file( $_FILES['fileField']['tmp_name'], $path."images/$newname") ) {
}

Hope this will help you.

Upvotes: 1

Related Questions