Mysql 似乎无法将图像上载到数据库
我在网页上的内容不想显示我的整个网页,但如果必须,我会。当我在网页外的一个虚拟页面中上传图像时,该站点只是返回到edit-profile.php,实际上并没有将图像上传到数据库中。但当我把它添加到我的网页时,它就停止工作了Mysql 似乎无法将图像上载到数据库,mysql,uploading,Mysql,Uploading,我在网页上的内容不想显示我的整个网页,但如果必须,我会。当我在网页外的一个虚拟页面中上传图像时,该站点只是返回到edit-profile.php,实际上并没有将图像上传到数据库中。但当我把它添加到我的网页时,它就停止工作了 <form action="/sub/test.php" method="POST" enctype="multipart/form-data"> <label for="title">Title:</label><br>
<form action="/sub/test.php" method="POST" enctype="multipart/form-data">
<label for="title">Title:</label><br>
<input type="text" name="title" id="title" size="64"><br><br>
<label for="photo">Photo:</label><br>
<input type="file" name="photo" id="photo"><br><br>
<label for="password">Password:</label><br>
<input type="password" name="password" id="password"><br><br>
test.php:
<?php
$db_host = ''; // don't forget to change
$db_user = '';
$db_pwd = '';
$database = '';
$table = '';
// use the same name as SQL table
$password = '123';
// simple upload restriction,
// to disallow uploading to everyone
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// This function makes usage of
// $_GET, $_POST, etc... variables
// completly safe in SQL queries
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 == 3) // cheking image type
$ext="png"; // to use it later in HTTP headers
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
$ext="gif";
else
$msg = 'Error: unknown file format';
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 = 'Success: image uploaded';
}
}
elseif (isset($_GET['title'])) // isset(..title) needed
$msg = 'Error: file not loaded';// 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 = 'Photo deleted';
}
}
}
elseif (isset($_GET['show']))
{
List item
---------
$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('no image');
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();
}
?>
<input type="submit" value="upload">
标题:
照片:
密码:
test.php:
你应该为你正在使用的语言添加一些标签,并解释你采取了哪些步骤来找出它为什么不工作。您确定您的页面正在连接到数据库吗?你在你的浏览器开发工具中看到任何错误吗?没有错误,看起来像是网页闪烁,但当我检查数据库时,什么都没有。你应该为你正在使用的语言添加一些标记,并解释你为找出它为什么不工作而采取的步骤。您确定您的页面正在连接到数据库吗?你在你的浏览器开发工具中看到任何错误吗?没有错误,看起来像是网页闪烁,但当我检查数据库时,什么都没有。它甚至看起来好像什么都没做