php 在数据库mysql php中存储和检索图像路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33086311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing and Retrieving image path in database mysql php
提问by Judy
I am developing a website using PHP, mysql and phpmyadmin. I have some database that I what want to retrieve it in html using PHP and mysql. I want to know how I can store and retrieve image paths from database and display it. I saw on the internet that there is a way, such as using the filesystem to store and retrieve images or making my laptop a server. what is the best way and how? I would be grateful for any help, Thanks.
我正在使用 PHP、mysql 和 phpmyadmin 开发一个网站。我有一些数据库,我想使用 PHP 和 mysql 在 html 中检索它。我想知道如何从数据库中存储和检索图像路径并显示它。我在互联网上看到有一种方法,例如使用文件系统来存储和检索图像或使我的笔记本电脑成为服务器。最好的方法是什么?如有任何帮助,我将不胜感激,谢谢。
回答by Nana Partykar
Since, you didn't described your question properly or didn't provided any code. So, i assumed your question in my way and posted my answer. Follow the step.
因为,您没有正确描述您的问题或没有提供任何代码。所以,我以我的方式假设了你的问题并发布了我的答案。按照步骤操作。
1) For Uploading Image
1) 上传图片
<form method='POST' action='UploadImage.php' enctype="multipart/form-data">
<input type='file' name='UploadImage'>
<input type='submit' value="submit">
</form>
Suppose your Project Folder Name Is : MyProject (Where all project files are present), Make one folder inside "MyProject" Folder namely "MyUploadImages" Now,
假设您的项目文件夹名称是:MyProject(所有项目文件都存在),在“MyProject”文件夹中创建一个文件夹,即“MyUploadImages”现在,
UploadImage.php
上传图片.php
<?php
include('connect.php'); // Do Database Connection in this file (create a file namely connect.php inside MyProject Folder)
extract($_POST);
$UploadedFileName=$_FILES['UploadImage']['name'];
if($UploadedFileName!='')
{
$upload_directory = "MyUploadImages/"; //This is the folder which you created just now
$TargetPath=time().$UploadedFileName;
if(move_uploaded_file($_FILES['files']['tmp_name'], $upload_directory.$TargetPath)){
$QueryInsertFile="INSERT INTO TableName SET ImageColumnName='$TargetPath'";
// Write Mysql Query Here to insert this $QueryInsertFile .
}
}
?>
Now, In your Database Table, you can find ImageColumnName that image path is set as MyUploadImages/1417Flower.jpg
现在,在您的数据库表中,您可以找到 ImageColumnName 图像路径设置为 MyUploadImages/1417Flower.jpg
2) Retreiving Image from database
2) 从数据库中检索图像
AnyPage.php
AnyPage.php
<?
$Query="SELECT * FROM TableName";
// Write mysql query to fetch $Query
store that ImageColumnName value to any variable say $MyPhoto.
?>
<img src="<?echo $MyPhoto;?>">
回答by singe batteur
or making my laptop a server>> I dont get that... do you have a server and php, and mysql server ?
或者让我的笔记本电脑成为服务器>> 我不明白……你有服务器和 php 以及 mysql 服务器吗?
As said Nirajan, the good way to do that is to store the path in DB, and the file itself in the folder you want. You will then use the field with the path to display your image :
正如 Nirajan 所说,这样做的好方法是将路径存储在 DB 中,并将文件本身存储在您想要的文件夹中。然后,您将使用带有路径的字段来显示您的图像:
for $image in $imagesList // *pseudo code*
<img src="$image->getPath()">
endfor
A quick search on the web will help you to find out the "real code" to do that !
在网络上快速搜索将帮助您找到“真正的代码”来做到这一点!