问题
I'm using laravel/homestead vagrant box.
When I try to use DirectoryIterator
for synced folder it returns only the "dot" entries (..
,.
).
My vagrant synced folder is called code
(in vmbox), inside this directory I have my vagrant files and project codes, so when I use DirectoryIterator
for /home/vagrant
it list all directories without problem including code
directory. But when I use it for /home/vagrant/code
only dot entries shows up, other directories is ignored. scandir()
works normally and list all directories without a problem.
Edit: here is the code and outputs:
foreach (new DirectoryIterator('/home/vagrant') as $fileInfo) {
echo $fileInfo->getFilename() . "<br>\n";
}
outputs:
.
..
.ssh
.sudo_as_admin_successful
.composer
.local
code
.config
.bash_aliases
.wget-hsts
.npm
foreach (new DirectoryIterator('/home/vagrant/code') as $fileInfo) {
echo $fileInfo->getFilename() . "<br>\n";
}
outputs:
.
..
foreach (scandir('/home/vagrant/code') as $fileInfo) {
echo $fileInfo. "<br>\n";
}
outputs:
.
..
.vagrant
Homestead.yaml
LICENSE.txt
Vagrantfile
after.sh
aliases
bin
composer.json
composer.lock
homestead
project
readme.md
scripts
src
回答1:
I had the same issue with vagrant. I debugged a little and found out the issue is the in the file type of the synced folder and not with the DirectoryIterator
Problem
Vagrantfile - folder sync config issue
config.vm.synced_folder "/var/www/virtual/machine/folder", "/var/www/host/machine/folder"
In the above configuration I did not specify any file type for the sync folder. because of that, Vagrant automatically choose the best synced folder option for my environment
Solution
I have changed the sync folder file type to "nfs" which solved the issue
Vagrantfile - solution
config.vm.synced_folder "/var/www/virtual/machine/folder", "/var/www/host/machine/folder", type: "nfs"
Note
Before using synced folders backed by NFS, the host machine must have nfsd installed, the NFS server daemon. This comes pre-installed on Mac OS X, and is typically a simple package install on Linux.
来源:https://stackoverflow.com/questions/63207799/directoryiterator-ignoring-directories-in-vagrant-synced-folders