It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Partner Sites
Board index Programming PHP and MySQL Forum

Show upload files in my browser

Moderator: Malcolm

Show upload files in my browser

Postby jeaddy on Sun Feb 10, 2008 2:50 am

Hello everyone,

I have a question with upload files. I create a html form and php codes to upload files. I also created a directory called uploads where my php upload files resides. when I upload it, all the files goes to the uploads directory.

so my question is when I upload can it be save into mysql and then I can bring it out and show it in the browser instead of how i have it in the directory.

How would I do this? I really want to show what I upload in the browser/server.

thanks -
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 1:35 am

Postby leonard on Mon Feb 11, 2008 2:46 pm

You can store files in the database using the blob datatype (blob = binary large object).

Code: Select all
DROP TABLE IF EXISTS `pics`;
create table pics(
   PIC_ID     int(5) not null auto_increment,
   PICNAME    varchar(100),
   PICTURE    blob
) TYPE=INNODB;


You can insert and select the data using:
Code: Select all
insert into picture (PIC_ID,PICNAME,PICTURE)
values ($PIC_ID,$PICNAME,load_file("/mypath/mypicture");

select PICTURE from picture where PIC_ID = $PIC_ID;

Note that displaying the picture needs more than simply selecting it. It always depends on what you want to store.

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 8:11 am
Location: Switzerland

Postby flabbyrabbit on Mon Feb 11, 2008 10:12 pm

If you are simply uploading the image to your server, then you could do as leonard said, but change "PICTURE blob " to "PICLINK varchar(100)". Then you could simply echo out the picture using the link in the database.
Code: Select all
<?php
echo "<img src='".$row[PICLINK]."'>";
?>


Flabby Rabbit
User avatar
flabbyrabbit
500+ Club
 
Posts: 691
Joined: Thu Jan 25, 2007 2:10 pm
Location: Midlands, England

Postby jeaddy on Tue Feb 12, 2008 1:04 am

Kool, thanks leonard and flabbyrabbit.



It always depends on what you want to store


I want to store mostly picture, excel files and sometimes music.

so do you think those are all possible?

thanks
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 1:35 am

Postby flabbyrabbit on Tue Feb 12, 2008 1:10 pm

Do you just want to show links to the files? Then my way would work. If you created a mysql database with: name and url. Then you could add some code to the upload form which adds the new file to the database. Then when someone views the directory you could echo out the files quite easily.

Flabby Rabbit
User avatar
flabbyrabbit
500+ Club
 
Posts: 691
Joined: Thu Jan 25, 2007 2:10 pm
Location: Midlands, England

Postby jeaddy on Wed Feb 13, 2008 4:15 am

Do you just want to show links to the files?

I want to show the files in the browser, the image of it. I created one and it works but it doesn't work for all pictures, it doesn't show all pictures only some. Do you know why that is?

I also would like to upload music and ms doc (mostly excel) files, so how would I select these and view them on the browser?

thanks
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 1:35 am

Postby jeaddy on Thu Feb 14, 2008 2:02 am

Hi

I just noticed why some pictures can't be uploaded, because some of those pictures files size are too large, about 600kb or 1.2 mb. I currently can only upload about 17 kb, how can I increase this capacity so that I can upload all my pictures?


thanks
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 1:35 am

Postby leonard on Thu Feb 14, 2008 8:59 am

I currently can only upload about 17 kb

You need to change your php-configuration.

Edit these values in your php.ini file and then restart the webserver:
upload_max_filesize = 5M ;
post_max_size = 6M ;

upload_max_filesize is the maximum a single file can be.
post_max_size is the total limit of your post.

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 8:11 am
Location: Switzerland

Postby jeaddy on Fri Feb 15, 2008 12:28 am

Thanks
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 1:35 am

Postby jeaddy on Fri Feb 15, 2008 12:51 am

You know how I can upload excel (ms docs) files and musics files into a database and show it?



Thanks
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 1:35 am

Postby jeaddy on Sat Feb 16, 2008 10:48 pm

I have a couple of issue and was hoping if you could help me.

1- I have the codes to upload pix and show them in the browser, but in only does it for one pix at a time and the last one uploaded.
I would like to show all the pixs in the database with the last one uploaded showed last, I tried for the past two days to do this but haven't found any
solution. if you have any let me know.

this is the code on the bottom that does this;


<code>
<?php

// Connect to database

$errmsg = "";
if (! @mysql_connect("localhost","TEST1","TEST")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("test");


$q = <<<CREATE
create table pix (
pid int primary key not null auto_increment,
title text,
imgdata longblob)
CREATE;
@mysql_query($q);


// Insert any new image into database

//-------------------------------------------------------------------------------------------
move_uploaded_file($_FILES['imagefile']['tmp_name'],"lates.img");
//MOVE UPLOADED FILE TO A NEW LOCATION
$inst = fopen("lates.img","rb");
$image = addslashes(fread($inst,filesize("lates.img")));

mysql_query ("insert into pix2 (title, imgdata) values (\""."\", \"".$image."\")");
//--------------------------------------------------------------------------------------------------
$gotten = @mysql_query("select imgdata from pix2 order by pid desc");

if ($row = @mysql_fetch_assoc($gotten)) {
$bytes = $row[imgdata];
} else {
$errmsg = "There is no image in the database yet";
$title = "no database image available";
$inst = fopen("../wellimg/ctco.jpg","rb");
$bytes = fread($inst,filesize("../wellimg/ctco.jpg"));
}

if ($_REQUEST[gim] == 1) {
header("Content-type: image/jpeg");
print $bytes;
exit ();
}


?>

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=tan><h2>Here's the latest picture</h2>
<font color=red><?php $errmsg ?></font>
<center><img src=?gim=1 width=1000 height=1000><br> <!--144-->
<b><?php $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=15000000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br>
<hr>
</body>
</html>

</code>


2 - do you know if or the codes to upload music and excel files to the database and show them on the browser? I was thinking that maybe I should do it as I
did it for pix and change it to xls for excel docs, etc.

thanks[code][/code]
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 1:35 am


Return to PHP and MySQL Forum

Who is online

Users browsing this forum: No registered users and 0 guests