Create a folder in the /root directory - php

I've tried everything online but I couldn't find a real solution, I have a php file that in local on my computer works perfectly, instead when I load it on my seerver it doesn't create the folder.
the php contains this string to create a new folder:
shell_exec("mkdir /root/users/$username");
I guess that maybe it's a problem of permission, I can just create folders in the var/www folder, dI tried a lot of different way to set my permission but I can't figure it out.

You should use mkdir to create folder or chmod to change permissions functions.
for example:
mkdir('newFolder', 775);
or you want to create on some path. Currently you are in /home/username/public_html/project/
mkdir('subproject', 775); // it will create in `project` folder.
if you want to create with some root like you asked in your question.
mkdir('/root/users/'.$username, 775);
I am sure it will be fix your issue and also a standard way to create.

Related

How to create a folder in a network different from root using php

Does anyone know how to create a folder in a network using php? I saw many comments and discussions about creating folder. But most of them talking about creating a folder in the root folder using mkdir().
What I need is to create a folder outside that. Lets say I want to create a folder in the ip location \\192.11.11.111\TEMP\Folder. But when I tried the following code
$structure = '\\192.11.11.111\TEMP\Folder';
mkdir ($structure);
It is producing an error Warning: mkdir(): No such file or directory in C:\MAMP\htdocs\
I also tried by using a letter instead of ip.
For Eg: z:\TEMP\Folder
But also not able to create folder. Mentioned location has full right to read and write. So issue is not due to permission also.
Can someone help?
I am able to create a folder by doing following way
$structure="//192.11.11.111/TEMP/Folder";
mkdir($structure, 0, true);
So the issue was due to the usage of wrong slashes.

OPENSHIFT - How do I change the document root?

Hi here I was openshift novice users, where before I used the cpanel for my hosting, and I want to switch to openshift, that I ask is how do I change the document root?, if previously on cpanel is Home/public_html/thelifestylelist.com to Home/public_html/thelifestylelist.com/public on openshift? (my index php is on public directory)
Hopefully the picture below can clarify my question.
Thank you.
Sorry i used external link for my image, need at least 15 reputation to attach image. .
This is the order OpenShift search for root directory
IF php/ dir exists THEN DocumentRoot=php/
ELSE IF public/ dir exists THEN DocumentRoot=public/
ELSE IF public_html/ dir exists THEN DocumentRoot=public_html/
ELSE IF web/ dir exists THEN DocumentRoot=web/
ELSE IF www/ dir exists THEN DocumentRoot=www/
ELSE DocumentRoot=/
So you just add the any of this folders.
According to this blog post, there are multiple document roots you can use for your PHP project (or static html project), you can read it here: https://www.openshift.com/blogs/openshift-online-march-2014-release-blog. Since you are using open shift, it would probably be best to just use the document root they provide you by using git to download/upload your files to your gear. You might review this page to help you get started: https://www.openshift.com/get-started

move_uploaded_file and relative path to destination in public_html in home directory?

I'm using OpenSuse 12.2 now. To learn and test PHP code I use public_html directory in my home folder: /home/wojtek/public_html/ I access project files in the following way:
http://localhost/~wojtek/projects/foo/bar.php
But I have not clue how to make move_uploaded_file to work. The server root is /srv/www. I set upload_tmp_dir to /srv/www/tmp (I have really no idea where it should go).
When I set destination of move_upladed_file to /srv/www/images, files landed there. But I'd like to make use of public_html folder as I'm keeping all my files there.
For example having:
public_html/projects/foo/
public_html/projects/foo/bar.php
public_html/projects/foo/images/baz.jpg
Can I somehow use the relative destination ('images/') of move_uploaded_file in the bar.php? So that I can access images relatively in IMG tag like from bar.php?
Sorry if it sounds a bit chaotic but I'm new to PHP.
No you wont be able to access the /srv/www/images folder from the /home/wojtek/public_html/projects/foo/bar.php web page.
Id suggest either
1) setting the move_uploaded_file to /home/wojtek/public_html/projects/foo/images
or
2) using a symbolic link to access the images folder from your public_html folder:
cd /home/wojtek/public_html/projects/foo
ln -s /srv/www/images/
and then it will appear that the images are stored in /home/wojtek/public_html/projects/foo/images, but they will actually still be here /srv/www/images
Its possible that you may run into trouble with permissions using this method however

codeigniter creating a directory

How do I create a directory using codeigniter? Basically what I am trying to do is allow user to upload files and then create a directory on the fly for storing these file could anyone point me on how could I create a directory using codeigniter
Thanks
All efforts will be appreciated
I have run the following codes in windows which works perfect for me-
<?php
$path = "uploads/product";
if(!is_dir($path)) //create the folder if it's not already exists
{
mkdir($path,0755,TRUE);
}
?>
Here 0755 is permission of the folder to be created. 755 means you can do anything with the file or directory, and other users can read and execute it but not alter it. Suitable for programs and directories you want to make publicly available.
Use mkdir
mkdir("/path/to/my/dir");

How do I copy files repository to new folder with PHP

I have a folder named "repository" in my admin folders. This folder holds 2 files: index.html and content.php. When a user creates a new page the php creates a new folder specified by the user then will need to copy the two files into that folder while leaving them in the repository.
copy(file,dest) does not work.
rename(file,dest) moves the files to the new folder but I lose them in the repository.
How do I copy the files in one folder to the new folder without losing the files in the original folder?
$dest = '../'.$menuLocation.'/'.$pageName;
$file1= "repository/index.html";
$file2= "repository/content.php";
mkdir($dest,0777);
rename($file1,$dest.'/index.html');
rename($file2,$dest.'/content.php');
$menuLocation and $pageName are supplied by the user. The files are there, file_exists returns back true. Also, the directory is created with no issues. rename() also works I just lose the files in repository.
For anyone hunting for the solution to this:
when using copy() in php you also need to include the file name.
copy(orginalfile,destination_with_filename);
for example:
wrong:
copy('/temp/sports/basketball.jpg','/images/sports/')
Correct:
copy('/temp/sports/basketball.jpg','/images/sports/basketball.jpg')
Use copy(). Make sure you capture the return value so that your program knows if it worked or not. Also check permission of the files and directory to confirm that the user executing the PHP script is able to create the new file in the place you specified. You might want use is_writable() on the directory to confirm these assumptions.

Categories