-1

I'm trying to make a new directory to handle profile pictures, but every time I upload the image, nothing happens as in the new directory is not created and there are no errors whatsoever. I already checked the Apache error log, but didn't notice any error pertaining to my recent code...

Here's a sample of my code

    //profile image upload script
    if (isset($_FILES['profile_pics'])) {
        if (((@$_FILES["profile_pics"] ["type"] == "image/jpeg" || (@$_FILES["profile_pics"] ["type"] == "image/png") || (@$_FILES["profile_pics"] ["type"] == "image/gif")) && (@$_FILES["profile_pics"] ["size"] < 1048576)) ) // LESS THAN ONE MEGABYTE

        {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            $rand_dir_name = substr(str_shuffle($chars), 0, 15);
            mkdir("userdata/profile_pics/$rand_dir_name");




        else {

        }

} }

metropolision
  • 405
  • 4
  • 10
EmmCee
  • 1
  • 1
  • 2
    Please add error reporting at the top of your file(s): `` And tell us the errors. Also check the permissions of the dir that you are allowed to create new sub dir's – Rizier123 Feb 06 '15 at 17:08
  • 2
    make sure the directory in which you are creating new directories has proper permissions – Bhavya Shaktawat Feb 06 '15 at 17:09
  • May be helpful for you: http://stackoverflow.com/questions/7101840/mkdir-failed-creating-directory-php?rq=1 http://stackoverflow.com/questions/13908722/php-unable-to-create-a-directory-with-mkdir?rq=1 – Blake Frederick Feb 06 '15 at 17:10
  • 2
    Use an absolute path, and make sure the directory has write permissions, 0775 should do it in most cases – CᴴᵁᴮᴮʸNᴵᴺᴶᴬ Feb 06 '15 at 17:10
  • done any basic debugging, like checking mkdir()'s return value? plus. don't use `@`. it's the coding equivalent of stuffing your fingers in your ears and going "lalalalalala can't hear you" – Marc B Feb 06 '15 at 17:29

2 Answers2

2

You have multiple errors in your PHP, are you sure you did not see any FATAL errors in your apache logs?

  • Your if query does not close properly
  • Your else statement is missing a braket
  • You're suppressing any errors with @ - remove them!
  • Use an absolute path instead of relative path to avoid errors in where you're creating the directory

Here's an example of how it should look:

// array of valid image types
$valid = array( 'image/jpeg', 'image/png', 'image/gif' );

if (isset($_FILES['profile_pics'])) {

    // if the file type is in the valid array and the size is less than 1MB
    if ( in_array($_FILES['profile_pics']['type'], $valid)
         && ($_FILES["profile_pics"]["size"] < 1048576) )
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        $rand_dir_name = substr(str_shuffle($chars), 0, 15);
        mkdir($_SERVER['DOCUMENT_ROOT'] . "/userdata/profile_pics/$rand_dir_name");

    } else {
        print( 'too big or not an image.' );
    }
}

EDIT

You should also check the mkdir response.. like this:

    $ok = mkdir($_SERVER['DOCUMENT_ROOT'] . "/userdata/profile_pics/$rand_dir_name");

    if( !$ok )
    {
        print('error creating directory, check your permissions');
    } else {
        print('created directory successfully!');
    }
0

Try to add permission.

mkdir("/path/", 0755);

Anand
  • 1,011
  • 1
  • 9
  • 13