PHP Saving and outputting GD Lib image - relative and absolute paths -


i have php script generates image using gd lib, saves predefined location. , should output it.

my directory structure this:

www.example.com/projects/project-1/ 

inside project-1 directory have these directories:

- /imgs/ - /js/ - /css/ - /php/ 

the script using gd lib in /php/ config.php script constants defined. included in main script.

say have 2 constants:

define('save_path', '/projects/project-1/imgs/'); //the image not save - not work define('output_path', '/projects/project-1/imgs/'); //this works if there image in location 

i save image so:

imagejpeg($im, save_path.$name, 100); 

i following error:

failed open stream: no such file or directory in /public_html/projects/project-1/php/main.php 

is possible 1 constant works both saving , outputting?

i know can't have absolute save path like: http://www.example.com/projects/project-1/imgs/

and know can't have absolute output path like: /public_html/projects/project-1/imgs/

so elegant solution problem?

your issue due using absolute pathing in save_path. absolute path begins /, , relative path not. these work:

define('save_path', '../imgs/'); //path relative php script, assuming php file isn't being included path define('save_path', '/public_html/projects/project-1/imgs/'); 

but

in efforts make more versatile, instead following. set 2 constants, 1 base application directory, , 1 path image folder. latter usable in conjunction former save path, , usable on own output path:

//const instead of define little prettier, preference const application_path = '/projects/project-1'; const image_path = '/imgs/';  //save image - absolute path file location imagejpeg($im, application_path.image_path.$name, 100);  //echo image url - absolute path file url echo "<img src='".image_path.urlencode($name)."' />"; 

you need edit image_path make changes images reside appropriately affect both system directory , web url.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -