Warning: move_uploaded_file(/users/6.png): Failed to open stream: Permission denied in C:\xampp\htdocs\scripts\pfpUpload.php on line 30
Warning: move_uploaded_file(): Unable to move "C:\xampp\tmp\phpA126.tmp" to "/users/6.png" in C:\xampp\htdocs\scripts\pfpUpload.php on line 30 Error while saving the profile picture.
Here's the PHP code too:
<?php
session_start();
// Verificar si la sesión no está iniciada y, si no, redirigir a login.php
if (!isset($_SESSION['username'])) {
header("Location: login");
exit();
}
// Obtener la ID de usuario del atributo data-user-id del body
$user_id = isset($_POST['user_id']) ? $_POST['user_id'] : '-1';
// Directorio donde se guardarán las imágenes de perfil
$targetDir = "/users/";
// Verificar si se envió un archivo
if (isset($_FILES["profilePicture"])) {
$file = $_FILES["profilePicture"];
// Obtener la extensión del archivo
$extension = pathinfo($file["name"], PATHINFO_EXTENSION);
// Nombre de archivo único basado en la ID de usuario
$fileName = $user_id . "." . "png";
// Ruta completa de destino para guardar el archivo
$targetFilePath = $targetDir . $fileName;
// Intentar mover el archivo al directorio de destino
if (move_uploaded_file($file["tmp_name"], $targetFilePath)) {
echo "La imagen de perfil se ha guardado correctamente.";
} else {
echo "Error al guardar la imagen de perfil.";
}
} else {
echo "No se ha seleccionado ninguna imagen.";
}
?>
So I am having an issue with a PHP file that should move the image of the input into a folder called "users" that stores the profile pictures
The thing is that, besides having full read & write permissions on both the PHP and the folder, still doesn't work.
I tried this command that GPT gave me but it didn't work
icacls C:\xampp\htdocs\users /grant IUSR:(OI)(CI)F
Also tried this, nothing either
<Directory "/xampp/htdocs/users">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User hostname
Group hostname
</IfModule>
I really need to fix this as I need to use something similar in other folders
The issue was that I used "/users/" and the PHP was looking for "C:\users" which we don't have permissions to write.
The correct one should be "users/" if it's in the same folder or, like in my case, ../users/ if it's in a sub folder.
Thanks to @ÁlvaroGonzález and @ADyson for the help