Php 在mysql数据库中存储图像不会';不行。现在怎么办?
我尝试了几天现在上传图像到我的数据库,然后显示它们。但是在我的脚本中似乎有一个错误,我找不到它:(所以现在我希望这个社区的人能在这种情况下帮助我-那真的太好了!非常感谢!我知道这很值得一读,对不起 以下是代码表单insert.php:Php 在mysql数据库中存储图像不会';不行。现在怎么办?,php,mysql,file-upload,Php,Mysql,File Upload,我尝试了几天现在上传图像到我的数据库,然后显示它们。但是在我的脚本中似乎有一个错误,我找不到它:(所以现在我希望这个社区的人能在这种情况下帮助我-那真的太好了!非常感谢!我知道这很值得一读,对不起 以下是代码表单insert.php: <?php /*Enter your password on line 29*/ error_reporting(E_ALL); ini_set("display_errors", 1); if(!isset($_FILES['userfile']))
<?php
/*Enter your password on line 29*/
error_reporting(E_ALL);
ini_set("display_errors", 1);
if(!isset($_FILES['userfile']))
{
echo '<p>Please select a file</p>';
}
else
{
try {
$msg= upload(); //this will upload your image
echo $msg; //Message showing success or failure.
}
catch(Exception $e) {
echo $e->getMessage();
echo 'Sorry, could not upload file';
}
}
// the upload function
function upload() {
$host="localhost";
$user="YYYY";
$pass="XXXX";
$db="ibm_sfreund";
$maxsize = 10000000; //set to approx 10 MB
//check associated error code
if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {
//check whether file is uploaded with HTTP POST
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
//checks size of uploaded image on server side
if( $_FILES['userfile']['size'] < $maxsize) {
//checks whether uploaded file is of image type
//if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {
// prepare the image for insertion
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
// put the image in the db...
// database connection
$con = mysql_connect("localhost", "...", "...");
mysql_select_db('...',$con);
// our sql query
$id_xd = $_POST["id"];
$sql = "UPDATE artikel SET image='{$imgData}', name='{$_FILES['userfile']['name']}', mime='{$_FILES['userfile']['type']}' WHERE id='$id_xd';";
// insert the image
mysql_query($sql) or die("Error in Query: " . mysql_error());
$msg='<p>Image successfully saved in database.</p>';
}
else
$msg="<p>Uploaded file is not an image.</p>";
}
else {
// if the file is not less than the maximum allowed, print an error
$msg='<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is '.$maxsize.' bytes</div>
<div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].
' bytes</div><hr />';
}
}
else
$msg="File not uploaded successfully.";
}
else {
$msg= file_upload_error_message($_FILES['userfile']['error']);
}
return $msg;
}
// Function to return error message based on error code
function file_upload_error_message($error_code) {
switch ($error_code) {
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk';
case UPLOAD_ERR_EXTENSION:
return 'File upload stopped by extension';
default:
return 'Unknown upload error';
}
}
已弃用:mysql_connect()[function.mysql connect]:mysql扩展已弃用,将来将被删除:请在第7行的/users/ibm/www/beta/loadpic.php中使用mysqli或PDO
您应该重写代码以使用PDO或mysqli。要使其立即工作,请尝试使用:
error_reporting(0);
ini_set('display_errors', 0);
但这不是一个解决方案,而是要测试它是否工作。因为您可以向我们共享二进制数据,所以在开始时出现的错误会破坏您的脚本
标题:
此外,您还需要发送一个正确的标题,以便浏览器识别这是一个图像,而不是一个网页。在此处使用GIf后,您可以尝试:
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_GIF));
在发送第一个输出之前。但在将图像保存到db之前,您需要先签出mime类型,然后再将其保存。然后,您可以使用二进制数据获取图像,并在显示时发回
addslashes不是很必要。请参考答案并调整您的代码。执行echo$img_final;
对于输出为GIF87a^–ÌÌÌÌ…
是正常的。那么我该怎么做呢?如果您只将图像存储在文件服务器上,并将路径存储在数据库中,您的生活会轻松得多。@jeroen Stori在文件服务器上压缩图像并不总是最好的解决方案。@yergo可能并不总是,但如果您存储的图像最大为10Mb,并且在图像数据上使用添加斜杠
之类的东西,则情况确实如此。现在它只显示字母等,而不显示警告。但是如何显示图像?@mm:您需要一个内容类型
标题作为提示嗯..脚本中是否有任何其他错误可能会使所有内容崩溃?内容类型以前不工作或当前状态下不工作?请检查我的更新并测试它。好了,现在它说图像无法显示,因为它包含错误?
error_reporting(0);
ini_set('display_errors', 0);
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_GIF));