I want to be able to load a different config file based on the domain name using purely PHP.
Currently my config file loads using the following
# Look for a config.php in the /themes/themeName/ folder
if ( ! defined('MULTI') && file_exists($tmp = WWW_ROOT . '/themes/' . $CONFIG['theme'] . '/config.php') ) {
But i need to be able to add if domain is domain1.com load config from /theme/domain1 or if it is domain2.com load from /theme/domain2 and if it is not either of these domains loads from /theme/default.
This is used when a domain name is parked ontop of another domain so the content is all the same except for the config files.
I was thinking something along the lines of
if ($_SERVER['HTTP_HOST']="domain1.com")
else if ($_SERVER['HTTP_HOST']="domain2.com")
else
Just not sure how to properly format that or it that would be the best way to do it.
$themes_path=array(
'domain1.com'=>'foldername1',
'domain2.com'=>'foldername2'
);
if (isset($_SERVER['HTTP_HOST']) && file_exists(WWW_ROOT . '/themes/' . $themes_path[$_SERVER['HTTP_HOST']] . '/config.php'){
include(WWW_ROOT . '/themes/' . $themes_path[$_SERVER['HTTP_HOST']] . '/config.php');
}
i think direct include $_SERVER['HTTP_HOST'] not very good.
if (isset($_SERVER['HTTP_HOST']) && file_exists(WWW_ROOT . '/themes/' . $_SERVER['HTTP_HOST'] . '/config.php')
include(WWW_ROOT . '/themes/' . $_SERVER['HTTP_HOST'] . '/config.php');
else
include(WWW_ROOT . '/themes/default/config.php');
Related
i am trying to catch the relative path to a file to create a share link.
From my httpdocs folder on the webserver, my file is here:
jack/single/uploads/folder1/image.jpg
The var $dir . '/' . $file gives me this output:
uploads/folder1/image.jpg
realpath($dir . '/' . $file gives me this output:
/home/vhosts/example.com/subdomains/develop3/httpdocs/jack/single/uploads/folder1/image.jpg
What i want to achieve is this output:
`http://develop3.example.com/jack/single/uploads/folder1/image.jpg`
How can i achieve this, so that i can create a share link?
You could use preg_replace on the output of realpath to replace everything up to httpdocs with your site's URL:
echo preg_replace('#^' . preg_quote($_SERVER['DOCUMENT_ROOT']) . '[\\\\/]#', "{$_SERVER['HTTP_HOST']}/", realpath('test6.php')) . "\n";
You can use $_SERVER['HTTP_HOST'] to get you site's name:
function get_link($to_file){
return "https://".$_SERVER['HTTP_HOST']."/jack/single/".$to_file;
}
echo get_link($dir . '/' . $file);
Change the function's url parts according to your project (do you use https or http? Will /jack/single always be the parent folder of uploads?)
my script file path is:
C:\xampp\htdocs\wordpress\wp-content\plugins\test\test.php
I need to run code from this path that will move images from path:
C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04
to:
C:\xampp\htdocs\wordpress\wp-content\uploads\images
My problem is that I have no idea how to force "rename" go two directories back in path.
I was trying something like:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
error I'm getting atm (I've changed my folder permissions, so maybe it is something with path?):
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied
. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
edit rename code:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
$destPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/images');
/*$srcDir = opendir($srcPath);*/
echo $srcPath ;
sleep(1);
rename($srcPath, $destPath);
From looking at it, it looks wrong
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
it should be
$srcPath = realpath(dirname(__FILE__) . '../..' . '/uploads/2017/04/obrazek.png');
You have an extra / slash at the beginning.
It seems that you're in a Windows machine, so you're using the wrong type of slashes: /../..' . '/uploads/2017/04/obrazek.png'
You could try to replace them with backslashes:\..\..\uploads\2017\04\obrazek.png'
Or you could try something like this:
$path = substr($srcPath, 0, strrpos($srcPath, '\20'));
// $path now contaits: 'C:\xampp\htdocs\wordpress\wp-content\uploads';
// Then you can do:
$srcPath = $path . '\images';
Regarding the warning error:
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
It seems that you try to rename a file to directory, so probably you forgot to append the file name to the new path.
$srcPath contains a file. $destPath contains a directory, but it should contain the file name.
I've finally after many hours.. find out how to fix it so here you go:
$srcPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\2017\04') . '\obrazek2.png');
$destPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\images') . '\obrazek2.png');
rename ($srcPath , $destPath );
The key was adding file name . '\obrazek2.png' after dirname (), because if filename is inside dirname () and it is destination path in rename, then it returns empty... :)
Not sure if I'm clear enough, because of my English, but it works and hopes it will help someone.
i want to add images to images/ in localhost :
What modifications should i add to this php script so i could add a folder for each id ( i want to post different images with the same Id in the same folder )
Below is the php file for uploading images :
<?php
$name=$_POST["id"];
$image=$_POST["image"];
$decodedImage=base64_decode("$image");
file_put_contents("images/" . $name . "/" .$name . ".JPG" , $decodedImage);
?>
is there any other way to add multiple images without Repeating the java code many times ?
Please make sure that directory exists before uploading file to it.
$name=$_POST["id"];
$image=$_POST["image"];
if(!file_exists ( "images/" . $name )){
mkdir("images/" . $name, 755);
}
$decodedImage=base64_decode("$image");
file_put_contents("images/" . $name . "/" .$name . ".JPG" , $decodedImage);
I search this question in Stackoverflow but I don't understand the answer. Sorry.
Well, I've a html upload form which handle by Php.
Following is my php upload script :
if($image["name"] != "")
{
//$path = PATH . DS . "uploads" . DS . "products" . DS . $id;
$path = "../../uploads" . DS . "products" . DS . $id;
if(!is_dir($path))
{
mkdir($path);
}
chmod($path, 0755);
//move_uploaded_file($image["tmp_name"], $path . DS . $image["name"]);
move_uploaded_file($image["tmp_name"], $path . DS . $uploadImage);//exit;
}
Now I'm trying to upload same image to Another website folder. Like : www.mysitename/folder/finalfolder. in the same time with Php.
Is there any way to upload it another website ?
The other server will need a script to handle the request. You can then POST that data to the other server's script using a CURL request. (See the link posted by #Andrey Volk)
Or, as suggested here: FTP will work too, if you have that level of access to the server.
Is there any way to call require_once with some "case insensitive flag" ?
In windows it's okay, but linux is case sensitive. Is there any way to override ?
Thanks
Sure, load
strtolower($className . ".php")
and name your files in lowercase.
Regardless of how you try to load your files, only the lowercase version will ever be loaded.
Just include some where else in you header.php or some other common file.,
<?php
$filename = basename($_SERVER['SCRIPT_FILENAME']);
$request = basename($_SERVER['SCRIPT_NAME']);
if($filename != $request)
die('Case of filename and request do not match!');
?>
I think this may help you resolve your problem. also refer the following location https://superuser.com/questions/431342/linux-both-case-sensitive-and-case-insensitive-and-always-inconvenient
You can use this every time you are loading you auto load the appropriate classes. yu have to change the directories depends on you project. you can you the echo or print_r to print what classes are loaded every time when you are calling something. also all you class names must ot have the same format for example, className.class.php e.g Dashboard.class.php, Category.class.php. you can use
ucwords to make the first letter capital.
function __autoload($className)
{
if (file_exists(__DIR__ . '/../library/' . strtolower($className) . '.class.php'))
{
require_once(__DIR__ . '/../library/' . strtolower($className) . '.class.php');
}
else if (file_exists(__DIR__ . '/../application/controllers/' . strtolower($className) . '.php'))
{
require_once(__DIR__ . '/../application/controllers/' . strtolower($className) . '.php');
}
else if (file_exists(__DIR__ . '/../application/models/' . strtolower($className) . '.php'))
{
require_once(__DIR__ . '/../application/models/' . strtolower($className) . '.php');
}
}