php created in the root directory - php

I use account
dev_hq
I want to create a folder in the root called medias.
I use the command
$medias = 'medias';
$path = './root/'.$medias;
if(!is_dir($path)){mkdir($path, 0777, true); }
but the directory is created in
/var/www/public/root/medias
=> I want to create folders in
/home/dev_hq/
and link
/home/dev_hq/root/medias

What are you are doing is absolutely wrong. Please try this code
$medias = 'medias';
$path = '../../root/'.$medias;
if(!is_dir($path)){mkdir($path, 0777, true); }
Hope this helps you

Related

phpSPO library - How to copy subfolders in Sharepoint

I am using the phpSPO library for sharepoint: https://github.com/vgrem/phpSPO
I am able to copy a folder along with it's files using:
$credentials = new ClientCredential("MyCredentialsGoHere","MyCredentialsGoHere");
$ctx = (new ClientContext("https://MyURL.sharepoint.com/enquiries"))->withCredentials($credentials);
$sourceFolder = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents/Project Templates");
$targetFolder = $sourceFolder->copyTo("Shared Documents/".$test, true)->executeQuery();
However it does not copy any subfolders.
I assume that I have to iterate through the subfolders of my source directory manually doing a copy for each one to the new target directory.
My starting point for this was to list the subfolders in a SharePoint folder:
$credentials = new ClientCredential("MyCredentialsGoHere","MyCredentialsGoHere");
$ctx = (new ClientContext("https://MyURL.sharepoint.com/enquiries"))->withCredentials($credentials);
$sourceFolder = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents/Project Templates");
$subfolders = $sourceFolder->getFolders()->executeQuery();
foreach ($subfolders as $folder) {
print_r($folder);
}
However this doe not work as I expected (it produces no output) and my resources are exhausted.
Any pointers to help me solve the issue or an example of a finished solution would be very helpful.
This will loop the folders in a folder:
$parentpath = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents/Project Templates");
$folders = $parentpath->getFolders();
$ctx->load($folders);
$ctx->executeQuery();
foreach ($folders->getData() as $folder) {
$display = $folder->getProperty("ServerRelativeUrl");
echo ("File name: ".$display."<br/>");
}

mkdir() function: No such file or directory

I have the code:
$path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook/user/".$userid."/coverphoto/";
if(!file_exists($path_directory) && !is_dir($path_directory)){
mkdir($path_directory, true);
and it is telling me Warning: mkdir(): No such file or directory
and I do not know what to do.
Help is very much appreciated
You should use mkdir() like this
mkdir($path_directory, 0777, true);
FYI: https://www.php.net/manual/en/function.mkdir.php
And you may use below code to make sure you have enough permission first.
$path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook";
mkdir($path_directory);
Try this
First check if the user has his own directory
$user_path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook/user/".$userid;
if(!file_exists($user_path_directory) && !is_dir($user_path_directory)) {
mkdir($path_directory, true);
}
If the user has his own directory, lets create his coverphoto directory.
$path_directory = $user_path_directory."/coverphoto/"
if(!file_exists($path_directory) && !is_dir($path_directory)) {
mkdir($path_directory, true);
}

mkdir() not creating folder on the website

my php code is not creating the final folders which are (profile,post and cover) when hosted online but creates when hosted locally.Here is the code please help
<?php
if(isset($_POST['file']) && ($_POST['file']=='Upload'))
{
$path = "fh_users/Male/".$user."/Profile/";
$path2 = "fh_users/Male/".$user."/Post/";
$path3 = "fh_users/Male/".$user."/Cover/";
mkdir($path, 0, true );
mkdir($path2, 0, true);
mkdir($path3, 0, true);
$img_name=$_FILES['file']['name'];
$img_tmp_name=$_FILES['file']['tmp_name'];
$prod_img_path=$img_name;
move_uploaded_file($img_tmp_name,"fh_users/Male/".$user."/Profile/".$prod_img_path);
mysql_query("insert into user_profile_pic(user_id,image) values('$userid','$img_name')");
header("location:Secret_Question1.php");
}
?>
the second parameter of the mkdir() function is the permissions.
using mkdir($path, 0, true ); you create a new folder with 000 permissions (ie no permissions for anybody)
you should use mkdir($path, 0750, true ); instead which will give full permissions to the owner, read/execute permissions for group users and nothing for unknown users
The execute permission on a directory allows the affected user to enter the directory, and access files and directories inside

php mkdir folder tree from array

I'm trying to create a folder tree from an array, taken from a string.
$folders = str_split(564);
564 can actually be any number. The goal is to create a folder structure like /5/6/4
I've managed to create all folders in a single location, using code inspired from another thread -
for ($i=0;$i<count($folders);$i++) {
for ($j=0;$j<count($folders[$i]);$j++) {
$path .= $folders[$i][$j] . "/";
mkdir("$path");
}
unset($path);
}
but this way I get all folders in the same containing path.
Furthermore, how can I create these folders in a specific location on disk? Not that familiar with advanced php, sorry :(
Thank you.
This is pretty simple.
Do a for each loop through the folder array and create a string which appends on each loop the next sub-folder:
<?php
$folders = str_split(564);
$pathToCreateFolder = '';
foreach($folders as $folder) {
$pathToCreateFolder .= DIRECTORY_SEPARATOR . $folder;
mkdir($folder);
}
You may also add the base path, where the folders should be created to initial $pathToCreateFolder.
Here you'll find a demo: http://codepad.org/aUerytTd
Or you do it as Michael mentioned in comments, with just one line:
mkdir(implode(DIRECTORY_SEPARATOR, $folders), 0777, TRUE);
The TRUE flag allows mkdir to create folders recursivley. And the implode put the directory parts together like 5/6/4. The DIRECTORY_SEPARATOR is a PHP constant for the slash (/) on unix machines or backslash (\) on windows.
Why not just do:
<?php
$directories = str_split(564);
$path = implode(DIRECTORY_SEPARATOR, $directories);
mkdir($path, 0777, true);
Don't know what you're really trying to do, but here are some hints.
There are recursive mkdir:
if(!file_exists($dir)) // check if directory is not created
{
#mkdir($dir, 0755, true); // create it recursively
}
Path you want can be made in two function calls and prefixed by some start path:
$path = 'some/path/to/cache';
$cache_node_id = 4515;
$path = $path.'/'.join('/', str_split($cache_node_id));
Resulting path can be used to create folder with the code above
So here we come to a pair of functions/methods
function getPath($node_id, $path = 'default_path')
{
return $path.'/'.join('/', str_split($node_id))
}
function createPath($node_id, $path = 'default_path');
{
$path = getPath($node_id, $path);
if(!file_exists($path)) // check if directory is not created
{
#mkdir($path, 0755, true); // create it recursively
}
}
With these you can easily create such folders everywhere you desire and get them by your number.
As mentioned earlier, the solution I got from a friend was
$folders = str_split(564);
mkdir(implode('/',$folders),0777,true);
Also, to add a location defined in a variable, I used
$folders = str_split($idimg);
mkdir($path_defined_earlier. implode('/',$folders),0777,true);
So thanks for all the answers, seems like this was the correct way to handle this.
Now the issue is that I need to the created path, so how can I store it in a variable? Sorry if this breaches any rules, if I need to create a new thread I'll do it...

How to check if a folder exists before creating it in laravel?

I need to know if a folder exists before creating it, this is because I store pictures inside and I fear that the pictures are deleted if overwrite the folder.
The code I have to create a folder is as follows
$path = public_path().'/images';
File::makeDirectory($path, $mode = 0777, true, true);
how can I do it?
See: file_exists()
Usage:
if (!file_exists($path)) {
// path does not exist
}
In Laravel:
if(!File::exists($path)) {
// path does not exist
}
Note: In Laravel $path start from public folder, so if you want to check 'public/assets' folder the $path = 'assets'
With Laravel you can use:
$path = public_path().'/images';
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
By the way, you can also put subfolders as argument in a Laravel path helper function, just like this:
$path = public_path('images/');
You can also call this method of File facade:
File::ensureDirectoryExists('/path/to/your/folder')
which creates a folder if it does not exist and if exists, then does nothing
In Laravel 5.x/6 you can do it with Storage Facade:
use Illuminate\Support\Facades\Storage;
$path = "path/to/folder/";
if(!Storage::exists($path)){
Storage::makeDirectory($path);
}
Way -1 :
if(!is_dir($backupLoc)) {
mkdir($backupLoc, 0755, true);
}
Way -2 :
if (!file_exists($backupLoc)) {
mkdir($backupLoc, 0755, true);
}
Way -3 :
if(!File::exists($backupLoc)) {
File::makeDirectory($backupLoc, 0755, true, true);
}
Do not forget to use use Illuminate\Support\Facades\File;
Way -4 :
if(!File::exists($backupLoc)) {
Storage::makeDirectory($backupLoc, 0755, true, true);
}
In this way you have to put the configuration first in config folder
filesystems.php . [Not recommended unless you are using external disks]
The recommended way is to use
if (!File::exists($path))
{
}
See the source code
If you look at the code, it's calling file_exists()
I normally create random folders inside the images for each file this helps a bit in encrypting urls and thus public will find it hardr to view your files by simply typing the url to your directory.
// Check if Logo is uploaded and file in random folder name -
if (Input::hasFile('whatever_logo'))
{
$destinationPath = 'uploads/images/' .str_random(8).'/';
$file = Input::file('whatever_logo');
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$savedPath = $destinationPath . $filename;
$this->whatever->logo = $savedPath;
$this->whatever->save();
}
// otherwise NULL the logo field in DB table.
else
{
$this->whatever->logo = NULL;
$this->whatever->save();
}
This is what works great for me
if(!File::exists($storageDir)){
File::makeDirectory($storageDir, 0755, true, true);
$img->save('Filename.'.png',90);
}

Categories