I'm trying to read a directory with php's scandir on Google App Engine. The files within the folder 'img' are definitely reachable, but I am trying to get a list of all files in the folder. What's wrong?
All files are apparently uploaded correctly, so app.yaml should be fine. Is it a PHP error or a GAE restriction?
This is my main php-script, run when accessing my site's /:
<?php
$dir = 'img';
if (is_dir($dir)) {
//this code is never reached:
echo "directory exists :-)";
$filenames = scandir($dir, 0);
print_r($filesnames);
} else {
echo "No such directory";
}
// this works:
echo "<div>
<img src=\"/img/example.png\">
</div>";
// this works:
$files1 = scandir(getcwd());
print_r($files1);
?>
opendir($dir) doesnt help either.
Here's my app.yaml:
application: mypage
version: 1
runtime: php55
api_version: 1
handlers:
# image files
- url: /img/(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: img/\1
upload: img/(.*\.(bmp|gif|ico|jpeg|jpg|png))
- url: /.*
script: helloworld.php
# this is probably redundant:
- url: /img
static_dir: img
EDIT: Adding this line to the image files section (not necessarily the img folder) solved the problem: application_readable: true
From the documentation:
application_readable
Optional. By default, files declared in static file handlers are uploaded as static data and are only served to end users, they cannot be read by an application. If this field is set to true, the files are also uploaded as code data so your application can read them. Both uploads are charged against your code and static data storage resource quotas.
Try adding the application_readable
flag to the img
handler. I'm not 100% sure if the flag will also cover the img
dir itself in addition to its content. It's worth a try.