Gifron
Gifron

Reputation: 96

Wordpress user image upload

I'm working on custom Wordpress Membership website. I've created plugin for users to register and login but I'm stuck with option for users to upload their profile image.

This is my form for image upload, there are also some other inputs:

    <p class="form-row">
      <input type="file" name="wp_custom_attachment"id="wp_custom_attachment" size="50" />
    </p>

Now on my script, I have public function that will fetch data from inputs. I use wp_upload_bits function:

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

Is there something else that needs to be added so this would work?

Upvotes: 1

Views: 956

Answers (1)

jaggedsoft
jaggedsoft

Reputation: 4038

File uploads will only work if your <form> element has the proper encoding set and uses POST:

<form enctype="multipart/form-data" action="upload.php" method="POST">

PHP's default file uploads are limited to a maximum size of 2MB

You might also run into a server-side file size limitation. Edit php.ini:

; Maximum allowed size for uploaded files.
upload_max_filesize = 64M

; Must be greater than or equal to upload_max_filesize
post_max_size = 64M

If you're using Apache Web Server, you can create a file named .htaccess in the same directory as your scripts:

php_value upload_max_filesize 64M
php_value post_max_size 64M

You can also put this in the PHP file that handles uploads:

ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');

You need to add a space in between the name and ID of your objects or it will prevent the upload from working properly when using any browser other than Chrome, Safari, or Firefox: (file uploads require name attribute)

<input type="file" name="wp_custom_attachment"id="wp_custom_attachment" size="50" />

Change to:

<input type="file" name="wp_custom_attachment" id="wp_custom_attachment" size="50" />

Upvotes: 1

Related Questions