0

When a user creates a profile and uploads a profile picture, the PHP "move_uploaded_file" function is used to move a temporary copy of this image to the project directory.

While my current code works perfectly fine on my Windows 10 machine, it doesn't work on my Mac because of incorrect file permissions.

I have already tried the method provided in other answers and nothing changes after running the terminal command:

sudo chmod 644 /opt/lampp/htdocs/yourfolder

This is true for any variation including:

sudo chmod -R 777 /opt/lampp/htdocs/yourfolder

Note that I DO have admin permissions on the laptop

Other users have stated that these commands have fixed their problems, so I suspect that mine is different.

PHP Code Snippet:

$username = $_POST["username"];
$password = $_POST["password"];
$ext = null;

/* Debugging information
echo $_FILES["profile_pic"]["name"]."<br>";
echo $_FILES["profile_pic"]["tmp_name"]."<br>";
echo $_FILES["profile_pic"]["size"]."<br>";
echo $_FILES["profile_pic"]["error"]."<br>";
*/

if (strrpos($_FILES["profile_pic"]["name"], ".jpg") != null || strrpos($_FILES["profile_pic"]["name"], ".jpeg") != null) {
    $ext = ".jpg";
};
if (strrpos($_FILES["profile_pic"]["name"], ".png") != null) {
    $ext = ".png";
};

$path = __DIR__ . "\data\users\images\\" . uniqid() . $ext;

if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $path)) {
    echo("File successfully uploaded");
} else {
    echo("Upload failed");
};

Returned: Upload failed

Error thrown:

move_uploaded_file(/opt/lampp/htdocs/Test-Site\data\users\images\5c1dadd52bb71.jpg): failed to open stream: Permission denied in /opt/lampp/htdocs/Test-Site/signup_confirm.php on line 27

Expected: user profile picture is inserted into the .../htdocs/Test-Site/data/users/images folder

Actual: above error about incorrect file permissions is thrown

Andrew
  • 357
  • 5
  • 16
  • 1
    possible solution: https://stackoverflow.com/a/36020844/1597430, also you mix different types of slashes as a directory separators and you may use `DIRECTORY_SEPARATOR` constant to fix it. – user1597430 Dec 22 '18 at 04:00
  • @user1597430 thanks, the directory separator was my problem – Andrew Dec 22 '18 at 04:19

1 Answers1

-1

The reason for the error is that I was using incorrect directory separators ("\" instead of "/" for Mac). Changing these made the profile picture move to the intended location.

Andrew
  • 357
  • 5
  • 16