I am using the following code to upload and move a file to the folder "film_images":
$filepath = '../images/film_images/';
echo '<br />Trying to store file at ' . $filepath;
if (!move_uploaded_file(
$_FILES['teaserimage']['tmp_name'],
sprintf($filepath . '%s.%s',
'test',
$ext))) {
throw new RuntimeException('Failed to move uploaded file.');
}
However, as many people here, I always got a
Failed to open stream: Permission Denied
exception in PHP. Then I went to the server and, using the setfacl
command I gave permission rw-
to the user www-data
, which is the user running this PHP script. Using rw-
I still got the exception. Only when I switched rights to rwx
, i.e. when I gave www-data
full control on this folder, it worked. Now I wonder two things:
- Why is it necessary to give the user execution rights in order to write a file?
- Is there a way to write the file without giving execution rights to the user? I fear that somebody might upload code, hidden in an image file, and execute it on my server.