0

画像パスをデータベースに保存し、アップロード後に表示するにはどうすればよいですか?

<?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);
?> 
4

2 に答える 2

1
"INSERT INTO photo (photo) VALUES ('{$_FILES["file"]["name"]}')"

それはうまくいくはずです。文字列で連想配列を使用するには、中{ }括弧 ( ) で囲む必要があります。


特定の質問に関係のない3つのポイント:

1 : データベースに入力する前に、常にユーザー入力をサナタイズする必要があります。したがって、あなたがすべきことは次のとおりです。

"INSERT INTO photo (photo) VALUES ('" . mysql_real_escape_string($_FILES["file"]["name"]) . "')"

または、mysqli または pdo で準備済みステートメントを使用します。

2 : データベースにファイルのリストを保存するだけの場合、何の意味がありますか? それらを保存しているディレクトリを反復処理しないのはなぜですか?

3 :mysql_*関数は減価償却されています。mysqliまたはの使用を検討する必要があります。pdo

于 2013-01-03T22:09:31.803 に答える
0

Mysqliを使用して解決したので、SQLインジェクションも防ぐことができます.....助けてくれてありがとう...

<?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;
$mysqli = new mysqli("localhost", "root", "", "simple_login");

// TODO - Check that connection was successful.

$photo= $_FILES["file"]["name"];

$stmt = $mysqli->prepare("INSERT INTO photo (photo) VALUES (?)");

// TODO check that $stmt creation succeeded

// "s" means the database expects a string
$stmt->bind_param("s", $photo);

$stmt->execute();

$stmt->close();

$mysqli->close();

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>
</div>
于 2013-01-04T00:02:41.560 に答える