PHP - 在数据库中存储图像链接并在不同页面上显示(Localhost)

时间:2021-11-12 23:28:26

SO! uhh, its not letting me post normally, sorry about the formatting im new! and confused ha! if anyone can help me with the below request that would be amazing! I've been struggling for the past 3 days!!

所以!呃,它没有让我正常发布,抱歉格式化新!哈哈!如果有人能帮我提供下面的请求,这将是惊人的!过去3天我一直在努力!

I'm going to try keep this as simple as possible to understand.

我将尝试尽可能简单地理解这一点。

Heres what I'm trying to do:

继承人我正在做的事情:

Users can have profile pictures, they upload the image, the image goes to their file (named after their username) and the link of the image gets stored in the database.

用户可以拥有个人资料图片,他们上传图像,图像进入他们的文件(以他们的用户名命名),图像的链接存储在数据库中。

My problem:

I'm running on localhost, and for some reason I can't display the picture on all pages. (e.g. works on Profile page, doesn't work in index page.) Profile page link = sessions/settings/profile Index = sessions/index User profiles = sessions/users/

我在localhost上运行,由于某种原因,我无法在所有页面上显示图片。 (例如,在“个人资料”页面上工作,在索引页面中不起作用。)个人资料页面链接=会话/设置/个人资料索引=会话/索引用户个人资料=会话/用户/

The code I'm using to store this is:

我用来存储它的代码是:

    $uploadDir = '../../users/' .$username. "/"; 

(This work only on profile)

(这项工作仅适用于个人资料)

What I've tried:

我尝试过的:

    $uploadDir = $_SERVER['SERVER_NAME'] . 'dir/sessions/users/'  .$username.  "/";

(this doesn't execute) And a few other obvious methods.

(这不执行)和其他一些明显的方法。

Sorry about it being so broad with the question, hope I explained it properly!

很抱歉这个问题如此广泛,希望我能正确解释!

2 个解决方案

#1


0  

You need to save only filename and get directory from your code, because it could change frequently.

您需要只保存文件名并从代码中获取目录,因为它可能会经常更改。

So, get filename and put in database, only, i.e, username.png and, when getting users' avatar, concatenate full url to it, including HOST, and folders. Something like this:

因此,获取文件名并放入数据库,只有,即username.png,并且,当获取用户的头像时,连接完整的URL,包括HOST和文件夹。像这样的东西:

$user_avatar_link = "http://$_SERVER[HTTP_HOST]/path/to/user_avatar/" + username.png;

Note: Check if $_SERVER[HTTP_HOST] has last backslash. If you're using HTTPS protocol, then create if statement using $_SERVER['HTTPS']

注意:检查$ _SERVER [HTTP_HOST]是否有最后一个反斜杠。如果您使用的是HTTPS协议,则使用$ _SERVER ['HTTPS']创建if语句

Full URL is the best practice, instead of using relative dir.

完整URL是最佳做法,而不是使用相对目录。

#2


0  

Try

$uploadDir = "$_SERVER['HTTP_HOST']" . '/users/' .$username. "/"; 

#1


0  

You need to save only filename and get directory from your code, because it could change frequently.

您需要只保存文件名并从代码中获取目录,因为它可能会经常更改。

So, get filename and put in database, only, i.e, username.png and, when getting users' avatar, concatenate full url to it, including HOST, and folders. Something like this:

因此,获取文件名并放入数据库,只有,即username.png,并且,当获取用户的头像时,连接完整的URL,包括HOST和文件夹。像这样的东西:

$user_avatar_link = "http://$_SERVER[HTTP_HOST]/path/to/user_avatar/" + username.png;

Note: Check if $_SERVER[HTTP_HOST] has last backslash. If you're using HTTPS protocol, then create if statement using $_SERVER['HTTPS']

注意:检查$ _SERVER [HTTP_HOST]是否有最后一个反斜杠。如果您使用的是HTTPS协议,则使用$ _SERVER ['HTTPS']创建if语句

Full URL is the best practice, instead of using relative dir.

完整URL是最佳做法,而不是使用相对目录。

#2


0  

Try

$uploadDir = "$_SERVER['HTTP_HOST']" . '/users/' .$username. "/";