Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a local and live environment path

Tags:

php

I'm using a form to upload files into a specific upload folder. Now, in my local development environment, the local folder is harcoded in a variable like this

$destination = 'c:/public_html/discography/artwork/';

Once the script is completed and ready to be moved in the live environment, the upload folder will be

$destination = '/home/my_name/public_html/discography/artwork/';

My question is the following: Right now, both variables are hard coded. How do i make it so depending on the environment, the correct path is selected?

If possible, i'd like it to be in a define statement, so i can use it anywhere in my script:

define('UPLOAD_ARTWORK', ????);

local:

$destination = 'c:/public_html/discography/artwork/';
$move = move_uploaded_file($_FILES['artwork']['tmp_name'], $destination.$_FILES['artwork']['name']);

live:

$destination = '/home/my_name/public_html/discography/artwork/';
$move = move_uploaded_file($_FILES['artwork']['tmp_name'], $destination.$_FILES['artwork']['name']);

wishing for

define('UPLOAD_ARTWORK', ????);
$move = move_uploaded_file($_FILES['artwork']['tmp_name'], UPLOAD_ARTWORK.$_FILES['artwork']['name']);

Thanks

like image 507
Marco Avatar asked Dec 30 '25 18:12

Marco


1 Answers

Your on the right path. All you need is some way to determine whether the script is being run locally or on the live server. This could be a subdomain if you're using something like dev.example.com or an ip address ala 127.0.0.1. Then you could write it like this.

switch($_SERVER['HTTP_HOST']){
    case "dev.example.com":
        define('UPLOAD_ARTWORK','c:/public_html/discography/artwork/');
        break;
    default:
        define('UPLOAD_ARTWORK','/home/my_name/public_html/discography/artwork/');
}
$move = move_uploaded_file($_FILES['artwork']['tmp_name'], UPLOAD_ARTWORK.$_FILES['artwork']['name']);
like image 102
Justin Lucas Avatar answered Jan 02 '26 09:01

Justin Lucas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!