반응형
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
반응형
'MySql' 카테고리의 다른 글
MySQL 숫자가 쉼표로 구분 된 목록에 있는지 mysql 확인 (0) | 2020.11.29 |
---|---|
MySQL 정수 데이터 유형으로 데이터베이스에서 무제한 표시 (0) | 2020.11.29 |
MySQL mysqli SELECT 쿼리의 결과 인쇄 (0) | 2020.11.29 |
MySQL 저장 프로 시저 반환 값 (0) | 2020.11.29 |
MySQL 4.0에 생성 및 마지막 업데이트 된 타임 스탬프 열이 모두 있음 (0) | 2020.11.29 |
댓글