Failed to open stream permission denied

17,573

What are the permissions on the folder? You may need to change it to enable PHP to write to it.

Share:
17,573
Harrison Howard
Author by

Harrison Howard

Updated on June 14, 2022

Comments

  • Harrison Howard
    Harrison Howard almost 2 years

    I am creating a profile image uploader, where the user can select a file and upload it, but I am getting this error:

    Warning: move_uploaded_file(user_files/profile_img/c3487ff77d.jpg) [function.move-    uploaded-file]: failed to open stream: Permission denied in     /Applications/XAMPP/xamppfiles/htdocs/projects/lr/core/functions/users.php on line 4
    
    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpi0WECJ' to 'user_files/profile_img/c3487ff77d.jpg' in /Applications/XAMPP/xamppfiles/htdocs/projects/lr/core/functions/users.php on line 4
    

    Line two through five is:

    function change_profile_image($user_id, $file_temp, $file_extn) {
    $file_path = 'user_files/profile_img/' . substr(md5(time()), 0, 10) . '.' . $file_extn;
    move_uploaded_file($file_temp, $file_path);
    }
    

    And the rest in a loggedin.php file is:

    if (isset($_FILES['profile_img']) === true) {
                if (empty($_FILES['profile_img']['name']) === true) {
                    echo 'Please choose a file!';
                } else {
                    $allowed = array('jpg', 'jpeg', 'png');
    
                    $file_name = $_FILES['profile_img']['name'];
                    $file_extn = strtolower(end(explode('.', $file_name)));
                    $file_temp = $_FILES['profile_img']['tmp_name'];
    
                    if (in_array($file_extn, $allowed) === true) {
                        change_profile_image($session_user_id, $file_temp, $file_extn);
                    } else {
                        echo 'Invalid file type! Allowed Types: ';
                        echo implode(', ', $allowed);
                    }
                }
            }
    
            if (empty($user_data['profile_img']) === false) {
                echo '<img src="', $user_data['profile_img'],'" alt="', $user_data['first_name'] ,'\'s Profile Image">';
            }
    

    I'm not sure what I am doing wrong? Its not my .htaccess, it must be the code, but why?

    Thanks