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

25 php mysql database file-upload image-processing

嗨,我已经阅读了很多论坛和网站,告诉你如何将图像上传到服务器,我已经设法让这个工作,我可以上传一个文件到我的服务器,但存储文件名确实适用于我发现的以下示例我还需要创建一个允许将更多数据输入数据库的表单.我坚持这个,因为之前做了很多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      
Run Code Online (Sandbox Code Playgroud)

我想要的表单如下所示:

   <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>
Run Code Online (Sandbox Code Playgroud)

将Image上传到服务器的示例,即:

<?

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

"; }

}

?>
Run Code Online (Sandbox Code Playgroud)

我必须使用的示例表单是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

PS:图像文件打开以便写入.

Coo*_*uke 26

对于那些看起来像我在网上试图找到如何完成这项任务的人来说,这就是答案.将照片上传到服务器,文件名存储在mysql数据库中,以及数据库中需要的其他表单数据.如果有帮助,请告诉我.

首先是你需要的表格:

    <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>
              Band Position:
            </p>
            <input type="text" name="bandMember"/>
            <p>
              Please Upload a Photo of the Member 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! Maxium size of File is 35kb.
            </p>
            <p>
              Photo:
            </p>
            <input type="hidden" name="size" value="350000">
            <input type="file" name="photo"> 
            <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" name="upload" title="Add data to the Database" value="Add Member"/>
          </form>
Run Code Online (Sandbox Code Playgroud)

然后,此代码处理来自表单的数据:

   <?php

// This is the directory where images will be saved
$target = "your directory";
$target = $target . basename( $_FILES['photo']['name']);

// This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];


// Connects to your Database
mysqli_connect("yourhost", "username", "password") or die(mysqli_error()) ;
mysqli_select_db("dbName") or die(mysqli_error()) ;

// Writes the information to the database
mysqli_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;

// Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

// Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

// Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?> 
Run Code Online (Sandbox Code Playgroud)

代码从www.about.com编辑


Sam*_*son 0

如果您想在表单中输入更多数据,只需通过 $_POST 访问提交的数据即可。

如果你有

<input type="text" name="firstname" />
Run Code Online (Sandbox Code Playgroud)

你可以通过以下方式访问它

$firstname = $_POST["firstname"];
Run Code Online (Sandbox Code Playgroud)

然后您可以更新查询行以读取

mysql_query("INSERT INTO dbProfiles (photo,firstname)
             VALUES('{$filename}','{$firstname}')");
Run Code Online (Sandbox Code Playgroud)

注意:始终过滤和清理您的数据。