chubbyk
chubbyk

Reputation: 6302

How to add simple image upload functionality to WP plugin

I want to add simple image upload functionality to my WP plugin. So the simple form with upload button. I dont want to use standard thickbox included in WP for this.

When you press the button file selection dialog will appear, you select file from your drive and it'll be added to the input box. Then when you press "save" button for plugin options it will send with form and handled on server side.

I wonder if there is ready to use WP functionality for the server part. As I want to save the upload image path also to DB options table.

EDIT: added PHP tag as wordpress is a PHP language

Upvotes: 3

Views: 4313

Answers (1)

HermPheus
HermPheus

Reputation: 190

You're in some luck-I'm currently working on a mod that would allow image uploads for MarketPress. Here's my skeleton, poorly indented script. This should help get you started methinks?

<form enctype="multipart/form-data" action="" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="10240" />
    Send this file: <input name="userfile" type="file" />
    Send this file: <input name="userfile2" type="file" />
    <input type="submit" value="Send File" />
</form>

<?php
if ($_POST['MAX_FILE_SIZE'] == '10240' ){
   $uploaddir = 'public_html/uploads';
   $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
   $validfiletype;
if ( preg_match('/\\.(exe|com|bat|zip|doc|txt)$/i', $_FILES['userfile']['name']) )
    $validfiletype = 0;
elseif( preg_match('/\\.(jpg|jpeg|gif|png|pdf|psd)$/i', $_FILES['userfile']['name']) ) 
    $validfiletype = 1;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "File upload unsuccessful.";
}}
?>

Upvotes: 1

Related Questions