How to move a folder to another folder PHP? [duplicate] - php

I have two folders
myappdemo.com/VueGuides/services/iclean
myappdemo.com/VueGuides/services/pics
I need to move iclean folder into pics folder using PHP.

Use rename(). Note that if this runs on a web server, the web server user must have access to write to the target directory.
rename("oldpath", "newpath");
// in your case, assuming the script calling rename()
// resides in the directory above 'myappdemo.com'
rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean");
// Or use full absolute paths
rename("/path/myappdemo.com/VueGuides/services/iclean", "/path/myappdemo.com/VueGuides/services/pics/iclean");

There's a specific PHP function for it
http://php.net/manual/en/function.rename.php

If you are worried about SEO i recommend you using redirects 301 in your .htaccess.
That must be something like that:
RewriteRule ^/VueGuides/services/iclean http://myappdemo.com/VueGuides/services/pics [NS,R=301,L]

I required a different solution in my case as I was moving a sub folders contents into the parent folder. rename wouldn't work in my instance because the path was the same.
(For Linux based machines):
exec('mv '.$this->useFolder.'/'.$sub_folder.'/*'.' '.$this->useFolder);
This uses the inbuilt mv function through exec.

Related

Find filepath to public_html directory or it's equivalent using PHP

I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.

php paths differ between ajax use and php use

I just stumbled upon a (at least for me) new weird behavior of paths while using php functions with pure php or ajax calls.
If I use just php the path in the php function is like:
require_once('wp-content/themes/xxx/tcpdf/tcpdf.php');
but if I use the exact same function with an ajax call, the path needs to be like this to make it work:
require_once('../tcpdf/tcpdf.php');
Can you explain me why this is so? Thank you very much!
The reason that you are getting that behavior is because all the includes are occuring from the root of the wordpress install so you are saying do down the directory structure several layers before you get to the file. Breaking it down it does like this.
wp-content/ Go down one directory level from the file i'm in
themes/ Then go down into the themes directory
xxx/ Then go to the xxx directory
tcpdf/ Then go to the tcpdf directory
tcpdf.php This is the file you want
When you are doing the second include you are in a directory that shares a parent with wp-content/themes/xxx/tcpdf/ so what you are saying is
../ Go up one directory level
tcpdf/ Go into the tcpdf directory
tcpdf.php This is the file you want
I guess when you are using it without an AJAX call, you are including it from some other file, that can make the difference.
However I recommend storing the application root directory in a constant (e.g. ROOT) and then you include everything relative to ROOT.
Redefine the include path to set_include_path ( APP_ROOT ). By doing so, ALL includes/requires will be relative to the application root that you will define in the constant APP_ROOT.
PHP normally has the current directory as include path, which can lead to problems like this you're having.

How to locate public folder using PHP?

I am writing installation script in PHP and part of my application may be installed in the folder that is one level above the public folder. Usually, public folder is called "public_html" but that may not always be the case. Is there some easy way to locate the name or path of the root public folder using PHP?
Try $_SERVER['DOCUMENT_ROOT']. (documentation)
If you want to get the directory above it, you can use dirname.
Brad does have a point, though; your script may not have access to that directory.
$_SERVER['DOCUMENT_ROOT'] will provide the full path up to public_html and you can explode with slash "/" and get last index of the exploded array.
You can also use realpath with argument "../", if you don't know the actual file name. This will return you the full parent path of public_html.
Hope one of this two will work for you :)

How to move one directory to another directory?

I have two folders
myappdemo.com/VueGuides/services/iclean
myappdemo.com/VueGuides/services/pics
I need to move iclean folder into pics folder using PHP.
Use rename(). Note that if this runs on a web server, the web server user must have access to write to the target directory.
rename("oldpath", "newpath");
// in your case, assuming the script calling rename()
// resides in the directory above 'myappdemo.com'
rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean");
// Or use full absolute paths
rename("/path/myappdemo.com/VueGuides/services/iclean", "/path/myappdemo.com/VueGuides/services/pics/iclean");
There's a specific PHP function for it
http://php.net/manual/en/function.rename.php
If you are worried about SEO i recommend you using redirects 301 in your .htaccess.
That must be something like that:
RewriteRule ^/VueGuides/services/iclean http://myappdemo.com/VueGuides/services/pics [NS,R=301,L]
I required a different solution in my case as I was moving a sub folders contents into the parent folder. rename wouldn't work in my instance because the path was the same.
(For Linux based machines):
exec('mv '.$this->useFolder.'/'.$sub_folder.'/*'.' '.$this->useFolder);
This uses the inbuilt mv function through exec.

How to declare paths in PHP to allow easy app move to not rooted folder?

I have wamp setup on my windows box. Generally, when I bring a site down from the web, I create a folder inside my www folder for the site name. ex: c:\wamp\www\mysite. Once I have the folder, I copy down all the live files. The issue is that all the paths are then broken because my local folder isn't rooted.
What is the best way to setup paths so that if the site moves to a folder that isn't rooted, it will work easily?
I use a file (usually called something like config.php) to keep track of the root folder. My definitions (constants) look like this:
define('BASE_DIR','/wherever/whenever/');
define('LIB_DIR', BASE_DIR . 'lib/');
And then when you need to include a file
include LIB_DIR . 'aFile.php';
This would be something you do on a new site or if you have time to refactor your current site.
Create an include file, that has constants setup based upon whatever the root directory is... then in your code, use the constants you created to include files.
Also note, that when you are using directory "slashes", always use the build in constant DIRECTORY_SEPARATOR instead of hard coding it, this will allow you to go from WIndows to Linux seamlessly.
We use $_SERVER['DOCUMENT_ROOT'] to determine where we are in the filesystem and then simply append the folder name of our project to that. This works perfectly for us. You should always use a configuration.php where you define basic paths and URL's that may change when moving the project from one server/folder to another.
Option 1. Use <base href=""/> tag
Option 2. Use a config file, like #MattCan suggests
Option 3. Use a server environment variable, like #Bjorn suggests
Option 4. Create a virtual host on your apache, than you can create a domain who appoint exactly where are your app folder. Apache Doc here

Categories