使用PHP将图像上传到服务器时,如何将文件名与其他信息一起存储在数据库中? [英] How to store file name in database, with other info while uploading image to server using PHP?

查看:13
本文介绍了使用PHP将图像上传到服务器时,如何将文件名与其他信息一起存储在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多论坛和网站,这些论坛和网站告诉您如何将图像上传到服务器,并且我已经设法使其正常工作,我可以将文件上传到我的服务器,但存储文件名确实适用于以下情况我发现的示例,我还需要创建一个表单,允许将更多数据输入到数据库中.我坚持这一点,因为以前做过很多 PHP.我已经尝试了不同的网站教程,但没有取得太大的成功,任何人都可以帮助我!我需要为我正在做的项目完成它.

Hi I have read many forums and websites that tell you how to upload an image to a server and I have managed to get this working, I can upload a file to a my server but storing the file name does work on the following example I found and I also need to create a form that allows more data to be entered to the database. I am stuck with this as a have done much PHP before. I have come to the end of trying different websites tutorials with no much success could anyone please help me! I need it done for a project I'm doing.

我基本上是在尝试制作一个 CMS,它允许用户上传乐队成员的照片并存储有关他们的信息,以便可以在网页上显示供公众查看.

I am basically trying to make a CMS that allows users to upload a photo of a band member and have information stored about them so that it can be displayed on a webpage for the public to view.

我的表看起来像这样:

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      

我想要的表格如下所示:

The form I want will look like this:

   <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>

上传图片到服务器的例子,就是这样:

The Example that uploads an Image to the server and only, that is this:

<?

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

"; }

}

?>

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

And the Examples form I have to use is this:

<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:图像文件已打开可写入.

PS: Images file is open for writing to.

推荐答案

这里是答案给那些看起来像我在整个网络上试图找出如何做的人这个任务.使用存储在 mysql 数据库中的文件名和数据库中所需的其他表单数据将照片上传到服务器. 如果有帮助,请告诉我.

Here is the answer for those of you looking like I did all over the web trying to find out how to do this task. Uploading a photo to a server with the file name stored in a mysql database and other form data you want in your Database. Please let me know if it helped.

首先是你需要的表格:

    <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>

然后这段代码处理你表单中的数据:

Then this code processes you data from the form:

   <?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.";
}
?> 

www.about.com

这篇关于使用PHP将图像上传到服务器时,如何将文件名与其他信息一起存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆