Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code to save the uploaded file to a static directory.

<?php
//set the image size in mb
$max_upload_size='10';
$MAXIMUM_FILESIZE = $max_upload_size * 1024 * 1024; 
$uploaddir = './uploads/file/'; 
$file = $uploaddir . basename($_FILES['uploadfile']['name']); 
if ($_FILES['uploadfile']['size']<$MAXIMUM_FILESIZE) {
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
  echo "success"; 
} else {
echo "error";
}
}else{
echo"size_error";
}
?>

But what I want is to create a username directory under "/uploads/file/". For example if the username is "john", then it should create a folder like "/uploads/file/john/" and saved the file under that folder.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Note - User needs to be logged in to work

global $current_user;
get_currentuserinfo();
$uploaddir = './uploads/file/'.$current_user->user_login.'/'; 
wp_mkdir_p( $uploaddir );

Reference - wp_mkdir_p() -

share|improve this answer
    
the correct function is get_current_user();. but my problem now is it does not create a "username" directory. the file will upload successfully if there is an existing username (i.e. john) directory. –  jaypabs Aug 4 '12 at 14:33
    
Call wp_mkdir_p( $uploaddir); before you attempt to move the upload. –  TheDeadMedic Aug 4 '12 at 16:34
    
@jaypabs The function get_current_user(); you are refereeing to is a PHP function not a wordpress. –  amit Aug 4 '12 at 16:41
    
@TheDeadMedic thanks for tip. –  amit Aug 4 '12 at 17:25
    
@amit yes you are right. thank you very much. –  jaypabs Aug 5 '12 at 0:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.