我正在尝试遍历一个目录并显示里面的所有图片。我让它工作,直到我将 Jquery/ajax 添加到等式中。不知何故,路径被复制了,所以我得到了这个错误(你可以看到它在哪里被复制):
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(images/gallery/album1,images/gallery/album1) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: The system cannot find the path specified
我一生都无法弄清楚为什么会这样……contruct(images/gallery/album1,images/gallery/album1)
应该是 construct(images/gallery/album1)
PHP:
$album = $_POST['album']; $dir = new DirectoryIterator("images/gallery/$album"); foreach ($dir as $fileInfo) { if($fileInfo->isDot()) continue; $pic = $fileInfo->getFilename(); print "<div> <img src='images/gallery/$album/$pic'> </div>"; }
jQuery/ajax:
function albumChosen(id) { var id = id; var album = $('a[id="'+id+'"]').attr("rel"); $.ajax({ url: "PHPscripts/getAlbums.php", type: "POST", data: {'album' : album}, success: function(data){ $('#galleryList').html(data); } }); return false;
}
如果我alert(album);
正确显示'album1'。所以它与它如何从$_POST
编辑:我添加了建议的代码..
$album = $_POST['album'];
print_r($_POST);
var_dump($album);
$dir = new DirectoryIterator("images/gallery/$album");
//rest of code...
print_r($_POST)
打印出来 Array ( [album] => album1 )
var_dump($album)
打印出来string 'album1' (length=6)