를 저장하는 방법 파일의 이름을 데이터베이스에서,다른 정보를 업로드하는 이미지를 사용하여 서버 PHP?

안녕하세요 저는 다양한 포럼을 읽고 웹사이트는 방법을 알려 주는 이미지를 업로드하려면 서버가 이동,나는 업로드할 수 있는 파일을 서버지를 저장하는 파일의 이름은 작업에 다음 예에서 나는 발견하고 내가 또한 필요로하는 형태를 만들 수 있는 이상의 데이터를 입력할 데이터베이스에 있습니다. 나는 나이로는 많 PHP 다. 제가 말하려고 다른 웹사이트 튜토리얼이 없는 많은 성공할 수 있 사람이 도와주세요! 나는 그것을 필요로 수행한 프로젝트 I'm 하고있다.

저는 기본적으로 만들려고 노력하 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:이미지 파일에 쓰기.

질문에 대한 의견 (2)

여기에응답당신의 사람들을 위해 찾고 했던 것처럼 모든 웹을 통해 알려고 노력하는 방법을 이렇게 작업입니다. 사진을 업로드하여 서버의 파일 이름으로 mysql 데이터베이스에 저장되고 다른 형태의 데이터에서 당신이 원하는 데이터베이스. 면 알려 주시기 바랍니 그것이 도움을 주었다.

첫째로 당신이 필요로하는 형태로:


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

            <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>

그런 다음 이 코드는 프로세스에서 당신에게 데이터 형태로 되어 있습니다.

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

해설 (2)

당신의 ID 를 각각의 사진 그래서 나의 제안은 당신이 이름을 바꿉니다. 예를 들어,당신은 이름 바꾸기에 날짜

<?php
 $date = getdate();
 $name .= $date[hours];
 $name .= $date[minutes];
 $name .= $date[seconds];
 $name .= $date[year];
 $name .= $date[mon];
 $name .= $date[mday];
?>

참고:don't 잊지 파일의 확장자는 파일 또는 당신을 생성할 수 있는 임의의 문자열을 사진,그러나 나는 권장하지 않습니다. 또한 것을 확인하도록 권장 파일 확장자를 업로드하기 전에 그 디렉토리에 있습니다.

<?php 
if ((($_FILES["photo"]["type"] == "image/jpeg")
            || ($_FILES["photo"]["type"] == "image/pjpg"))
            && ($_FILES["photo"]["size"] < 100000000))
            {
                move_uploaded_file($_FILES["photo"]["tmp_name"], $target.$name);

                if(mysql_query("your query"))
                {
                    //success handling
                }
                else 
                {
                    //failed handling
                }
            }
            else
            {
                //error handling
            }
?>

희망이 도움이 될 수 있습니다.

해설 (0)

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

            <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>

로 저장 addMember.php

<?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
mysql_connect("yourhost", "username", "password") or die(mysql_error()) ;
mysql_select_db("dbName") or die(mysql_error()) ;

//Writes the information to the database
mysql_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['photo']['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.";
}
?>

위 코드에서 하나의 작은 버그,내가는 버그가 수정되었습니다.

해설 (0)

하려는 경우에는 입력상의 데이터 형태로,당신은 단순히 액세스를 제출된 데이터를 통해$_POST.

이 있는 경우

<input type="text" name="firstname" />

당신은에 액세스

$firstname = $_POST["firstname"];

할 수 있습니다 쿼리를 업데이트라인을 읽

mysql_query("INSERT INTO dbProfiles (photo,firstname)
             VALUES('{$filename}','{$firstname}')");

참고:는 항상 필터를 삭제합니다.

해설 (1)

의 일부:

$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

";

지 않't make much sense,연결 안't 명$결과는 이름의 문제가 아닙 코딩을 하나입니다.

무엇이 문제를 코딩은 경우($결과),당신의 말은 경우에 연결할 수 있는 데이터베이스와 관계없이 삽입 쿼리가 실패하거나 성공할 것 출력이"이미지로 저장된 데이터베이스".

를 추가하려고 하시

$realresult = mysql_query("INSERT into dbProfiles (photo) VALUES('".$_FILES['filep']['name']."')");

을 변경하는 경우($결과)하$realresult

심의 쿼리가 실패하고,아마도 당신은 추가 열 또는 뭔가?

도 복사/붙여넣기 쿼리를 교체,".$이전 버전의 php['filep']['이름']."으로 테스트하고 그것을 실행하는 쿼리에는 브라우저과하는지 확인합니다.

해설 (1)

다음과 같이 문제를 방지 따옴표에서 파일 이름은,예를 들어,

"freddy's pic.jpg"

어떤에서 허용 가능한 몇 가지 운영 체제입니다.

기:

$pic=($_FILES['photo']['name']);

후:

$pic=(mysql_real_escape_string($_FILES['photo']['name']));
해설 (0)