2014-11-06 3 views
0

먼저이 포럼에 대한 몇 가지 주제를 찾았지만 내 페이지에서 제대로 표시하지 못하는 것 같습니다 ... 그래서 제가 묻는 것입니다.mysql에서 html 브레이크를 검색하는 방법

누군가가 웹 페이지에 설명이있는 사진을 업로드 할 수있는 웹 페이지 용 이미지 업로드 스크립트를 만들려고합니다. 지금까지 업로드가 잘되었고 데이터베이스에 저장되어 표시되었지만 문제는 mysql에서 텍스트를 가져올 때 textarea의 텍스트가 브레이크를 표시하지 않는다는 것입니다 ...

어떻게 할 수 있습니까? 이걸 고쳐?

은 upload.php로

<?php 
 
// Start a session for error reporting 
 
session_start(); 
 
    
 
// Call our connection file 
 
require("includes/conn.php"); 
 
    
 
// Check to see if the type of file uploaded is a valid image type 
 
function is_valid_type($file) 
 
{ 
 
    // This is an array that holds all the valid image MIME types 
 
    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif"); 
 
    
 
    if (in_array($file['type'], $valid_types)) 
 
     return 1; 
 
    return 0; 
 
} 
 
    
 
// Just a short function that prints out the contents of an array in a manner that's easy to read 
 
// I used this function during debugging but it serves no purpose at run time for this example 
 
function showContents($array) 
 
{ 
 
    echo "<pre>"; 
 
    print_r($array); 
 
    echo "</pre>"; 
 
} 
 
    
 
// Set some constants 
 
    
 
// This variable is the path to the image folder where all the images are going to be stored 
 
// Note that there is a trailing forward slash 
 
$TARGET_PATH = "content/uploads/"; 
 
    
 
// Get our POSTed variables 
 
$fname = $_POST['fname']; 
 
$lname = $_POST['lname']; 
 
$image = $_FILES['image']; 
 
    
 
// Sanitize our inputs 
 
$fname = mysql_real_escape_string($fname); 
 
$lname = mysql_real_escape_string($lname); 
 
$image['name'] = mysql_real_escape_string($image['name']); 
 
    
 
// Build our target path full string. This is where the file will be moved do 
 
// i.e. images/picture.jpg 
 
$TARGET_PATH .= $image['name']; 
 

 
// Make sure all the fields from the form have inputs 
 
if ($fname == "" || $lname == "" || $image['name'] == "") 
 
{ 
 
    $_SESSION['error'] = "All fields are required"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 

 
// Check to make sure that our file is actually an image 
 
// You check the file type instead of the extension because the extension can easily be faked 
 
if (!is_valid_type($image)) 
 
{ 
 
    $_SESSION['error'] = "You must upload a jpeg, gif, or bmp"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 

 
// Here we check to see if a file with that name already exists 
 
// You could get past filename problems by appending a timestamp to the filename and then continuing 
 
if (file_exists($TARGET_PATH)) 
 
{ 
 
    $_SESSION['error'] = "A file with that name already exists"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 
    
 
// Lets attempt to move the file from its temporary directory to its new home 
 
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) 
 
{ 
 
    // NOTE: This is where a lot of people make mistakes. 
 
    // We are *not* putting the image into the database; we are putting a reference to the file's location on the server 
 
    $sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')"; 
 
    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); 
 
    header("Location: images.php"); 
 
    exit; 
 
} 
 
else 
 
{ 
 
    // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to 
 
    // Make sure you chmod the directory to be writeable 
 
    $_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 
?>

이며,이 텍스트 영역에 입력 할 때 discription 및 브레이크와 이미지를 표시하는 데 필요한 PHP입니다 :

<?php 
 
// Get our database connector 
 
require("includes/conn.php"); 
 
?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 
    
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
    <head> 
 
     <title>Dream in code tutorial - List of Images</title> 
 
    </head> 
 
    
 
    <body> 
 
     
 
     <div> 
 
    
 
      <?php 
 
       // Grab the data from our people table 
 

 
       $sql = "select * from people"; 
 
       $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
 
    
 
       while ($row = mysql_fetch_assoc($result)) 
 
       { 
 
        echo "<div class=\"picture\">"; 
 
        echo "<p>"; 
 
    
 
        // Note that we are building our src string using the filename from the database 
 
        echo "<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" /><br />"; 
 
        echo $row['fname'] . " " . "<br />" . "<br />" . $row['lname'] . "<br />"; 
 
        echo "</p>"; 
 
        echo "</div>"; 
 
\t \t \t \t } 
 
    
 
    
 
      ?> 
 
     
 
     </div> 
 
    </body> 
 
</html>

내가하고 nl2br 만의 속삭임을 사용하여 같은 몇 가지를 시도했습니다

<?php 
 

 
// Start a session for displaying any form errors 
 

 
session_start(); 
 
?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 

 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
    <head> 
 
     <title>Dream in code tutorial</title> 
 

 
     <style type="text/css"> 
 
      label 
 
      { 
 
       float: left; 
 
       text-align: right; 
 
       margin-right: 10px; 
 
       width: 100px; 
 
       color: black; 
 
      } 
 

 
      #submit 
 
      { 
 
       float: left; 
 
       margin-top: 5px; 
 
       position: relative; 
 
       left: 110px; 
 
      } 
 
      #error 
 
      { 
 
       color: red; 
 
       font-weight: bold; 
 
       font-size: 16pt; 
 
      } 
 

 
     </style> 
 
    </head> 
 

 
    <body> 
 

 
     <div> 
 

 
       <?php 
 
       if (isset($_SESSION['error'])) 
 
       { 
 
        echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>"; 
 
        unset($_SESSION['error']); 
 
       } 
 

 
       ?> 
 

 
       <form action="upload.php" method="post" enctype="multipart/form-data"> 
 
       <p> 
 
        <label>Merk</label> 
 
        <input type="text" name="fname" size="50"/><br /> 
 

 
        <label>beschrijving</label> 
 
        <textarea name="lname" style="width:250px;height:150px;"></textarea><br /> 
 
        
 
        <label>Upload afbeelding</label> 
 
        <input type="file" name="image" /><br /> 
 
        
 
        <input type="hidden" name="MAX_FILE_SIZE" value="5000000" /> 
 
        <input type="submit" id="submit" value="Upload" /> 
 
       </p> 
 
       </form> 
 
     </div> 
 
    </body> 
 
</html>

:

및 certenty에 대한3210, 이것은 당신이 discription를 입력하고 이미지를 업로드의하자의 index.php입니다 작동하지 않는 것 같습니다 (아마도 내가 잘못 사용하기 때문입니다 ...)

답변

0

매우 간단한 해결책이 있다고 생각합니다. 첫 번째 변경이 : 여기에

$lname = mysql_real_escape_string($lname); 

: 모든 줄 바꿈 문자를 가지고 <BR> 문자로를 켠 다음 MySQL의 삽입을 위해 그것을 벗어날 것

$lname = mysql_real_escape_string(nl2br($lname)); 

.

또한 텍스트 필드를 텍스트 영역으로 변경해야합니다. 그래서이를 변경 : 이것에

<input type="text" name="lname" size="50"/> 

을 : 작동

<textarea name="lname" cols="50" rows="3"></textarea> 

희망을! 후속 질문이 있으면 알려주십시오.

+0

은 완벽하게 작동하므로 왜 작동하지 않았는가, 왜 내가 images.php에서 변경해야한다고 생각하고 있었는지 그 결과는 내 출력이었고 업로드에 잘못 생각한 적이 없었습니다 ... 감사합니다. 많이! – FGOD

+0

나는 약간의 후속 질문을 가지고 있지만 브레이크 물건들과 관련이 없다 : p 그래서 나는 더 나은 질문을한다.) 나는 여전히 이것을 나의 레이아웃에서 구현할 필요가 있고, 어떤 이유로 그것이 레이아웃을 엉망으로 만든다. 내 페이지 – FGOD

+0

nevermind의 CSS를 업로드하는 것을 잊어 버린 문제를 발견하여 레이아웃에서 많은 도움이되지 못합니다. p – FGOD

관련 문제