从服务器路径获取URL [英] Get URL from server path
问题描述
我使用Codeigniter的文件上传库上传文件,并尝试将该URL插入数据库。 Codeigniter仅在使用 $ this-> upload-> data()
时提供server_path,这不能用于向用户显示图像。
I am uploading a file using Codeigniter's File Uploading Library and trying to insert the URL into the database. Codeigniter only supplies the server_path when using $this->upload->data()
, which isn't usable for displaying the image to users.
我通常可以做一些像 base_url('uploads)。 '/'。 $ data ['file_name']
但我将图像存储在每个帖子的文件夹中。
I could normally just do something like base_url('uploads) . '/' . $data['file_name']
but I am storing the images in a folder for each post.
例如, c $ c> C:/xampp/htdocs/site/uploads/32/image.jpg 作为full_path,如何将其转换为 http://mysite.com /uploads/32/image.jpg
For example, I am getting C:/xampp/htdocs/site/uploads/32/image.jpg
as the full_path, how can I convert this to http://mysite.com/uploads/32/image.jpg
唯一要注意的事情是使用正则表达式,但我觉得有是PHP函数还是Codeigniter函数来帮助这个?
The only thing that comes to mind is using a regular expression, but I feel like there has to be PHP function or Codeigniter function to help with this?
如何将服务器路径转换成正确的URL?
How can I convert the server path into the correct URL?
推荐答案
您可以使用$ _SERVER ['HTTP_HOST'];获取url。使用您的帖子的ID,您可以这样构建您的路径:
You can use $_SERVER['HTTP_HOST']; to get the url. Using the ID for your post, you can construct your path like so:
$url = "http://" . $_SERVER['HTTP_HOST'] . "/" . $id . "/" . basename($data['file_name']);
basename()函数返回路径中最后一个/之后的所有内容。
the basename() function returns everything after the last / in your path.
这篇关于从服务器路径获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!