본문 바로가기
MySql

MySQL PHP를 사용하여 mysql 데이터베이스에 이미지를 저장하는 방법

by 베이스 공부 2020. 11. 29.
반응형

MySQL 데이터베이스에 이미지를 저장하고 표시하려면 어떻게해야합니까? 지금까지 사용자로부터 이미지를 가져 와서 폴더에 저장하는 코드 만 작성했습니다. 지금까지 작성한 코드는 다음과 같습니다. HTML 파일

<input type="file" name="imageUpload" id="imageUpload">

PHP 파일

    $target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";}

 

해결 방법

 

나는 대답을 찾았습니다. 여기에서 똑같은 것을 찾는 사람들을 위해 내가 한 방법입니다. 데이터베이스에 이미지를 업로드하는 것을 고려해서는 안됩니다. 대신 업로드 된 파일의 이름을 데이터베이스에 저장 한 다음 파일 이름을 검색하여 이미지를 표시하려는 곳에서 사용할 수 있습니다.

HTML 코드

<input type="file" name="imageUpload" id="imageUpload">

PHP 코드

if(isset($_POST['submit'])) {

    //Process the image that is uploaded by the user

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }

    $image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable

    //storind the data in your database
    $query= "INSERT INTO items VALUES ('$id','$title','$description','$price','$value','$contact','$image')";
    mysql_query($query);

    require('heading.php');
    echo "Your add has been submited, you will be redirected to your account page in 3 seconds....";
    header( "Refresh:3; url=account.php", true, 303);
}

이미지 표시 코드

while($row = mysql_fetch_row($result)) {
    echo "<tr>";
    echo "<td><img src='uploads/$row[6].jpg' height='150px' width='300px'></td>";
    echo "</tr>\n";
}

 

참조 페이지 https://stackoverflow.com/questions/26757659

 

 

반응형

댓글