在使用PHP将图像上传到服务器时,如何在数据库中存储文件名以及其他信息?

嗨,我读过很多论坛和网站,它们告诉您如何将图像上载到服务器,并且设法将其上载,我可以将文件上载到服务器上,但是存储文件名确实适用于以下示例而且我还需要创建一个表单,以允许将更多数据输入数据库。我坚持这样做,因为之前已经做过很多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">

PS:图像文件已打开,可以写入。


MYYA
浏览 936回答 3
3回答

皈依舞

您为每张照片都有一个ID,所以我的建议是重命名照片。例如,您按日期重命名<?php&nbsp;$date = getdate();&nbsp;$name .= $date[hours];&nbsp;$name .= $date[minutes];&nbsp;$name .= $date[seconds];&nbsp;$name .= $date[year];&nbsp;$name .= $date[mon];&nbsp;$name .= $date[mday];?>注意:别忘了文件的文件扩展名,或者可以为照片生成随机字符串,但是我不建议这样做。我还建议您先检查文件扩展名,然后再将其上传到目录中。<?php&nbsp;if ((($_FILES["photo"]["type"] == "image/jpeg")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || ($_FILES["photo"]["type"] == "image/pjpg"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && ($_FILES["photo"]["size"] < 100000000))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move_uploaded_file($_FILES["photo"]["tmp_name"], $target.$name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mysql_query("your query"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //success handling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //failed handling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //error handling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }?>希望这会有所帮助。
打开App,查看更多内容
随时随地看视频慕课网APP