I have seen another question with the same problem, but with just 1 answer, which didn't worked for me.
The question: PHP scandir - multiple directories
I'm using Joomla! to create my site
(I create everything with templates, most that I don't understand creating an extension).
So, I created folder pbfData in main folder.
In it I created folder users. But scandir does not detects any of those.
Errors (without if (is_dir))
Warning: scandir(/pbfData/) [function.scandir]: failed to open dir: No such file or directory in [...]/pbf/index.php on line 61
Warning: scandir() [function.scandir]: (errno 2): No such file or directory in [...]/pbf/index.php on line 61
Fatal error: Cannot access empty property in [...]/pbf/index.php on line 61
The code looks like this:
class users {
var $users;
function refUsers() {
$dir = '/pbfData/';
if (is_dir($dir)) {
$this->$users = scandir($dir);
} else {
echo "<b>CRITICAL ERROR: </b>No access to users directory.";
}
}
[...] }
It outputs
CRITICAL ERROR: No access to users directory.
(Note: adding is_dir to solve it was answer to the other question, but as I have written it doesn't work for me.)
You are checking if the folder /pdfData/ exists, and since the path starts with / what you are really checking is if you have a folder with the name pdfData inside your ROOT folder (C:\ or D:\ [etc] in windows, and / in linux/unix).
That means that if you run your script /var/www/html/pdf/index.php, event if you have the folder /var/www/html/pdf/pdfData - the function is_dir('/pdfData/') will return false, because you don't have that folder in your ROOT/
Folder structure
----------------
/pdfData/ <-- This is the folder that your code check (which doesn't exists)
/var/
/var/www/
/var/www/pdf/
/var/www/pdf/pdfData/ <-- This is the folder you want to check for
If you want to check the folder pdfData inside the current folder that you are running your code from, you should use $dir = 'pbfData/'; (without the starting slash)
Another options is to use the __DIR__ Magic constant:
$dir = __DIR__ . '/pbfData/';
is_dir is not a solution to get directory path, it just checks if the file is a directory or not. To get the path you can use JPATH_SITE or JPATH_BASE etc as you are inside Joomla. Joomla already comes with built in constants. have a look here https://docs.joomla.org/Constants .
Your code will be
$dir = JPATH_BASE."/pbfData",
Related
I saw on internet a piece of php code to handle images, it goes like this:
$directory="books";
$dirint = dir($directory);
while (($archivo = $dirint->read()) !== false)
{
if (eregi("gif", $archivo) || eregi("jpg", $archivo) || eregi("png", $archivo))
{
echo '<img src="'.$directory."/".$archivo.'">'."\n";
}
}
$dirint->close();
The thing is that the code works ok if directory "books" is in the same directory as the php file, but I tried changing directory "books" to another directory, let´s say C:\Users\User and when I run the php file I get the following error:
Warning: dir(books): failed to open dir: No such file or directory in
C:\xampp\htdocs\PHP_Course\galeria.php on line 16
I´m beginning to believe that I can´t move the images directory, it has to be in the same directory as the php file.
Can anyone confirm this or I´m wrong and I´m not using the right path to point to the right directory?
Thanks in advance for any tips.
You can debug this by using the php function getcwd()
Can you try using
$dirint = dir(__DIR__ . DIRECTORY_SEPARATOR . $directory);
(also not that eregi() is deprecated and has been replaced by preg_match())
I´m beginning to believe that I can´t move the images directory, it
has to be in the same directory as the php file.
Can anyone confirm this?
No. Changing the directory should not be a problem, but you have to make sure it exists and your PHP code references the correct path.
If you move the directory books to another path, you have to adjust your variable:
$directory = 'C:\Users\User\books';
Keep in mind you have to change this if you ever plan to run the script on a different machine, especially on Linux. You might want to create a config file for the definition of this path.
I know there are similar topics,but when I tried using them, I still got the same error.
The problem is this:
Warning: mkdir() [function.mkdir]: No such file or directory in /home/... on line 30
I got this code:
$id = mysql_insert_id();
mkdir("memberFiles/$id", 0755);
What's the problem?I already have memberFiles folder.
Use the complete path to your file. You can use it with __DIR__ then you have the actual directory form your file.
mkdir(__DIR__."/memberFiles/$id", 0755);
for example. And you should check if the directory is available before you try create it.
if(!is_dir(__DIR__."/memberFiles/$id")) {
mkdir(__DIR__."/memberFiles/$id", 0755);
}
If you have PHP < 5.3 then its dirname(__FILE__) instead of __DIR__.
I am creating a medium size application.
This application consists of a lot of products.
Now these products have many images (one product can have 5 - 6 images)
To try and make some sort of ordering I want to create one folder for each product this folder contains all images that is bound to the product.
Now so far I have tried the following:
move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name']);
However when I try this I get the following error:
Warning (2): move_uploaded_file(/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png): failed to open stream: No such file or directory [APP/Controller/ImagesController.php, line 56]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php472ci6' to '/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png' [APP/Controller/ImagesController.php, line 56]
Now I am not a complete noob and know that this means that I am missing permissions to the folder.
However the problem is that if the folder does not exist (i.e this is the first time an image for that product is uploaded) then a new folder should be created.
My question is two parted.
Does this automatically create a new folder if it doesn't already exist?
How can I give permission to a newly created folder so that I avoid this problem?
[I] know that this means that i am missing permission to the folder.
Actually no =). The error message reads:
failed to open stream: No such file or directory
Which makes no reference to permissions the problrm is: the containing-folder you're trying to write to doesn't exist.
Does this automatically create a new folder if it doesn't already exist?
No.
How can i give permission to a newly created folder?
It's not necessary to do so - anything created will have the correct permissions to permit the webserver user to read the files. However first it's necessary to try and create a folder, which in the question isn't the case.
Using CakePHP, the Folder class can be used to do that:
App::uses('Folder', 'Utility');
$dir = new Folder('/path/to/folder', 2);
The second parameter is used to create a new folder if it doesn't exist. In the context of the question that means the code would look something like this:
function whatever() {
if ($this->request->data) {
...
$unused = new Folder(APP.'product_images/'.$product_id, true);
if (move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name'])) {
...
} else {
...
}
}
}
The folder APP/product_images should already exist, and must have permissions such that the webserver user (e.g. apache) can write to it otherwise it will not be possible to create the sub-folders/upload files. Assuming APP/product_images exists and the webserver user has permissions to write to it, there is no need to modify permissions of uploaded files - files created by a user are by default readable by that user.
Try this:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
1) Does this automaticly create a new folder if it doesnt already exist. => file_exists and mkdir
2) how can i give permission to a newly created folder so that i avoid this problem => 0777
No, it won't create the folder dynamically.
Use chmod() to change permissions.
For checking the existence of some folder you can use is_dir() too.
It's always a good idea too add the magic constant
__DIR__
to the file or directory path.
( __DIR__
gives out the path to the directory in which your script is located). In the errormessages "APP" is highlighted in a different colour than the path name. This could be a hint that the path cannot be located.
I have php web application named mywebsite inside c:\wamp\www\ .
This folder consist of featured/fashionWeek/database/Mysql.php file. I use dirname(FILE) function to get path of folder from root so that I can place the folder to any place in public_html folder of live server.
It works fine in my localhost. I have following code inside wamp\www\mywebsite\featured\fashionWeek\database\Mysql.php The include(dirname(FILE); works fine that returns this path: C:/wamp/www/mywebsite/featured/fashionWeek/database
<?php include(dirname(__FILE__).'/../config/Dbconfig.php') ;?>
But When I placed the mywebsite folder under public_html folder of the live server. It gives following errors:
Warning: include(/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php) [function.include]: failed to open stream: No such file or directory in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3
Fatal error: include() [function.include]: Failed opening required '/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3
To go backwards, i.e '..', you use the dirname function.
<?php
include(dirname(dirname(__FILE__)).'/config/Dbconfig.php');
Should work.
More
It will be tiresome to manage these eventually, so I suggest in your original file, you define a ROOT constant. That will contain the absolute path to the root of your project. It's simple enough.
defined('ROOT') or define('ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
https://stackoverflow.com/a/2152320/1528331
See if that helps. Also,
dirname(__FILE__) is the same as __DIR__
a Little Bit Flabby , but it may help you
if(!function_exists('getRootdir'))
{
function getRootdir($NFILE)
{
$filefitter="";
while(!file_exists($filefitter.$NFILE))
$filefitter="../".$filefitter;
return $filefitter;
}
}
use
like $rootpath=getRootdir("Root identifier folder / php file");
Well my PHP script generated an error with a hyperlink in it.
Does anyone know what's wrong?
PHP Warning: rename(./uploads/temp/00013/,./uploads/orders/39/) [<a href='function.rename'>function.rename</a>]: No such file or directory
update:
actual code in PHP
if(!file_exists('uploads/orders/')) {
mkdir('uploads/orders/'); // ensuring the orders folder exist
}
rename('uploads/temp/' . $u . '/', 'uploads/orders/' . $i . '/');
update:
Sorry, my fault. I coded to delete previous temp folder before this code execute. Thanks!
It seems that one (or both) of these directories don't exist:
uploads/temp/00013
uploads/orders/39
Have you checked that:
these directories exist?
Apache/PHP has permission to read/write in these directories?
Your current directory is really the parent directory of your "upload" directory?
When a computer tells you
No such file or directory
the first thing you should check is if the file/directory exists. This is not a random error message, it's given only in the specific situation when a file or directory you try to use does not exist.
In this case in particular, both ./uploads/temp/00013/ and ./uploads/orders/ have to exist. If orders doesn't exist it's not created for you.