首次尝试上传图像并存储到db [英] First attempt at uploading image and storing to db
问题描述
我正在尝试上传图片,根据该图片创建一个新文件
然后将base64编码的图像数据存储到一个数据库。
我真的不知道我的代码出错了所以我只会在下面显示
:
以下代码包含处理上传文件的脚本,
上传文件的表单,然后是一些链接,用于查看来自
a php脚本的文件simpleimageviewer.php:
<?php
//此文件包含函数''dbconn()'',允许你要
//连接到数据库
include(''functions.php'');
?>
< html>
< body>
<?php
//
if(isset($ _ GET [''action''])){
if(isset($ _ GET [''action''])=="添加){
$ tmpfilesize = $ _FILES [''imgfile''] ['s ize''];
$ tmpfilename = $ _FILES [''imgfile''] [''tmp_name''];
$ tmpfiletype = $ _FILES [''imgfile ''] [''type''];
if($ tmpfilesize> 0){
if(substr($ tmpfiletype,0,6)==''image /''){
//从上传的图片创建图片
开关($ tmpfiletype)
{
case" image / jpeg":
case" image / pjpeg" :
$ img = imagecreatefromjpeg($ tmpfilename);
break;
case" image / gif":
$ img = imagecreatefromgif($ tmpfilename);
break;
case" image / png":
$ img = imagecreatefrompng( $ tmpfilename);
休息;
}
//创建新文件
$ newfile = tempnam(" ; / tmp"," img_");
//创建jpeg并另存为新文件
imagejpeg($ img,$ newfile,80);
//打开新文件
$ nf = fopen($ newfile," r");
//获取已打开文件的内容并存储在var中
$ filecontents = fread($ nf,filesize($ newfile));
//销毁文件
fclose($ nf);
unlink($ newfile) ;
if($ filecontents){
$ insert =" INSERT INTO imglib(imgid,imgdata,imgtype)VALUES
(null,''" 。 base64_encode($ filecontents)。 '',''" 。 $ imgtype。 "'')" ;;
$ result = mysql_query($ insert,$ db);
if($ result){
echo"< p> Result!< / p>" ;;
} else {
echo"< p> No,no,no ..它没有用< / p>" ;;
}
}
}其他{
echo" ;< p>请上传图片 - 错误的文件类型(。
$ tmpfiletype。)< / p>" ;;
}
}其他{
echo"< p>文件未上传< / p>" ;;
}
}
}
?>
< form enctype =" multipart / form-data"名称= QUOT;" method =" post"
action =" simple.php?action = add">
< input type =" hidden"名称= QUOT; MAX_FILE_SIZE" value =" 5000000"><! - about。
5MB - >
< input name =" imgfile" type =" file">< br />
< input name =" submit"类型= [提交" value =" Submit">
< / form>
<?php
$ select =" SELECT * FROM imglib";
$ result = mysql_query($ select,$ db);
if(mysql_num_rows($ result)> 0){
echo"< ul> \ n";
while($ row = mysql_fetch_array($ result)){
$ imgid = $ row [ ''imgid''];
echo < li>< a href = \" simpleimageviewer.php?imgid =" 。 $ imgid。
" \">图片" 。 $ imgid。 "< / a>< / li> \ n";
}
echo"< / ul>" ;;
}
?>< / body>
< / html>
抱歉关于代码的质量,但我想我应该提供它
all。我也在某些时候需要调整上传图像的大小,所以我似乎最好创建一个临时文件,我可以在写作之前用
来播放。
问题是,当我使用以下
脚本(simpleimageviewer.php)查看图像时:
include('' functions.php'');
//图片输出
$ select =" SELECT imgdata,imgtype FROM imglib WHERE imgid =" ; 。
$ _GET [''imgid''];
$ result = mysql_query($ select,$ db);
while($ row = mysql_fetch_array($ result)){
$ imgdata = $ row [''imgdata''];
$ imgtype = $ row [''imgtype''] ;
}
// header
header(" Content-type:"。$ imagetype);
echo base64_decode($ imgdata);
flush();
它不会显示所有图像。请参阅
http:// www .martynbissett.co.uk / test / ... er.php?imgid = 5
看看我的意思。当我突出显示图像时,它确实显示了很多白色
区域,这些区域在上传的图像中创建初始
图像时似乎一直存在问题。
任何人都可以告诉我的代码可能有什么问题,干杯
Burnsy
_GET [''action' ])==" add"){
tmpfilesize =
Hi,
I am trying to upload an image, create a new file based on that image
and then store the base64 encoded image data in a database.
I dont really know where my code is going wrong so I will just display
it below:
The following code contains the script to process the uploaded file,
the form to upload the file and then a few links to view the file from
a php script called simpleimageviewer.php:
<?php
//this file includes the function, ''dbconn()'', that allows you to
//connect to the database
include(''functions.php'');
?>
<html>
<body>
<?php
//
if (isset($_GET[''action''])) {
if (isset($_GET[''action'']) == "add") {
$tmpfilesize = $_FILES[''imgfile''][''size''];
$tmpfilename = $_FILES[''imgfile''][''tmp_name''];
$tmpfiletype = $_FILES[''imgfile''][''type''];
if ($tmpfilesize > 0) {
if (substr($tmpfiletype, 0, 6) == ''image/'') {
//create image from uploaded image
switch ($tmpfiletype)
{
case "image/jpeg":
case "image/pjpeg":
$img = imagecreatefromjpeg($tmpfilename);
break;
case "image/gif":
$img = imagecreatefromgif($tmpfilename);
break;
case "image/png":
$img = imagecreatefrompng($tmpfilename);
break;
}
//create new file
$newfile = tempnam("/tmp", "img_");
//create jpeg and save as newfile
imagejpeg($img, $newfile, 80);
//open new file
$nf = fopen($newfile, "r");
//get contents of opened file and store in var
$filecontents = fread($nf, filesize($newfile));
//destroy file
fclose($nf);
unlink($newfile);
if ($filecontents) {
$insert = "INSERT INTO imglib (imgid, imgdata, imgtype) VALUES
(null, ''" . base64_encode($filecontents) . "'', ''" . $imgtype . "'')";
$result = mysql_query($insert, $db);
if ($result) {
echo "<p>Result!</p>";
}else{
echo "<p>No, no, no ... it didnt work</p>";
}
}
} else {
echo "<p>Please upload an image - wrong file type (" .
$tmpfiletype . ")</p>";
}
}else{
echo "<p>File not uploaded</p>";
}
}
}
?>
<form enctype="multipart/form-data" name="" method="post"
action="simple.php?action=add">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"><!-- approx.
5MB -->
<input name="imgfile" type="file"><br />
<input name="submit" type="submit" value="Submit">
</form>
<?php
$select = "SELECT * FROM imglib";
$result = mysql_query($select, $db);
if (mysql_num_rows($result) > 0) {
echo "<ul>\n";
while ($row = mysql_fetch_array($result)) {
$imgid = $row[''imgid''];
echo " <li><a href=\"simpleimageviewer.php?imgid=" . $imgid .
"\">Image " . $imgid . "</a></li>\n";
}
echo "</ul>";
}
?></body>
</html>
Sorry about the mass of code but I thought I should just provide it
all. I also at some point need to resize the uploaded image so I seemed
the best idea to create a temporary file that I could play around with
before writing.
The problem is that when I go to view the image using the following
script (simpleimageviewer.php):
include(''functions.php'');
//IMAGE OUTPUT
$select = "SELECT imgdata, imgtype FROM imglib WHERE imgid = " .
$_GET[''imgid''];
$result = mysql_query($select, $db);
while ($row = mysql_fetch_array($result)) {
$imgdata = $row[''imgdata''];
$imgtype = $row[''imgtype''];
}
//header
header("Content-type: " . $imagetype);
echo base64_decode($imgdata);
flush();
It doesnt display all the image. See
http://www.martynbissett.co.uk/test/...er.php?imgid=5 to
see what I mean. When I highlight the image it does show a lot of white
area that appears to have been a problem when creating the initial
image from the uploaded one.
Can anyone tell from my code what might be the problem, cheers
Burnsy
_GET[''action''])) {
if (isset(
_GET[''action'']) == "add") {
tmpfilesize =
这篇关于首次尝试上传图像并存储到db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!