I have created a mkdir function in my php webpage but it doesn't appear to be working.
Here it is:
mkdir("Game/" . $user . "/" . $name . ".actibuild", 0777, true);
user and name are defined above. Here's the snippet of code:
if (isset($_POST['name'])){
if (isset($_POST['desc'])){
$name = mysql_real_escape_string($_POST['name']);
$desc = mysql_real_escape_string($_POST['desc']);
$user = $check_user['Username'];
mkdir("Game/" . $user . "/" . $name . ".actibuild", 0777, true);
mysql_query("INSERT INTO `games`(`creator`, `name`, `desc`) VALUES ('$user', '$name', '$desc')");
header('Location: Games.php');
}
}
It is correctly running those queries into the database, but it isn't creating those directories.
Can you help?
check the current directory with:
echo __FILE__;
or
echo getcwd();
and build your path relative to this reference.
or
you can use chdir("/") to set the root directory as the current directory, then try to create your path.
Do you know where PHP executes? You have a relative system path (starting with Game). Chances are, your directory is getting created (if permissions allow), but in a location relative to the working directory, which is not necessarily the same place where your PHP script lives.
You using relative paths, it may be not the place you think directories are created but current directory/web-root/etc.
Try:
$path = "Game/" . $user . "/" . $name . ".actibuild";
is_writable('.') || die(realpath('.') . ' is not writable');
mkdir($path, 0777, true) || die(realpath($path).' directory not created');
print_r(realpath($path));
Related
Below I have left my code. It currently works in my development environment (localhost), but when I push the changes to my live server it seems like my php doesn't create the folder/file.
public static function saveImage($image, $name, $path = '')
{
$img_data = explode(',', $image);
$mime = explode(';', $img_data[0]);
$data = $img_data[1];
$extension = explode('/', $mime[0])[1];
if(!file_exists('../public/media/img/' . $path)){
mkdir('../public/media/img/' . $path, 0755);
echo('Test1');
}
echo('test2');
file_put_contents('../public/media/img/' . $path . $name . '.' . $extension, base64_decode($data));
return 'media/img/' . $path . $name . '.' . $extension;
}
Locally it will hit echo('test1') the first time, then it will only hit echo('test2'). When its on the server it always hits the echo('test1')
By default mkdir is not create a path recursively. An example if on your server you dont have a ../public/media folder, mkdir returns false and dont create a path.
To solve this pass a third parameter to mkdir as true:
mkdir('../public/media/img/' . $path, 0755, true);
Do yourself a favour and use absolute pathes...
You can use the constant __DIR__ to evaluate the folder in which the script actually resides.
Relative pathes are calculated from the current working directory, which can be different than __DIR__
so i can upload my photo from my Android app fine to /var/www/html/ProductPhotos but when i want to get the name of the Product and use that as the name of the new directory and image name then its not working. I create the new directory and /var/www/html/ProductPhotos with 777 permissions (even though its super bad) but for now its what i need. here's my PHP code:
<?php
$ProductAccountName = $_POST['ProductAccountName'];
$ProductName = $_POST['ProductName'];
$ProductImage = $_POST['EncodedImage'];
$NewDirectory = "/var/www/html/ProductPhotos/" . $ProductAccountName;
mkdir($NewDirectory, 0777, true);
//$DecodedProductImage = base64_decode("$ProductImage");
//$ProductName = $ProductName .".JPG";
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName, $ProductName . ".JPG", $DecodedProductImage);
?>
You're using a comma instead of a period. And you're missing a slash.
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName . "/" . $ProductName . ".JPG", $DecodedProductImage);`
See the file_put_contents docs.
You may want to be put into place some checks to make sure the user doesn't use relative paths(using ../ as part the ProductAccountName, for example). Just be careful of the user using this to do malicious things.
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);
}
Is it possible to make directory based on user input, i want to store their cookies and also create directory for them to access on the site.
User input are the below
$cid = $_POST['cid'];
$gid = $_POST['gid'];
Original of the code :
mkdir('/home/user/public_html/ref/', 0777 );
I want the code to exist like this :
mkdir('/home/user/public_html/ref/',$cid."_".$gid, 0777 );
Instead of , you should concat directory path with .
Just try with:
mkdir('/home/user/public_html/ref/' . $cid . "_" . $gid, 0777);
Try it:
mkdir('/home/user/public_html/ref/' . $cid . "_" . $gid, 0777 );
Like others have said before me use:
mkdir('/home/user/public_html/ref/' . $cid . "_" . $gid, 0777, true);
Notice the "true" 3rd variable.
This will allow mkdir() to create recursively any missing directories.
I'm attempting to try and debug the following code with the file_exists function. I've ran a var_dump on the avatar directory and it always returns as bool(false). I'm not sure why. I tested the code below and it gets to the file exists but it proves the if statement false everytime. Any thoughts? I have looked and the image is in the directory correctly.
$default_avatar = 'default.jpg';
$avatar_directory = base_url() . 'assets/globals/images/avatars/';
if (!is_null($user_data->avatar))
{
$avatar = $avatar_directory . $user_data->avatar;
if (file_exists($avatar))
{
$user_data->avatar = $avatar_directory . $user_data->avatar;
}
else
{
$user_data->avatar = $avatar_directory . $default_avatar;
}
}
else
{
$user_data->avatar = $default_avatar;
}
$default_avatar = 'default.jpg';
$avatar_directory = 'assets/globals/images/avatars/';
if (!is_null($user_data->avatar))
{
$avatar = $avatar_directory . $user_data->avatar;
if (file_exists(FCPATH . $avatar))
{
$user_data->avatar = base_url() . $avatar_directory . $user_data->avatar;
}
else
{
$user_data->avatar = base_url() . $avatar_directory . $default_avatar;
}
}
else
{
$user_data->avatar = $default_avatar;
}
from the name base_url seems like a function that will get a url like http://www.mysite.com, which will not work for doing local directory functions.
you need something like getcwd, or a full path
getcwd will get the current working directory (the directory where the initial script was executed from):
//If say script.php was exectued from /home/mysite/www
$avatar_directory = getcwd() . '/assets/globals/images/avatars/';
//$avatar_directory would be
/home/mysite/www/assets/globals/images/avatars/
Well this works both CLI and via Apache etc...:
$avatar_directory = substr(str_replace(pathinfo(__FILE__, PATHINFO_BASENAME), '', __FILE__), 0, -1) . '/assets/globals/images/avatars/'
The did returned is the one that the php file itself is in, not the root.
assuming you meant base_url() to point to the root of your project -
$file = __DIR__ . "/path/to/file.ext";
if (file_exists($file)) {
//...
}
Or some variation thereof. This also works:
__DIR__ . "/.."
it resolves to the parent directory of __DIR__.
see PHP's magic constants:
http://php.net/manual/en/language.constants.predefined.php
If you are looking for a remote resource - a file not located on your local filesystem - you have to change your php.ini to permit that. And it's probably not a good idea, this is not usually considered safe or secure. At all.
http://php.net/manual/en/features.remote-files.php
And note:
"This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir."
-- from http://php.net/manual/en/function.file-exists.php
-- edited to add relevant information based on a comment from OP.