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

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

问题描述

您好我已经阅读了许多论坛和网站告诉你如何上传图像到服务器,我已经设法得到这个工作,我可以上传文件到我的服务器,但存储文件名的工作在以下示例我发现,我还需要创建一个表单,允许更多的数据输入到数据库。我坚持这是一个做了很多PHP之前。我已经到了尝试不同的网站教程结束没有什么成功,任何人都可以帮助我!我需要它做一个项目我在做。



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






我的表格如下所示:

 字段类型Null默认值
id int(10)No
nameMember varchar(25)No
bandMember text No
photo varchar(30)No
关于成员文本No
otherBands text No

我想要的表单将如下所示: / p>

 < h1>添加新的乐队成员或会员< / h1> 
< form method =postaction =addMember.phpenctype =multipart / form-data>
< p>
请输入乐队成员名称。
< / p>
< p>
乐队成员或关系人姓名:
< / p>
< input type =textname =nameMember/>
< p>
请输入乐队成员位置。示例:鼓。
< / p>
< p>
会员位置:
< / p>
< input type =textname =bandMember/>
" p>
请以gif或jpeg格式上传照片。文件名应以成员名命名。如果相同的文件名被上传两次,它将被覆盖!
< / p>
< p>
照片:
< / p>
< input type =filename =filepsize = 35 />
< p>
请在这里输入乐队成员的任何其他信息。
< / p>
< p>
其他会员信息:
< / p>
< textarea rows =10cols =35name =aboutMember>
< / textarea>
< p>
请输入会员所在的任何其他乐队。
< / p>
< p>
其他乐队:
< / p>
< input type =textname =otherBandssize = 30 />
< br />
< br />
< input TYPE =submittitle =将数据添加到数据库value =添加成员/>
< / 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]。

$ result = mysql_connect(localhost,******,*****)或die(Could not save image name

错误:。mysql_error());

mysql_select_db(project)或die(Could not select database);
mysql_query(INSERT into dbProfiles(photo)VALUES('。$ _ FILES ['filep'] ['name']。
if($ result){echo图像名称保存到数据库

; }

}

?>

我必须使用的示例表格是:

 < form action = addMember.php method = post enctype =multipart / form-data> 
< table border =0cellspacing =0align = center cellpadding =3bordercolor =#cccccc>
< tr>
< td>文件:< / td>
< td>< input type =filename =filepsize = 45>< / td>
< / tr>
< tr>
< td colspan = 2>< p align = center>
< input type = submit name = action value =Load>
< / td>
< / tr>
< / table>
< / form>

PS:图片文件打开以供写入。

解决方案

这里是 web试图找出如何做这个任务。将照片上传到服务器,并将文件名存储在mysql数据库中,并在数据库中指定其他表单数据。请让我知道是否有帮助。



首先您需要的表单:

 < form method = postaction =addMember.phpenctype =multipart / form-data> 
< p>
请输入乐队成员名称。
< / p>
< p>
乐队成员或关系人姓名:
< / p>
< input type =textname =nameMember/>
< p>
请输入乐队成员位置。示例:鼓。
< / p>
< p>
乐队位置:
< / p>
< input type =textname =bandMember/>
< p>
请以gif或jpeg格式上传会员的照片。文件名应以成员名命名。如果相同的文件名被上传两次,它将被覆盖!文件的大小为35kb。
< / p>
< p>
照片:
< / p>
< input type =hiddenname =sizevalue =350000>
< input type =filename =photo>
< p>
请在这里输入乐队成员的任何其他信息。
< / p>
< p>
其他会员信息:
< / p>
< textarea rows =10cols =35name =aboutMember>
< / textarea>
< p>
请输入会员所在的任何其他乐队。
< / p>
< p>
其他乐队:
< / p>
< input type =textname =otherBandssize = 30 />
< br />
< br />
< input TYPE =submitname =uploadtitle =将数据添加到数据库value =添加成员/>
< / form>然后,此代码将通过以下形式处理您的数据:








$ b < b

 <?php 

//这是保存图片的目录
$ target =your directory;
$ target = $ target。 basename($ _FILES ['photo'] ['name']);

//从
$ name获取所有其他信息$ name = $ _ POST ['nameMember'];
$ bandMember = $ _ POST ['bandMember'];
$ pic =($ _ FILES ['photo'] ['name']);
$ about = $ _ POST ['aboutMember'];
$ bands = $ _ POST ['otherBands'];


//连接到你的数据库
mysqli_connect(yourhost,username,password)或者die mysqli_error
mysqli_select_db(dbName)或die(mysqli_error());

//将信息写入数据库
mysqli_query(INSERT INTO tableName(nameMember,bandMember,photo,aboutMember,otherBands)
VALUES('$ name','$ bandMember','$ pic','$ about','$ bands'));

//将照片写入服务器
if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],$ target))
{

//告诉你如果它的所有ok
echo文件。 basename($ _FILES ['uploaded file'] ['name'])。 已上传,您的信息已添加到目录;
}
else {

//如果不是
就会显示和错误echo抱歉,上传文件时出现问题。
}
?>

www.about.com


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.

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.


My Table looks like this:

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: Images file is open for writing to.

解决方案

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.

Firstly the form you need:

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

Code edited from www.about.com

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

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