I'm new to PHP, and I'm trying to build a script. When I load the script, I get the following error:
Warning: opendir(http://www.hetweerinboskamp.nl/voorpagina/movies) [function.opendir]: failed to open dir: not implemented
<?php
$hal ='';
$dir ='http://www.hetweerinboskamp.nl/voorpagina/movies';
if ($handle = opendir($dir)) {
// Loop the folders
while (false !== ($file = readdir($handle))) {
if(strlen($file) > 4) {
$rawd = parsename($file);
$hal.= 'new Date('.substr($rawd,0,4).', '.substr($rawd,4,2).'-1, '.substr($rawd,6,2).'),';
//$hal.= $rawd.',';
}
closedir($handle);
}
opendir() is used to open a local directory and since PHP 5.0.0 on an ftp directory.
If your PHP code runs on www.hetweerinboskamp.nl then /voorpagina/movies is actually a local directory and you can do this:
$dir ='<wwwroot>/voorpagina/movies';
if ($handle = opendir($dir)) {
where wwwroot is the root of the filesystem as seen by your php code.
If you're trying to download content from another website, try e.g. file_get_contents(). Note that if the remote server lists the content of a directory the listing is in fact an HTML page generated on the fly by the server. You may find yourself needing to parse that page. A better approach is to check whether the server offers some sort of API where it sends back the content in a standardized form, e.g. in JSON format.
opendir operates on directories on a filesystem, not HTTP URIs.
While some HTTP URIs return directory listings (the one you are using doesn't, it is a 404 error), those listings as HTML documents generated by the webserver and are not actual directories.
Manual claims this function works with URL's, however, it appears it doesn't.
Use a local path (either relative or absolute). For example, './voorpagina/movies'. This has solved a similar problem to me before. I hope this helps.
Most remote servers does not send a directory listing back as such opendir cannot understand what your trying to do so it cant work.
You will need to use something like ftp, here is an example: http://php.net/manual/en/ftp.examples-basic.php or cURL
Related
I'm trying to make PHP grab the paths to all images in a folder (and then select one at random), but I keep getting a 403 - forbidden warning.
I tried using both glob() and scandir(), as well as trying the path directly in the browser.
Here is my code (using glob()) -
$directory = plugins_url('images/backgrounds', __FILE__);
if(is_dir($directory)) :
$backgrounds = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
endif;
I'm using WAMP and developing locally, so I can't see any reason why Apache wouldn't have access to the folder in question.
The $directory is correct, so it's not that it doesn't exist that is causing a problem (a 404 error is returned if I deliberatly alter the path to one I know doesn't exist).
I am preventing indexing via .htaccess but I have since removed this line and it made no difference.
Can anyone please suggest what I might check to try and fix this issue? Thanks.
Edit
I should add that my PHP and Apache logs seem to not be logging this event.
In your code, you have used - plugins_url which looks like a Wordpress function which will return a URL.
But you have to pass a file path to the glob function.
So you can use plugin_dir_path(), a wordpress function to get absolute path of plugin directory which can be used in glob function.
I was getting below error so changed
opendir(http://test.myserv.com/Optfolder/upload/upload)
[function.opendir]: failed to open dir: not implemented
so changed to file_get_contents($dir)
But then in next I got this error
readdir(): supplied argument is not a valid Directory resource
What should I do?
I use below code to read folder images is there any option for opendir and readdir so ic an use them for my purpose?
In $dir I get full path like http://testserv.com/optfolder/upload
$dir = opendir($dir);
while ($file = readdir($dir)) {
if (preg_match("/.png/",$file) || preg_match("/.jpg/",$file) || preg_match("/.gif/",$file) ) {
$string[] = $file;
}
}
opendir() cannot be used on a URL as it opens a directory relative to the filesystem. This is also a potential duplicate of Warning: open dir: not implemented.
readdir() is also exactly the same as opendir() in regards to how it locates directories.
Please take a look at the documentation http://php.net/manual/en/function.opendir.php to fully understand it.
You can't use opendir() on HTTP urls. You need to specify a relative directory path instead. See opendir
Try using this:
file_get_contents('http://testserv.com/optfolder/upload');
I need a function/method in php to access multiple web pages in a loop. Just as someone can manually access a web page and load any scripts on there. I'm not downloading any information I just want the script to access it so that any php code can run on that page. It's a hack for a program i'm working on that needs cron jobs running. The cron job will run one script that will load multiple pages eg. http:// localhost/program/script1, http:// localhost/program/script2. I can then dynamically add pages from a database as time goes on.
here you would just separate the code you want shared into another file and then use
require("/path/to/filename.php");
The path instead of being a url will be the filesystem path to where you saved the file.
Good starting points to reference this file is $_SERVER["DOCUMENT_ROOT"] so you could say something like.
require($_SERVER["DOCUMENT_ROOT"]."/program/script1.php");
You could scan a directory and loop through each of the files in it then read or write to it with fopen
$files = scandir('folder/');
foreach($files as $file) {
//do your work here
$fhandle = fopen($file, 'r');
}
http://www.w3schools.com/php/func_directory_scandir.asp
http://php.net/manual/en/function.fopen.php
I'm new to PHP, and I'm trying to build a script. When I load the script, I get the following error:
Warning: opendir(http://www.hetweerinboskamp.nl/voorpagina/movies) [function.opendir]: failed to open dir: not implemented
<?php
$hal ='';
$dir ='http://www.hetweerinboskamp.nl/voorpagina/movies';
if ($handle = opendir($dir)) {
// Loop the folders
while (false !== ($file = readdir($handle))) {
if(strlen($file) > 4) {
$rawd = parsename($file);
$hal.= 'new Date('.substr($rawd,0,4).', '.substr($rawd,4,2).'-1, '.substr($rawd,6,2).'),';
//$hal.= $rawd.',';
}
closedir($handle);
}
opendir() is used to open a local directory and since PHP 5.0.0 on an ftp directory.
If your PHP code runs on www.hetweerinboskamp.nl then /voorpagina/movies is actually a local directory and you can do this:
$dir ='<wwwroot>/voorpagina/movies';
if ($handle = opendir($dir)) {
where wwwroot is the root of the filesystem as seen by your php code.
If you're trying to download content from another website, try e.g. file_get_contents(). Note that if the remote server lists the content of a directory the listing is in fact an HTML page generated on the fly by the server. You may find yourself needing to parse that page. A better approach is to check whether the server offers some sort of API where it sends back the content in a standardized form, e.g. in JSON format.
opendir operates on directories on a filesystem, not HTTP URIs.
While some HTTP URIs return directory listings (the one you are using doesn't, it is a 404 error), those listings as HTML documents generated by the webserver and are not actual directories.
Manual claims this function works with URL's, however, it appears it doesn't.
Use a local path (either relative or absolute). For example, './voorpagina/movies'. This has solved a similar problem to me before. I hope this helps.
Most remote servers does not send a directory listing back as such opendir cannot understand what your trying to do so it cant work.
You will need to use something like ftp, here is an example: http://php.net/manual/en/ftp.examples-basic.php or cURL
My project files are located on remote server in the folder. I access a file in this folder in this way:
http://www.example.com/searchtest.html
This opens a page with Input Box where user types keywords to search. The search script is a .php file located in the root itself. The script has to search for .html files with the name similar to the keywords entered. These .html files are also located in the same root folder where all .php files reside.
My application is running well when I work on local machine and is able to search well, but when I hosted it on my site, it gives error:
Warning: file_get_contents(//.rnd) [function.file-get-contents]: failed to open stream: Permission denied in /home/myServer/public_html/example/search1.php on line 40
.rnd
It is giving error on a line where I am trying to access files in my directory. Below is my code snippet. And yes, I am the administrator and I have all privileges on my site.
$matchingFiles = array();
$dirName = "\\";
$dh = opendir($dirName);
while( ($file = readdir($dh)) !== false)
{
$fullPath = $dirName . "/" . $file;
if(is_dir($fullPath)) continue; // Skip directories
similar_text($lcSearch,strtolower($file),$percentSimilar);
if($percentSimilar >= $percentMatch)
{
$matchingFiles[] = $fullPath;
}
}
It gives error probably in the "opendir" function.
I also want to know whether the $dirName holds correct path or not. It must hold the same path from where the correct script is run.
Assuming you did not change the $dirName assignment to omit server detail on a public posting, you should be setting that to the path to search. Since your use is pretty static, I'd suggest using the full path to the directory to open, e.g.
$dirName = '/home/myServer/public_html';
And of course make sure the permissions to all files it will search are compatible with the user your server is running as.