2017-01-22 66 views
0

我已經開始學習PHP。我在許多文件中使用文件路徑。我希望使用它在配置文件中定義並在所有需要的文件中使用該變量。目前我在下面的文件中使用過它。從配置文件到其他文件的變量

function base64ToImageProfile($base64_code){ 

    $img_base64 = $base64_code; 
    //replace data:image/jpeg tag from base64 
    $img_base64 = str_replace('data:image/jpeg;base64,', '', $img_base64); 

    //replace data:image/png tag from base64 
    $img_base64 = str_replace('data:image/png;base64,', '', $img_base64); 

    //replace space with + from base64 
    $img_base64 = str_replace(' ', '+', $img_base64); 

    //decode base64 to image object 
    $decode_img = base64_decode($img_base64); 

    if (!$decode_img) { 
     return 'error'; 
    } 
    //create path for image 
    $file_path= 'userImage/'.uniqid().'.png'; 

    $success=file_put_contents($file_path,$decode_img); 
    //saves decoded image to specified path 
    if ($success) { 

     $file_path = 'www.example.com/myfolder/'.$file_path; 
    } 

    return $success ? $file_path:'error';  

} 

我想從配置文件中使用變量爲我的url位於它。我該怎麼做?

+0

獲得從'$ _SERVER'這些信息如果你硬編碼你在每次上安裝代碼的時間來改變它新服務器 – RiggsFolly

+0

@RiggsFolly這是很好的解決方案。你能告訴我如何在這一行中使用它嗎?「$ file_path ='www.example.com/myfolder/'.$file_path;」 ?謝謝 –

+0

'$ HowToBecomeADeveloper =(試驗+測試)' – RiggsFolly

回答

2

使用INI文件是一個不錯的和優雅的解決方案! PHP有一個本地函數來正確處理它。例如,它可以創建一個INI文件是這樣的:

app.ini

[application] 
app_name = My App 
app_email = [email protected] 
app_url = www.myapp.com.br 

[database] 
db_name  = mydatabase 
db_user  = myuser 
db_password = mypassword 

configLoader.php

$ini = parse_ini_file('app.ini'); 

然後你就可以方便地訪問使用的定義$ ini數組。

echo $ini['db_name']; // mydatabase 
echo $ini['db_user']; // myuser 
echo $ini['app_name']; // mypassword 

重要提示:出於安全原因,INI文件必須在一個非公開的文件夾