I am wondering what I am doing wrong. I'm inside of PATH and I want to create a folder inside of PATH. I want to check if the folder already exists and, if not, create one. Getting the name of the folder from an input field with name of "dirname".
if (isset($_POST['createDir'])) {
//get value of inputfield
$dir = $_POST['dirname'];
//set the target path ??
$targetfilename = PATH . '/' . $dir;
if (!file_exists($dir)) {
mkdir($dir); //create the directory
chmod($targetfilename, 0777); //make it writable
}
}
It might be a good idea to make sure that the directory you are handling is indeed a directory. This code works... edit as you please.
define("PATH", "/home/born05/htdocs/swish_s/Swish");
$test = "set";
$_POST["dirname"] = "test";
if (isset($test)) {
//get value of inputfield
$dir = $_POST['dirname'];
//set the target path ??
$targetfilename = PATH . '/' . $dir;
if (!is_file($dir) && !is_dir($dir)) {
mkdir($dir); //create the directory
chmod($targetfilename, 0777); //make it writable
}
else
{
echo "{$dir} exists and is a valid dir";
}
Good luck!
Edited: comment was a good hint ;)
You have to use
!is_dir($dir)
instead of
!file_exists($dir)
it's not a file, it's a directory!
Good luck!
You can use is_dir().
#codeworxx file_exists can be used to check a directory as well..
http://www.php.net/manual/en/function.file-exists.php
Related
<?php
if (isset($_POST['filename']) && isset($_POST['editorpassword']) && isset($_POST['roomname'])) {
$dir = $_POST['filename']; // This must match the "name" of your input
$path = "evo/" . $dir;
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
}
?>
I have this script where I'm trying to create a new folder. The script itself is ran inside of a folder called /evo and by using this code, it creates the folder in there. Where it needs to go is ../../creative however even if I try and use
$path = "./rooms/creative/" . $dir;
or something to that effect it creates it with the base folder as evo so it appears at:
../evo/rooms/creative (creating the folders that don't exist there with it as it should)
I'm just unsure what to write in for the path on where I need it created to find the right location.
Simplest solution is to remove the "evo" in $path = "evo/" . $dir;
I am using mkdir like so
mkdir('somePath\\' . $this->name. '-' . $this->generateRandomString(), 0777, true);
The output can be something like
C:\xampp\htdocs\someFolder\templates\generated\Nick-ycolYWzdin
So, I append a name and random string as the folder name. Problem is, I now need to use PHP to put a file in this folder.
Is there any way to get the path of the folder I just created, including the folder name (with the name and generated string)?
Thanks
store the mkdir parameter in a variable prior to calling the mkdir function.
$path = 'somePath\\' . $this->name. '-' . $this->generateRandomString();
mkdir($path, 0777, true);
/*
Other stuff happens
*/
move_uploaded_file($file, $path);
You should store the path in a variableand pass it to mkdir function
$new_path = ''somePath\\' . $this->name. '-' . $this->generateRandomString()';
if (mkdir($new_path)) {
copy($file, $new_path."/".$file);
}
There are all these resources for recursively looping through sub directories, but I haven't found ONE that shows how to do the opposite.
This is what I want to do...
<?php
// get the current working directory
// start a loop
// check if a certain file exists in the current directory
// if not, set current directory as parent directory
// end loop
So, in other words, I'm searching for a VERY specific file in the current directory, if it doesn't exist there, check it's parent, then it's parent, etc.
Everything I've tried just feels ugly to me. Hopefully someone has an elegant solution to this.
Thanks!
Try to create a recursive function like this
function getSomeFile($path) {
if(file_exists($path) {
return file_get_contents($path);
}
else {
return getSomeFile("../" . $path);
}
}
The easiest way to do this would be using ../ this will move you to the folder above. You can then get a file/folder list for that directory. Don't forget that if you check children of the directory above you then you're checking your siblings. If you just want to go straight up the tree then you can simply keep stepping up a directory until you hit root or as far as you are permitted to go.
<?php
$dir = '.';
while ($dir != '/'){
if (file_exists($dir.'/'. $filename)) {
echo 'found it!';
break;
} else {
echo 'Changing directory' . "\n";
$dir = chdir('..');
}
}
?>
Modified mavili 's code:
function findParentDirWithFile( $file = false ) {
if ( empty($file) ) { return false; }
$dir = '.';
while ($dir != '/') {
if (file_exists($dir.'/'. $file)) {
echo 'found it!';
return $dir . '/' . $file;
break;
} else {
echo 'Changing directory' . "\n";
chdir('..');
$dir = getcwd();
}
}
}
I have a script which displays the images present on a folder.
E.g. Script path (from HTDOCS):
/admin/global/thisscript.php
E.g. Pictures folder path (from HTDOCS):
/public/test/images/
Now I need to print under each image the path of the file starting from the HTDOCS directory.
E.g. if I have:
$picture1 = /WEB/mysite/htdocs/public/test/images/picture1.png
$picture1name = basename($picture1)
I'd like to print
print "/public/test/images/".$picture1name
having
/public/test/images
in a variable which is automatically update if picture1.png path changes.
If I use
$_SERVER['PHP_SELF']
it returns the path of the file I'm displaing. Also
__FILE__
__DIR__
basename
dirname
are not working for me.
How can I get the path?
Thank you
perhaps $_SERVER['DOCUMENT_ROOT']
edit:
Something like:
$path = "/WEB/mysite/htdocs/public/test/images/picture1.png";
$path = dirname(str_replace($_SERVER['DOCUMENT_ROOT'], '', $path));
Not sure if this could help but could you explode the path result you have and build it back from the HTDOCS entry? something like:
$fullpath = /WEB/mysite/htdocs/public/test/images/picture1.png
$pathbits = explode("/", $fullpath);
$newpath = "";
$foundstart = false;
foreach($pathbits as $key => $value){
if ($value == "htdocs" || $foundstart == true) {
$foundstart = true;
$newpath = $newpath + "/" + $value;
}
}
I am making a intranet customer manager in PHP. For each customer a directory is created for the shop to add files into. What my script is supposed do is if no directory exists create it, if it does exists dont create it.
What is actually happening is if the directory already exists I am getting the following error :
Warning: mkdir() [function.mkdir]: File exists in C:\server2go\server2go\htdocs\customermgr\administrator\components\com_chronoforms\form_actions\custo m_code\custom_code.php(18) : eval()'d code on line 14
So what is happening it is trying to create it anyway, even though the if statement should stop it ?, im confused on what I am doing wrong :-S .
<?php
$customerID = $_GET['cfid'];
$directory = "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
$thisdir = getcwd();
mkdir($thisdir ."/customer-files/$customerID" , 0777); }
?>
Replace:
if(file_exists($directory) && is_dir($directory)) {
with:
$thisdir = getcwd();
if(file_exists($thisdir.$directory) && is_dir($thisdir.$directory)) {
or better:
<?php
$customerID = $_GET['cfid'];
$directory = "./customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
mkdir($directory , 0777); }
?>
Just took a short look but i would try this:
$directory = $thisdir . "/customer-files/$customerID";
and remove $thisdir from mkdir();
also you should move your $thisdir before the $directory declaration
The function file_exists() does not use relative paths, where is_dir() can. So instead, use the common denominator and pass an absolute path to these functions. Additionally you can move the call to getcwd() into the $directory assignment and reuse $directory later for creating the directory.
<?php
$customerID = $_GET['cfid'];
// Get full path to directory
$directory = getcwd() . "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
// Do nothing
}
else {
// Directory doesn't exist, make it
mkdir($directory , 0777); }
}
?>