PHP - Convert File system path to URL

后端 未结 11 1488
野的像风
野的像风 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:51

    IMHO such automation is really error prone. You're far better off using some explicit path helpers (eg. one for uploads, one for user pics, etc) or just encapsulate for example an uploaded file with a class.

    // Some "pseudo code"
    $file = UploadedFile::copy($_FILES['foo']);
    $file->getPath(); // /var/www/example.org/uploads/foo.ext
    $file->getUri();  // http://example.org/uploads/foo.ext
    

提交回复
热议问题