如何在使用PHP将图像上传到服务器时将文件名存储在数据库中,并带有其他信息?
2022-08-30 16:41:15
嗨,我已经阅读了许多论坛和网站,告诉您如何将图像上传到服务器,我已经设法使这一切正常,我可以将文件上传到我的服务器,但是存储文件名确实适用于我发现的以下示例,我还需要创建一个允许将更多数据输入到数据库中的表单。我坚持这一点,因为以前做过很多PHP。我已经结束了尝试不同网站的教程,但没有取得多大成功,任何人都可以帮我!我需要为我正在做的项目完成它。
我基本上正在尝试制作一个CMS,允许用户上传乐队成员的照片并存储有关他们的信息,以便它可以显示在网页上供公众查看。
我的表如下所示:
Field Type Null Default
id int(10) No
nameMember varchar(25) No
bandMember text No
photo varchar(30) No
aboutMember text No
otherBands text No
我想要的表单将如下所示:
<h1>Adding a new Band Member or Affiliate</h1>
<form method="post" action="addMember.php" enctype="multipart/form-data">
<p>
Please Enter the Band Members Name.
</p>
<p>
Band Member or Affiliates Name:
</p>
<input type="text" name="nameMember"/>
<p>
Please Enter the Band Members Position. Example:Drums.
</p>
<p>
Member's Position:
</p>
<input type="text" name="bandMember"/>
<p>
Please Upload a Photo in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten!
</p>
<p>
Photo:
</p>
<input type="file" name="filep" size=35 />
<p>
Please Enter any other information about the band member here.
</p>
<p>
Other Member Information:
</p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
<p>
Please Enter any other Bands the Member has been in.
</p>
<p>
Other Bands:
</p>
<input type="text" name="otherBands" size=30 />
<br/>
<br/>
<input TYPE="submit" title="Add data to the Database" value="Add Member"/>
</form>
将图像上载到服务器的示例,并且仅如下所示:
<?
if ($_POST["action"] == "Load")
{
$folder = "images/";
move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".$_FILES["filep"]["name"]);
echo "
<p align=center>File ".$_FILES["filep"]["name"]."loaded...";
$result = mysql_connect("localhost", "******", "*****") or die ("Could not save image name
Error: " . mysql_error());
mysql_select_db("project") or die("Could not select database");
mysql_query("INSERT into dbProfiles (photo) VALUES('".$_FILES['filep']['name']."')");
if($result) { echo "Image name saved into database
"; }
}
?>
我必须使用的示例表单是这样的:
<form action=addMember.php method=post enctype="multipart/form-data">
<table border="0" cellspacing="0" align=center cellpadding="3" bordercolor="#cccccc">
<tr>
<td>File:</td>
<td><input type="file" name="filep" size=45></td>
</tr>
<tr>
<td colspan=2><p align=center>
<input type=submit name=action value="Load">
</td>
</tr>
</table>
</form>
PS:图像文件已打开以进行写入。