hey guys apparently directory functions such as opendir, and scandir are not working on my host.
following is the code, its from php.net.

the output is keep on coming fail.
is there something wrong with my php settings or something else.

the page is,

<?php

$dir = "http://www.worldofpakistan.net/";
$dh  = scandir($dir);
if($dh)
{
echo "success";
}
else
{
echo "fail";
}

while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?>

Dani AI

Generated

Short diagnosis: as recommended, printing the last PHP warning revealed an OS-level ENOTTY / “Inappropriate ioctl for device” condition. That typically happens when PHP’s directory functions are asked to operate on something that isn’t a real filesystem directory or on a stream wrapper that doesn’t implement directory operations. scandir/opendir expect a local path (or a wrapper that implements dir_opendir/dir_readdir), so passing an HTTP (or an unsupported) URL commonly produces the exact failure seen. (php.net)

Practical checks and a minimal local fix:

  • Confirm the target is a filesystem path (use DIR or $_SERVER['DOCUMENT_ROOT'] to build it) and verify it with is_dir() / is_readable() before calling scandir().
  • Enumerate available stream wrappers to see what's supported on the host.
    Example snippets (place in a test script, not a public page):
$dir = __DIR__ . '/';
if (!is_dir($dir) || !is_readable($dir)) {
    var_dump(error_get_last());
    exit;
}
$files = array_diff(scandir($dir), ['.','..']);
print_r($files);

print_r(stream_get_wrappers());

Use stream_get_wrappers() to see which URL wrappers are present on that PHP build. (php.net)

If the goal is listing files on a remote host, HTTP will only work if the remote server exposes a directory listing or an API; otherwise use FTP/SSH-based listing APIs (e.g. ftp_nlist() / SFTP) or add a server-side script to return JSON. Some hosting builds have had wrapper-related issues (examples where missing/disabled curl-wrapper support caused errno 25); in those cases the provider must enable the needed wrappers or the script must switch to native FTP/SSH calls. (php.net)

Quick troubleshooting checklist: check phpinfo() for allow_url_fopen, open_basedir and disabled functions; confirm file ownership/permissions and test from the CLI (paths differ between CLI and web SAPI). If stream wrappers that are required are absent at the host level, only the hosting provider can change the PHP build or enable them. (php.net)

Recommended Answers

All 3 Replies

replace 'echo "fail";'
with print_r(error_get_last());

im getting the following output:

Array ( [type] => 2 [message] => scandir() [function.scandir]: (errno 25): Inappropriate ioctl for device [file] => /home/worldofp/public_html/try/write.php [line] => 7 )

instead of the list of files and directories.

I've never seen that error and there's not much documentation on it so I would assume that it is a problem at the server level.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.