binar
binar

Reputation: 356

php glob function returns nothing

Hi Stackoverflow masters, I have problem with glob function php.

$gallery_full = glob("/var/www/home.com/uplo_1/*.*",GLOB_NOSORT);
print_r($gallery_full);

This lines returns me nothing. All dirs have chmod 777. In uplo_1 there are jpg and png files. Any one know how to run it ?

Upvotes: 2

Views: 1487

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Edit: After testing your posted code with my own system path, there were no problems.

  • Check to make sure you have your system path correct.

  • Use phpinfo(); inside a file, look under Environment, then DOCUMENT_ROOT.

    • If you do not have access/privilege to use phpinfo(), then login to your admin panel, and/or contact your hosting provider for your system path, should that be the case.
  • Use error reporting http://php.net/manual/en/function.error-reporting.php

"All dirs have chmod 777"

  • That isn't the safest setting. Use 755 for folders and 644 for files.

Original answer before edit:

This is what I use and would be better if your folder happens to contain files other than images.

  • Sidenote: It's best for a folder to have an index file, otherwise someone may find the folder and have a peek at what's inside it, that's if you haven't taken care of this otherwise.

Ensure that the path is correct.

$imagesDir = '/path/to/files';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE | GLOB_NOSORT);

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Reference:

Upvotes: 4

prismus
prismus

Reputation: 91

When I use glob I prepend the DOCUMENT_ROOT first, like this:

$fileCount = $_SERVER['DOCUMENT_ROOT'] . '/path/to/file.ext';
echo $fileCount;  

This should work.

Upvotes: 1

Related Questions