이미지 경로를 데이터베이스에 저장하고 업로드 한 후에 표시하는 방법은 무엇입니까?이미지 경로를 데이터베이스에 삽입하는 방법은 무엇입니까?
<?php
$sub=0;
ini_set("display_errors", 0);
if(isset($_REQUEST['submited'])) {
// your save code goes here
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
$sub= 1;
echo "<font size='7' color='white'><b> Success! Your photo has been uploaded.</b></font>";
}
}
}
else
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="submited" value="true" />
<?php
ini_set("display_errors", 0);
if($sub==0)
{
?>
<label size="16" for="file">Choose Photo:</label>
<input id="shiny" type="file" name="file" onchange="file_selected = true;">
<input id="shiny" type="submit" value="Upload" name="submit">
<?php
}
?>
</form>
여기서 데이터베이스 정보는 ... 어떻게 데이터베이스에 이미지 경로를 삽입 한 후 그림을 표시합니까? 나는 VALUES ('$_FILES["file"]["name"]')";
시도했지만 그것은 작동하지 않는 것 ..
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("simple_login", $con);
$sql="INSERT INTO photo (photo)
VALUES
('$_FILES["file"]["name"]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
?>
** 경고 ** 당신의 코드는 [SQL 주입 취약점]를 포함 (http://en.wikipedia.org/wiki/SQL_injection)! - 직접 원시, 필터링되지 않은, 확인되지 않은 사용자 입력을 전달하는 SQL 문자열로 변환합니다. SQL 주입은 [매우 쉽게 고칠 수 있습니다] (http://stackoverflow.com/q/60174/168868). [PDO로 전환] (http://php.net/book.pdo) 또는 [mysqli] (http://php.net/book.mysqli)를 고려하여 [매개 변수화 된 쿼리와 함께 준비된 문구] (http : //en.wikipedia.org/wiki/Prepared_statement). – Charles
왜이 작업을 수행할까요? – Woot4Moo