【问题标题】:PHP glob() returns an empty arrayPHP glob() 返回一个空数组
【发布时间】:2019-09-08 11:16:38
【问题描述】:

我有这个代码:

<?php
    session_start();

    if( isset( $_SESSION[ 'user' ] ) )
    {
        $pictures = glob( "http://localhost/wandelverhalen/cms/newPictures/*.*" );
        echo count( $pictures );
        echo "\n";
        echo( $_SESSION[ 'user' ] );
     }else{
        echo('You are not logged on.');
     }
?>

我在http://localhost/wandelverhalen/cms/newPictures 目录中有 50 个文件。但代码回显: 0 约翰内斯

这是调用 php 代码的 javascript:

$.ajax({
    method: 'post',
    url: url,
    dataType: 'json',
    success: function( response ){
        var data = response;
        console.log( data );
    },
    error: function( err ){
        console.log( err.responseText );
    }
});

我找到了类似问题的各种答案,但没有一个能解决我的问题。

非常感谢您的帮助。

【问题讨论】:

    标签: php glob


    【解决方案1】:

    您应该提供 glob 函数的绝对路径

    __DIR__."/wandelverhalen/cms/newPictures/*.*"
    

    用于所有类型的图像

    glob(__DIR__."/wandelverhalen/cms/newPictures/*.{jpg,png,gif}", GLOB_BRACE)
    

    【讨论】:

    • 感谢您的回答 Rakesh。但我得到相同的结果。 DIR 是包含 php 脚本的目录。 (C:\inetpub\wwwroot\wandelverhalen\styles\functions\php)。这导致: glob( C:\inetpub\wwwroot\wandelverhalen\styles\functions\phpwandelverhalen/cms/newPictures/*.*) 这是一个不存在的目录。
    • @johannes 确保你的路径应该是wandelverhalen/cms/newPictures
    • 谢谢。路径确实是问题所在。我发现了这个:glob("../../../cms/newPictures/*.{jpg,JPG,JPEG,gif,png}", GLOB_BRACE);
    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2019-08-26
    • 2019-11-12
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多