2

I have the following code to upload a file:

<?php
//set the image size in mb
$max_upload_size='10';
$MAXIMUM_FILESIZE = $max_upload_size * 1024 * 1024;
get_current_user();
global $current_user;
$uploaddir = './uploads/file/'.$current_user->user_login.'/'; 
$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";
}
?>

This works great if the user_login already exists under /uploads/file/ directory. How can I create the directory that is based on variable $current_user->user_login if it doesn't exists? If exists then continue on the next code.

2
  • i tried it but i can't get this to work. here's my sample code: if(file_exists($uploaddir ) && is_dir($uploaddir )) { mkdir("./uploads/file/$current_user->user_login", 0777); } Commented Aug 4, 2012 at 14:49
  • Why -1? This is a valid question. Commented Aug 4, 2012 at 14:54

2 Answers 2

5

From http://nz.php.net/manual/en/function.move-uploaded-file.php#105026:

The destination directory must exist; move_uploaded_file() will not automatically create it for you.

Just use is_dir and mkdir.

<?php
//set the image size in mb
$max_upload_size='10';
$MAXIMUM_FILESIZE = $max_upload_size * 1024 * 1024;
global $current_user;
$current_user = get_current_user();
$uploaddir = './uploads/file/'.$current_user->user_login.'/';
if (!is_dir($uploaddir) && !mkdir($uploaddir)){
  die("Error creating folder $uploaddir");
}
$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";
}
?>
Sign up to request clarification or add additional context in comments.

17 Comments

is seems move_uploaded_file returns true but the folder is not created successfully.
Run this script. It may help. If the folder doesn't exist, it should give you a "Error creating folder" message.
may be it creates on different directory. but i don't know where it is. the full path is this based on my cPanel: public_html/wp-content/themes/my-theme/upload/uploads/file.
So your $uploaddir variable should be ./upload/uploads/file/, maybe?
i don't think so because the code above is located under public_html/wp-content/themes/my-theme/upload folder named as upload-file.php. this file is being called by an ajax code like action: '<?php echo UPLOAD; ?>/upload-file.php',
|
1
  <?php
//set the image size in mb
$max_upload_size='10';
$MAXIMUM_FILESIZE = $max_upload_size * 1024 * 1024;
get_current_user();
global $current_user;
if(!is_dir('./uploads/file/'.$current_user->user_login))
{
   mkdir('./uploads/file/'.$current_user->user_login);
}
$uploaddir = './uploads/file/'.$current_user->user_login.'/'; 
$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";
}
    ?>

Or after $updloaddir initialization add if statement with $uploaddir parameter in is_dir() and mkdir().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.