PHP - Convert File system path to URL

后端 未结 11 1509
野的像风
野的像风 2020-11-29 08:12

I often find that I have files in my projects that need to be accessed from the file system as well as the users browser. One example is uploading photos. I need access to t

11条回答
  •  悲&欢浪女
    2020-11-29 08:49

    Make it easy on yourself and just define the correct locations for both the filesystem and web folders and prepend the image filename with them.

    Somewhere, you'd declare:

    define('PATH_IMAGES_FS', '/var/www/example.com/uploads/');
    define('PATH_IMAGES_WEB', 'uploads/');
    

    Then you can just swap between paths depending on your need:

    $image_file = 'myphoto.jpg';
    
    $file = PATH_IMAGES_FS.$image_file;
    //-- stores: /var/www/example.com/uploads/myphoto.jpg
    
    print PATH_IMAGES_WEB.$image_file;
    //-- prints: uploads/myphoto.jpg
    

提交回复
热议问题