Ali Can Gönüllü tarafından 2019-08-15 09:58:08 tarihinde yazıldı. Tahmini okunma süresi 4 dakika, 55 saniye.
Çalışmalarım için bağış yapmak isterseniz Patreon hesabımdan bağışta bulunabilirsiniz.
Blog adresinde yazılanlar sadece eğitim amacıyla deneysel olarak hazırlanmıştır. Konu içerisinde geçen yöntemleri kendi oluşturduğunuz test ortamında denemenizi tavsiye ederiz.
Herhangi bir tarih, yer ve/veya mekanda oluşacak zararlardan alicangonullu.org, alicangonullu.org yöneticisi ve/veya konu yazarı mesul değildir.
Merhabalar
Bu konumuzda sizlere PHP ile Blob şeklinde şifrelenmiş resim mantığını açıklayacağız. Öncelikle Blob nedir onu açıklamalıyız.
Blob Nedir ?
Blob normal bir resmin şifrelenip sıkıştırılarak veritabana aktarılmış halidir. Şuana kadar en güvenli yol olarak görülür.
Gelelim Fasulyenin Faydalarına
<?php
function sql_safe($s)
{
if (get_magic_quotes_gpc())
$s = stripslashes($s);
return mysql_real_escape_string($s);
}
// If user pressed submit in one of the forms
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// cleaning title field
$title = trim(sql_safe($_POST['title']));
if ($title == '') // if title is not set
$title = '(empty title)';// use (empty title) string
if ($_POST['password'] != $password) // cheking passwors
$msg = 'Error: wrong upload password';
else
{
if (isset($_FILES['photo']))
{
@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);
// Get image type.
// We use @ to omit errors
if ($imtype == 4)
$ext="pdf";
elseif ($imtype == 3)
$ext="png";
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
$ext="gif";
else
$msg = '<script>alert("Error: Dosya Formatı Geçersiz");
</script>';
if (!isset($msg)) // If there was no error
{
$data = file_get_contents($_FILES['photo']['tmp_name']);
$data = mysql_real_escape_string($data);
// Preparing data to be used in MySQL query
mysql_query("INSERT INTO {$table}
SET ext='$ext', title='$title',
data='$data'");
$msg = '<script>alert("Başarılı: Dosya Yüklendi");
</script>';
}
}
elseif (isset($_GET['title'])) // isset(..title) needed
$msg = '<script>alert("Error: Dosya Yüklenemedi");
</script>';// to make sure we've using
// upload form, not form
// for deletion
if (isset($_POST['del'])) // If used selected some photo to delete
{ // in 'uploaded images form';
$id = intval($_POST['del']);
mysql_query("DELETE FROM {$table} WHERE id=$id");
$msg = '<script>alert("Başarılı: Dosya Silindi");</script>';
}
}
}
elseif (isset($_GET['show']))
{
$id = intval($_GET['show']);
$result = mysql_query("SELECT ext, UNIX_TIMESTAMP(image_time), data
FROM {$table}
WHERE id=$id LIMIT 1");
if (mysql_num_rows($result) == 0)
die('Veritabanda Dosya Bulunamadı');
list($ext, $image_time, $data) = mysql_fetch_row($result);
$send_304 = false;
if (php_sapi_name() == 'apache') {
// if our web server is apache
// we get check HTTP
// If-Modified-Since header
// and do not send image
// if there is a cached version
$ar = apache_request_headers();
if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists
($ar['If-Modified-Since'] != '') && // not empty
(strtotime($ar['If-Modified-Since']) >= $image_time)) // and grater than
$send_304 = true; // image_time
}
if ($send_304)
{
// Sending 304 response to browser
// "Browser, your cached version of image is OK
// we're not sending anything new to you"
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT', true, 304);
exit(); // bye-bye
}
// outputing Last-Modified header
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $image_time).' GMT',
true, 200);
// Set expiration time +1 year
// We do not have any photo re-uploading
// so, browser may cache this photo for quite a long time
header('Expires: '.gmdate('D, d M Y H:i:s', $image_time + 86400*365).' GMT',
true, 200);
// outputing HTTP headers
header('Content-Length: '.strlen($data));
header("Content-type: image/{$ext}");
// outputing image
echo $data;
exit();
}
?>
<html><head>
<title>Resim Paneli</title>
</head>
<body>
<?php
if (isset($msg)) // this is special section for
// outputing message
{
?>
<p style="font-weight: bold;"><?=$msg?>
<br>
<!-- I've added reloading link, because
refreshing POST queries is not good idea -->
</p>
<?php
}
?>
<h1> Resim Paneli</h1>
<hr></hr>
<h2>Yüklenen Resimler:</h2>
<hr></hr>
<form action="<?=$PHP_SELF?>" method="post">
<!-- This form is used for image deletion -->
<?php
$result = mysql_query("SELECT id, image_time, title FROM {$table} ORDER BY id DESC");
if (mysql_num_rows($result) == 0) // table is empty
echo '<ul><li>Resim Yok Veya Yüklenemedi</li></ul>';
else
{
echo '<ul>';
while(list($id, $image_time, $title) = mysql_fetch_row($result))
{
// outputing list
echo "<li><input type='radio' name='del' value='{$id}'>";
echo "<a href='../gr.php?gtr={$id}'>{$title}</a> – ";
echo "<small>{$image_time}</small></li>";
}
echo '</ul><label for="password">Şifre:</label><br>
<input type="password" name="password" id="password"><br><br>';
echo '<input type="submit" value="Sil">';
echo '<hr></hr>';
}
?>
</form>
<h2>Yeni Resim Yükle:</h2>
<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data">
<label for="title">Başlık :</label><br>
<input type="text" name="title" id="title" size="64"><br><br>
<label for="photo">Resim:</label><br>
<input type="file" name="photo" id="photo"><br><br>
<label for="password">Şifre:</label><br>
<input type="password" name="password" id="password"><br><br>
<input type="submit" value="upload">
</form>
</body>
</html>
Ilk aşamada SQL bağlantımızı güvenli hale getirdik. Sonrasında resim yükleme fonkisyonu ve en son resim yükleme panelimizi yaptık.
Okuduğunuz için teşekkürler !