PHP Directory handling - php

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.

Related

PHP opendir() not opening apache alias directory [duplicate]

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

Error when attempting to rename all the files in a directory (PHP)

I am practicing PHP on my computer using PHPStorm. I have a directory full of images that I placed in my project. These images were pulled off of a web page and all have names such as "attachment_56602224.jpg." I want to rename them to all numbers (like 1.jpg, 2.jpg) just for simplicity. Here's what I have so far:
$i = 1;
if ($handle = opendir('pics')) {
while (false !== ($fileName = readdir($handle))) {
rename($fileName, $i);
$i++;
}
closedir($handle);
}
Here is the error I am getting when I run it in my browser:
Warning: rename(.,1): The process cannot access the file because it is being used by another process. (code: 32) in C:\Users\Mike\PhpstormProjects\php\learn.php on line 12
Warning: rename(..,2): Access is denied. (code: 5) in C:\Users\Mike\PhpstormProjects\php\learn.php on line 12
Warning: rename(attachment_56602224.jpg,3): The system cannot find the file specified. (code: 2) in C:\Users\Mike\PhpstormProjects\php\learn.php on line 12
And that last error repeats for every image.
the first thing you should do is checking if the files that you are reading are effectively files and not directories, also you should use the relative path for the source and destination file you are renaming.
In that snippet code, you are trying to rename a file in the root directory of your project and not in the "pics" subfolder.

Move Zpanel Default Backups Directory or Opendir Backups directory

I'm working on a script which will upload all the zpanel backups to my Amazon S3 account. It works if I don't use the zpanel backups and create and upload backups created by me using CronJob.
The reason why I cannot upload the zpanel backup is I cannot figure out a way to reach the "backups" directory using php opendir function.
I know the absolute path of the backups folder though. And as it seems, this absolute path won't work with opendir()
I've used : opendir('var/zpanel/hostdata/my_username/backups/')
And it won't work. Now if I want to use the relative path, I cannot reach there either.
So, is there a way that I can move the zPanel Backups directory somewhere inside the public_html folder? Or, a web URL that can reach the backups folder? Ajaxplorer can reach there, why I cannot?
If none is possible, I'll greatly appreciate if someone can teach me a way to create backups exactly like zPanel (all DB + everything inside public_html folder).
The code looks like this
require_once('S3.php');
// Enter your amazon s3 creadentials
$s3 = new S3('xxxxx', 'xxxxx');
if ($handle = opendir('var/zpanel/hostdata/my_username/backups/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
//echo "okay";
echo $file; exit;
if ($s3->putObjectFile("backups/$file", "my_basket", "$file", S3::ACL_PUBLIC_READ)) {
echo "<strong>We successfully uploaded your file.<br /></strong>";
//this will delete the file from your server after upload
//if (file_exists($baseurl . '/' . $file)) { unlink ($baseurl . '/' . $file); }
}else{
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
}else{
echo "No file found here ".$root;
}
}
closedir($handle);
}
Thanks a lot in advance!
You have a typo in your code. If you want to use an absolute path with opendir, make sure it starts with a slash (/). So the correct version would be:
opendir('/var/zpanel/hostdata/my_username/backups/')
If you still can't get this to work, verify that the path you pass to opendir is indeed a directory and also verify the permissions on that directory to make sure your PHP process can read from it.
From the PHP doc,
If path is not a valid directory or the directory can not be opened
due to permission restrictions or filesystem errors, opendir() returns
FALSE ...
Also, if you use a any open-basedir restrictions in your php.ini or in your code, make sure the path you pass to opendir is not blocked by those restrictions.

Move uploaded file to root directory

I'm making a content management system for a website I built. I want the system to be discrete, so I made it exist in only one PHP file, called '_admin.php'. All the content displayed in this file comes from includes that I store in a sub-folder called 'admin' (out of the way).
The photos used on the website are stored in an 'assets' folder that also sits in the root dir. The admin page has direct access to the assets folder, as it is also in the root. But the upload file script sits a few directories into the 'admin' folder and I want the uploaded files to be stored in the assets folder.
The move_uploaded_file() method takes the destination path for the file, but it requires a direct path. I try using $_SERVER['DOCUMENT_ROOT'], but the resulting directory doesn't seem to have any of my files. If I use getcwd() in a doc in the root, it returns the actual file structure that I can use. The same if I echo out __FILE__. But I've experimented with this SERVER constant a lot and I can't locate my website with it.
Since the script that uploads the images is called as a form action, I can't pass the root directory as a variable.
Not really sure what I'm doing wrong, anyone have any ideas?
Thanks
edit **
//See if Files array contains new files
if (!empty($_FILES['file'])){
foreach($_FILES['file']['name'] as $key => $name){
$error = $_FILES['file']['error'][$key];
$temp_name = $_FILES['file']['tmp_name'][$key];
$dir = getcwd();
$move_file = move_uploaded_file($temp_name, "$dir/temp/$name");
if (($error == 0) && ($move_file)){
$uploaded[] = $name;
}else{
die($error);
}
}
echo __FILE__;
echo "<br/>";
echo __DIR__;
echo "<br/>";
echo $_SERVER['DOCUMENT_ROOT'];
exit();
}
The upload script I'm currently using. This script works fine because I'm storing the images in the same directory as the script. I just don't know how to store them in my root.
I would specify the full absolute file path if you can. I set this via define() in a config file for my CMS. On install you figure out what that path is and set it.
define("BASEFILEPATH", "/home/.../[webroot]"); // The base file path for the website
You may be looking for something more general, but you could have some sort of install script where the user can enter basic info into a form, such as username, pwd, etc. and you could have them enter this path as well.

Warning: open dir: not implemented

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

Categories