简体   繁体   English

MySQL数据库存储图像的URL和PHP页面显示吗?

[英]MySQL Database store image url and php page display it?

I was wondering if there's any way I could store an image URL in a database with a unique numeric id, and a php page display the image by getting data (the image URL) from the database and displaying it to the user on the image.php page? 我想知道是否有什么方法可以将图像URL存储在具有唯一数字ID的数据库中,而php页面通过从数据库获取数据(图像URL)并将其显示给用户来显示图像。 PHP页面?

An example of this would be: - Having the image URL http://www.example.com/images/image10.jpg (where this URL would be stored in the database, alongside an id of 10) - Having an image.php page which retrieves the image URL from the database and displays the image on the same page (the image.php page) (and doesn't redirect to the image URL) - an example result being image.php?id=10 where the image.php file would source the image URL from the database with an image id of 10 (using my example: www.example.com/images/image10.jpg) and display it (so it would be like viewing the image as if you were viewing www.example.com/images/image10.jpg but instead the URL would be http://www.example.com/image.php?id=10 ) 例如:-具有图片URL http://www.example.com/images/image10.jpg (此URL将存储在数据库中,ID为10)-具有image.php页面,该页面从数据库中检索图像URL并在同一页面(image.php页面)上显示图像(并且不会重定向到图像URL)-示例结果为image.php?id = 10,其中图像.php文件将从图像ID为10的数据库中获取图像URL(使用我的示例:www.example.com/images/image10.jpg)并显示它(因此就像查看图像一样,查看www.example.com/images/image10.jpg,但URL将会是http://www.example.com/image.php?id=10

Is there any way to do this? 有什么办法吗? I'm new to php and MySQL so not really good with code - trying to experiment :) 我是php和MySQL的新手,因此对代码的了解不是很好-尝试进行实验:)

Thanks in advance! 提前致谢!

Edit: I think people are getting confused by my bad description (sorry abot that). 编辑:我认为人们对我的不好的描述感到困惑(抱歉,是这样)。 I've noticed in your answers that you have included the actual image URL in your code - I dont understand why though. 我在您的回答中注意到您在代码中包含了实际的图片网址-尽管我不明白为什么。

EXAMPLE of how I'd like this to work: 我希望如何工作的示例:

My table would have these feilds: img_id img_url 我的桌子上有这些领域:img_id img_url

The image.php file would GET the img_url FROM the table using the id on the end of the image.php url. image.php文件将使用image.php网址末尾的ID从表中获取img_url。 FOR EXAMPLE: - image.php?id=10 - the image.php file would use the id (which is 10), and display the image by retreiving the img_url (which would be: www.example.com/images/image10.jpg) from the table and displaying it - the image id would be img_id in the table. 例如:-image.php?id = 10-image.php文件将使用ID(为10),并通过获取img_url(将为www.example.com/images/image10)显示图像。 jpg)并显示它-图像ID在表中为img_id。 - image.php?id=12 - the image.php file would use the id (which is 12) and display the image by retreiving the img_url (which would be: www.example.com/images/image12.jpg) from the table and displaying it - the image id would be img_id in the table. -image.php?id = 12-image.php文件将使用ID(为12)并通过从以下位置获取img_url(即为:www.example.com/images/image12.jpg)显示图像。表并显示它-图像ID在表中为img_id。

You can try this: 您可以尝试以下方法:

//File: image.php?id=10 
//you query   
$img = 'http://example.com/sample.jpg'; // Image path from database
$getInfo = getimagesize($img);
header('Content-type: ' . $getInfo['mime']);
readfile($img);

I think what you need is this : 我认为您需要的是:

$image_path="http://example.com/image10.jpg";' 'mysql_query("INSERT INTO table VALUES ('',$image_path)");

in the php have this : 在PHP中有这个:

$id=$_GET['id'];if (isset($_GET['id'])){' '$result = mysql_query("SELECT path FROM table WHERE id=$id");' '$row = mysql_fetch_row($result);' '$path=$row[0];echo "Image number ".$id." is <img src='".$path."'>';}

It all depends on your code organisation, let me explain my self, right now in my mind i can think of two ways of organisation: 这完全取决于您的代码组织,让我解释一下我的自我,现在在我看来,我可以想到两种组织方法:

First, using a folder containing all images, with the same name + number for each image; 首先,使用包含所有图像的文件夹,每个图像具有相同的名称和编号; eg: images/img1.jpg - images/img2.jpg - images/img3.jpg etc 例如:images / img1.jpg-images / img2.jpg-images / img3.jpg等

Using this method you'll not have to use even database, just within your page you go for this code, exemple for page http://exemple.com/images.php?id=10 使用这种方法,您甚至不必使用数据库,只需在页面中查找该代码,例如页面http://exemple.com/images.php?id=10

$id = (isset($_GET["id"]))? $_GET["id"] : NULL; //Get the ID

Then use the method given by user3410107 然后使用user3410107给出的方法

$img = "http://example.com/img$id.jpg"; // Image path
$getInfo = getimagesize($img);
header('Content-type: ' . $getInfo['mime']);
readfile($img);

Second method is indeed using database, in this case there should be at least 2 columns within db, a unique ID + Images path, in this case you'll go for: 第二种方法确实是使用数据库,在这种情况下,db中至少应有2列,一个唯一的ID + Images路径,在这种情况下,您将使用:

$query = mysql_query("SELECT id, imagePath FROM table WHERE id = $id");
while($data = mysql_fetch_assoc($query)){
$img = $data["imagePath"];
}

Once you get the image path, the same process as before: 获得图像路径后,与之前相同的过程:

$getInfo = getimagesize($img);
header('Content-type: ' . $getInfo['mime']);
readfile($img);

For security reasons add this line after $id variable 出于安全原因,在$ id变量后添加此行

$id = (isset($_GET["id"]))? $_GET["id"] : NULL; //Get the ID
$id = mysql_real_escape_string($id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM