PHP:mkdir() can't create folder - php

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__.

Related

PHP scandir doesn't work

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",

php mkdir via symlink warning

I want to make directory recursively by use of symlinks (softlinks) but I encouter with warning :
Warning: mkdir(): File exists in ...Path to php code... on line 21
The directory that i want to create is /vagrant/resources/page.
In the /var/www path I create a symlink named resources that links to /vagrant/resources directory and the php code is like below:
$directory = '/var/www/resources/page';
if(!file_exists($directory)){
mkdir($directory,0777,true);
}
The permissions to all directories inside /vagrant is set to 777.
Thanks.
As #arkascha mentioned your problem is not with symlinks but with the existence of the directory you are trying to make. which is a bit strange considering you have a fair condition around your mkdir command.
have try with is_dir() instead of file_exists()
I solved a similar problem with readlink. Also checking before if it is a link or not. After than the path can be used as expected.
$path = '/var/www/resources'
if (is_link($path)) {
$path = readlink($path);
}

PHP require_once error with relative path

I would like to test my php application in phpunit. My problem is the require_once doesn't find the file what I would like to test. I get this error:
Warning:
require_once(C:\MyProject\phpunit-tesztek\include/../include/form.php):
failed to open stream: No such file or directory in
C:\MyProject\phpunit-tesztek\include\FormTest.php on line 4
So it search the form.php file in include/../include/form.php what is wrong.
I used this code:
require_once 'PHPUnit/Autoload.php';
require_once(__DIR__.'/../include/form.php');
the Test file is in C:\MyProject\phpunit-tesztek\include\FormTest.php
and the file what I want to test is in: C:\MyProject\include\form.php
What is the problem?
The problem is the relative path. The __DIR__ uses the directory the file is in without a trailing separator. You are then changing the path with a relative path (/../) to go up one directory.
See the manual entry -> PHP Manual for __DIR__
__DIR__ will return C:\MyProject\phpunit-tesztek\include
You are looking to use the C:\MyProject\include\ path
__DIR__ . '/../../include/Form.php';
I normally use the dirname(__FILE__) myself, as it gets the current directory of the source code file (which is absolute) allowing my relative path to move from that location.
Try removing ../
require_once(DIR.'include/form.php');
Use magic constant DIR :
require_once(__DIR__ . '/../include/form.php');

PHP Make Directory

I am not sure why this code is breaking
mkdir("upload/".$username.'/'.$title.'/', 0700);
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/".$username.'/'.$title.'/' . $_FILES["file"]["name"]);
echo "Stored in: " ."upload/".$username.'/'.$title.'/' . $_FILES["file"]["name"];
$link = "upload/".$username.'/'.$title.'/' . $_FILES["file"]["name"];
Here are my errors
Warning: mkdir() [function.mkdir]: No such file or directory in /Volumes/shared/Digital/_Websites/_TEST/qpm/submit.php on line 101
Warning: move_uploaded_file(upload/test/test/Hand Over.docx) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Volumes/shared/Digital/_Websites/_TEST/qpm/submit.php on line 103
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/MAMP/tmp/php/phpYvo5b5' to 'upload/test/test/Hand Over.docx' in /Volumes/shared/Digital/_Websites/_TEST/qpm/submit.php on line 103
Stored in: upload/test/test/Hand Over.docx1 record added
I;m sure i had this working and now I have somehow broke it?
the echo stored in echos the correct string so not sure why the mkdir is failing, apologies if this a simple fix
There are a few things you should know about mkdir():
By default, it can only create a directory if the parent directory exists; it can create intermediate directories if you pass true as the third parameter.
Without an absolute path, it's hard to say where your directory will get created. You can either use a configuration file to store this or use a combination of __FILE__, __DIR__ and dirname() to derive it.
I would further advice never to use the value of $_FILES['file']['name'] directly to create files on your server; use tempnam(), optionally in combination with a sanitized version of the original file name.
first it is strongly recommended to use absolute path instead of relative path.because relative path may not be working well if you run this php script in an other path.
take this for eg:
test.php:/home/donald/test.php
<?php
mkdir("test_dir");
when pwd is "/home/donald/" you run "php test.php" it will make a "test_dir" in /home/donald/
when pwd is "/home/" you run "php donald/test.php" it will a make "test_dir" in /home/and you can also add true to mkdir() as the 3rd param to mkdir recursively or just use "exec('mkdir -p yourpath')" if it's allowed

php create directory

I am creating a directory and I am getting the error
Warning: chmod() [function.chmod]: No such file or directory in /home/www/public_html/console/pubs-add.php on line 104
The php file that is running the code is in www/console and the directory that I am trying to create is in www/images/gallery.
I have tried many variations of setting the path such as ../images/gallery or home/www but nothing seesm to work
define("PATH", "/www/images/gallery");
$dir = 'a1234';
$targetfilename = PATH . '/' . $dir;
if (!is_file($dir) && !is_dir($dir)) {
mkdir($dir); //create the directory
chmod($targetfilename, 0777); //make it writable
}
You mkdir command just uses $dir, which is just 'a1234' (in the current working directory). This will fail (and make the chmod fail too).
The solution: you probably want to prefix $dir with PATH...
Dear chmod() create some time problem. So i will suggest u that use this
mkdir("/path/to/my/dir", 0700);
if u want the created directory should be ready and wirtable then use this
mkdir("/path/to/my/dir", 0777);
The problem is that you cannot chmod a file that you haven't made. For that reason, I've changed the line
$task = chmod($targetfilename, 0777); //make it writable
to
$task = chmod($dir, 0755); //make folder writable
Tip: If you want a folder to be writable, chmod it to 755 and not 777. 777 is for files.
It makes the $dir in your current working directory, however, it doesn't mean that this equals your $targetfilename. I would say that you have to do mkdir($targetfilename) rather than mkdir($dir).

Categories