By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Image storing in database

P: n/a
Hello people, I could really use your help on this one... I have script
to upload image data to a database, but all that is uploaded is the
decription, not the data BLOB itself. There must be something wrong
with the code that fread() and fopen() the file, but what?

The inclusion file below, which diplays the form:

<?php
print ("<table>");
print ("<tr>");
print ("<td><form method=POST action=xhandleaanbod.php
enctype=multipart/form-data>");
print ("Omschrijving:");
print ("</td>");
print ("<td><input type=text name=form_description1 size=25>");
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td><input type=hidden name=MAX_FILE_SIZE value=1000000>");
print ("Bestand:");
print ("</td>");
print ("<td><input type=file name=form_data1 size=25>");
print ("</td>");
print ("</tr>");
print ("</table>");
?>
And handling it, I use 5 of these includes, numbered accordingly

<?
$data1 = addslashes(fread(fopen($form_data1, "r"),
filesize($form_data1)));
$data2 = addslashes(fread(fopen($form_data2, "r"),
filesize($form_data2)));
$data3 = addslashes(fread(fopen($form_data3, "r"),
filesize($form_data3)));
$data4 = addslashes(fread(fopen($form_data4, "r"),
filesize($form_data4)));
$data5 = addslashes(fread(fopen($form_data5, "r"),
filesize($form_data5)));

$query = "INSERT into $Tabelnaam &
#40;description1,bin_data1,filename1,filesize1,fil etype1,description2,bin_data2,
filename2,filesize2,filetype2,description3,bin_dat a3,filename3,filesize3,filetyp
e3,description4,bin_data4,filename4,filesize4,file type4,description5,bin_data5,f
ilename5,filesize5,filetype5) values ('$form_description1',
'$form_data1', '$form_data1_name', '$form_data1_size',
'$form_data1_type', '$form_description2', '$form_data2',
'$form_data2_name', '$form_data2_size', '$form_data2_type',
'$form_description3', '$form_data3', '$form_data3_name',
'$form_data3_size', '$form_data3_type', '$form_description4',
'$form_data4', '$form_data4_name', '$form_data4_size',
'$form_data4_type', '$form_description5', '$form_data5',
'$form_data5_name', '$form_data5_size', '$form_data5_type')";
?>

Then I get all these warmings, an entry is made to the database, but
the data BLOB fields in there remain empty:

Warning: fopen("", "r") - Success in /xhandleaanbod.php on line 100

Warning: stat failed for (errno=2 - No such file or directory) in
/xhandleaanbod.php on line 100

Warning: fread(): supplied argument is not a valid File-Handle resource
in /xhandleaanbod.php on line 100

Warning: fopen("", "r") - No such file or directory in
/xhandleaanbod.php on line 101

Warning: stat failed for (errno=2 - No such file or directory) in
/xhandleaanbod.php on line 101

Warning: fread(): supplied argument is not a valid File-Handle resource
in /xhandleaanbod.php on line 101

Warning: fopen("", "r") - No such file or directory in
/xhandleaanbod.php on line 102

Warning: stat failed for (errno=2 - No such file or directory) in
/xhandleaanbod.php on line 102

Warning: fread(): supplied argument is not a valid File-Handle resource
in /xhandleaanbod.php on line 102

Warning: fopen("", "r") - No such file or directory in
/xhandleaanbod.php on line 103

Warning: stat failed for (errno=2 - No such file or directory) in
/xhandleaanbod.php on line 103

Warning: fread(): supplied argument is not a valid File-Handle resource
in /xhandleaanbod.php on line 103

Warning: fopen("", "r") - No such file or directory in
/xhandleaanbod.php on line 104

Warning: stat failed for (errno=2 - No such file or directory) in
/xhandleaanbod.php on line 104

Warning: fread(): supplied argument is not a valid File-Handle resource
in /xhandleaanbod.php on line 104
De opdracht is:
1
De opdracht is met succes uitgevoerd!
Something tells me there should be more to it to read and upload a
filem, but don't know what it is...

Thanks!

Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 23 Apr 2005 04:48:01 -0700, "pescott" <go****@pescott.com> wrote:
Hello people, I could really use your help on this one... I have script
to upload image data to a database, but all that is uploaded is the
decription, not the data BLOB itself. There must be something wrong
with the code that fread() and fopen() the file, but what?

The inclusion file below, which diplays the form:

<?php
print ("<table>");
print ("<tr>");
print ("<td><form method=POST action=xhandleaanbod.php
enctype=multipart/form-data>");
print ("Omschrijving:");
print ("</td>");
print ("<td><input type=text name=form_description1 size=25>");
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td><input type=hidden name=MAX_FILE_SIZE value=1000000>");
print ("Bestand:");
print ("</td>");
print ("<td><input type=file name=form_data1 size=25>");
print ("</td>");
print ("</tr>");
print ("</table>");
?>
And handling it, I use 5 of these includes, numbered accordingly

<?
$data1 = addslashes(fread(fopen($form_data1, "r"),
filesize($form_data1)));


That's not how you handle file uploads. Read:

http://uk2.php.net/features.file-upload

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
I read it, but honestly, I don't know where the upload form supplied
differs from mine. The filename is displayed, and the filename is
inserted as a BLOB, but have no clue where things are going astray. The
script was taken from a tutorial.

Jul 17 '05 #3

P: n/a
On 23 Apr 2005 05:46:44 -0700, "pescott" <go****@pescott.com> wrote:
I read it, but honestly, I don't know where the upload form supplied
differs from mine. The filename is displayed, and the filename is
inserted as a BLOB, but have no clue where things are going astray. The
script was taken from a tutorial.


The problem's not in the form, it's how you're processing the files, and many
other issues. If this came from a tutorial, it is a very poor tutorial. See
other reply where I've gone into more detail.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4

P: n/a
On 23 Apr 2005 04:48:01 -0700, "pescott" <go****@pescott.com> wrote:
<?php
print ("<table>");
All these prints are a bit of a waste of time, when you can output literal
HTML with PHP easily; just drop out of PHP mode with ?> and put the HTML
straight in.

There's nothing actually wrong with all the prints, it's just harder to read
than it could be.
print ("<tr>");
print ("<td><form method=POST action=xhandleaanbod.php
enctype=multipart/form-data>");
print ("Omschrijving:");
print ("</td>");
print ("<td><input type=text name=form_description1 size=25>");
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td><input type=hidden name=MAX_FILE_SIZE value=1000000>");
print ("Bestand:");
print ("</td>");
print ("<td><input type=file name=form_data1 size=25>");
print ("</td>");
print ("</tr>");
print ("</table>");
?>
You've not got a closing form tag or a submit button.
And handling it, I use 5 of these includes, numbered accordingly

<?
Short open tags should be avoided, use <?php instead.
$data1 = addslashes(fread(fopen($form_data1, "r"),
filesize($form_data1)));
Unfortunately this makes no sense whatsoever.

$form_data1 will not be set unless you are on a very old version of PHP or are
running with "register_globals" turned on, which is not recommended.

And anyway, it would not point to an uploaded file. Skipping ahead to the
error messages:
Warning: fopen("", "r") - Success in /xhandleaanbod.php on line 100
First argument is "", which is empty, proving that $form_data1 was not set. So
it's trying to open an empty file. This produces a weird error ("Success"), but
it is an error.

You didn't check for errors when opening the file, so things just get worse
from here onwards since you're feeding invalid results to the rest of the
calls.
Warning: stat failed for (errno=2 - No such file or directory) in
/xhandleaanbod.php on line 100
filesize uses stat to get file information. Since the filename is empty, this
fails too - there's no file named by the empty string ""/
Warning: fread(): supplied argument is not a valid File-Handle resource
in /xhandleaanbod.php on line 100
fopen already failed, so passing that file handle to fread is never going to
work.
$query = "INSERT into $Tabelnaam &
#40;description1,bin_data1,filename1,filesize1,fi letype1,description2,bin_data2,
I'm assuming the ( is an artifact of the Google Groups web interface to
newsgroups which is not very good.
filename2,filesize2,filetype2,description3,bin_da ta3,filename3,filesize3,filetyp
e3,description4,bin_data4,filename4,filesize4,fil etype4,description5,bin_data5,f
ilename5,filesize5,filetype5) values ('$form_description1',
'$form_data1', '$form_data1_name', '$form_data1_size',
'$form_data1_type', '$form_description2', '$form_data2',
'$form_data2_name', '$form_data2_size', '$form_data2_type',
'$form_description3', '$form_data3', '$form_data3_name',
'$form_data3_size', '$form_data3_type', '$form_description4',
'$form_data4', '$form_data4_name', '$form_data4_size',
'$form_data4_type', '$form_description5', '$form_data5',
'$form_data5_name', '$form_data5_size', '$form_data5_type')";
?>

Then I get all these warmings, an entry is made to the database, but
the data BLOB fields in there remain empty:


You never showed the code that executes a query, just set up an SQL statement,
so I assume there is more code you haven't posted.

Read http://uk.php.net/features.file-upload again.

Note that all access to uploaded files is done through the $_FILES array.

The following is untested but is closer to what you want:

<?php
if (isset($_FILE['form_data1'])
&& $_FILE['form_data1']['error'] == UPLOAD_ERR_OK))
{
$data1 = mysql_real_escape_string(
file_get_contents($_FILE['form_data1']['tmp_name'])
);

// same again for the other files
// then do your SQL stuff
}

There is an example at the bottom of the manual page above that demonstrates
uploading an array of files, which would be neater than $data1, $data2, ...,
$data6.

One slight difference between what you want to do and the full examples in the
manual is that you don't need to bother using move_uploaded_file(), since in
your case you don't want to keep the file, you just upload its contents to the
database, then you can leave PHP to delete the temporary file itself.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5

P: n/a
I modified the handling script quite a bit, and there are no parsing
errors, but still a warning and a db entry error.

The file description and file itself are browsed in an inclusion
script, 5, numbered accordingly (corresponding to 5 groups of fields to
hold data in de db). Then the processing starts, like so:

<?php
/* Dit script ontvangt en verwerkt de door "xcheckaanbod.php"
gegenereerde gegevens. */

// Stel hier de variabelen voor de toegang tot de database in:
$Host = "localhost";
$Gebruiker = "";
$Wachtwoord = "";
$DBNaam = "";
$Tabelnaam = "aanbod";

$Verbinding = mysql_connect ($Host, $Gebruiker, $Wachtwoord) or die("Er
kon geen verbinding worden gemaakt met de database");

// select database
mysql_select_db ("$DBNaam");

// Snijd de binnekomende gegevens bij.
$adres = trim (htmlspecialchars($_POST["adres"]));
// (Cut the trimming of other fields for space purposes)
$verkocht = trim (htmlspecialchars($_POST["verkocht"]));
$form_description1 = trim
(htmlspecialchars($_POST["form_description1"]));
$form_description2 = trim
(htmlspecialchars($_POST["form_description2"]));
$form_description3 = trim
(htmlspecialchars($_POST["form_description3"]));
$form_description4 = trim
(htmlspecialchars($_POST["form_description4"]));
$form_description5 = trim
(htmlspecialchars($_POST["form_description5"]));

// store the file information to variables for easier access 1
$tmp_name1 = $_FILES['form_data1']['tmp_name'];
$type1 = $_FILES['form_data1']['type'];
$name1 = $_FILES['form_data1']['name'];
$size1 = $_FILES['form_data1']['size'];

// store the file information to variables for easier access 2
$tmp_name2 = $_FILES['form_data2']['tmp_name'];
$type2 = $_FILES['form_data2']['type'];
$name2 = $_FILES['form_data2']['name'];
$size2 = $_FILES['form_data2']['size'];

// store the file information to variables for easier access 3
$tmp_name3 = $_FILES['form_data3']['tmp_name'];
$type3 = $_FILES['form_data3']['type'];
$name3 = $_FILES['form_data3']['name'];
$size3 = $_FILES['form_data3']['size'];

// store the file information to variables for easier access 4
$tmp_name4 = $_FILES['form_data4']['tmp_name'];
$type4 = $_FILES['form_data4']['type'];
$name4 = $_FILES['form_data4']['name'];
$size4 = $_FILES['form_data4']['size'];

// store the file information to variables for easier access 5
$tmp_name5 = $_FILES['form_data5']['tmp_name'];
$type5 = $_FILES['form_data5']['type'];
$name5 = $_FILES['form_data5']['name'];
$size5 = $_FILES['form_data5']['size'];

// if the upload succeded, the file will exist
if
(file_exists('$tmp_name1','$tmp_name2','$tmp_name3 ','$tmp_name4','$tmp_name5')){

// check to make sure that it is an uploaded file and not a system
file
if(is_uploaded_file('$tmp_name1','$tmp_name2','$tm p_name3','$tmp_name4','$tmp_name5')){

// open the file for a binary read
$file1 = fopen($tmp_name1,'rb');
$file2 = fopen($tmp_name2,'rb');
$file3 = fopen($tmp_name3,'rb');
$file4 = fopen($tmp_name4,'rb');
$file5 = fopen($tmp_name5,'rb');

// read the file content into a variable
$data1 = fread($file1,filesize($tmp_name1));
$data2 = fread($file2,filesize($tmp_name2));
$data3 = fread($file3,filesize($tmp_name3));
$data4 = fread($file4,filesize($tmp_name4));
$data5 = fread($file5,filesize($tmp_name5));

// close the file
fclose($file1,$file2,$file3,$file4,$file5);

// now we encode it and split it into acceptable length lines
$data1 = chunk_split(base64_encode($data1));
$data2 = chunk_split(base64_encode($data2));
$data3 = chunk_split(base64_encode($data3));
$data4 = chunk_split(base64_encode($data4));
$data5 = chunk_split(base64_encode($data5));
}

}

else {
print("<br><b>Nope! Try again!</b><br>\n");
}

$query = "INSERT into $Tabelnaam (adres,verkocht,
description1,bin_data1,filename1,filesize1,filetyp e1,
description2,bin_data2,filename2,filesize2,filetyp e2,
description3,bin_data3,filename3,filesize3,filetyp e3,
description4,bin_data4,filename4,filesize4,filetyp e4,
description5,bin_data5,filename5,filesize5,filetyp e5)
values
('$adres', '$verkocht',
'$form_description1', '$data1', '$name1', '$size1', '$type1',
'$form_description2', '$data2', '$name2', '$size2', '$type2',
'$form_description3', '$data3', '$name3', '$size3', '$type3',
'$form_description4', '$data4', '$name4', '$size4', '$type4',
'$form_description5', '$data5', '$name5', '$size5', '$type5')";
$Opdracht = mysql_query($query) or die(mysql_error());

print ("De opdracht is:<br />$Opdracht<P>\n");

if(mysql_affected_rows($Verbinding) == 1)
{
print ("De opdracht is met succes uitgevoerd!<br>\n");
}
else
{
print ("De opdracht kon niet worden uitgevoerd!<br>\n");
}
mysql_close ($Verbinding);
?>

The error I get is this:
Warning: Wrong parameter count for file_exists() in xhandleaanbod.php
on line 91

Nope! Try again!
De opdracht is:
1
De opdracht is met succes uitgevoerd!

How do I alter the "parameter count" for these multiple uploads?

Jul 17 '05 #6

P: n/a
pescott <go****@pescott.com> wrote:
I modified the handling script quite a bit, and there are no parsing
errors, but still a warning and a db entry error. .... The error I get is this:
Warning: Wrong parameter count for file_exists() in xhandleaanbod.php
on line 91


http://nl3.php.net/file_exists
and
http://nl3.php.net/manual/en/functio...oaded-file.php

will tell you the correct syntax for the parse error you are getting.

Jul 17 '05 #7

P: n/a
I got this, check the filename or directory to see if it exists, but
how about the syntax for multiple files?

if (file_exists($tmp_name1)) {

but for multiple files, do I add an if statement for each filename, or
can I check to see if they all exist in one go? If I am supposed to
create if statements for each tmp_name, how fdo I combine these
statements? Howzitdun?

if (file_exists($tmp_name1, $tmp_name2)) {

Jul 17 '05 #8

P: n/a
On 23 Apr 2005 09:42:09 -0700, "pescott" <go****@pescott.com> wrote:
I got this, check the filename or directory to see if it exists, but
how about the syntax for multiple files?

if (file_exists($tmp_name1)) {

but for multiple files, do I add an if statement for each filename, or
can I check to see if they all exist in one go? If I am supposed to
create if statements for each tmp_name, how fdo I combine these
statements? Howzitdun?

if (file_exists($tmp_name1, $tmp_name2)) {


if (file_exists($tmp_name1) && file_exists($tmp_name2)) {

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #9

P: n/a
I sure am appreciative of all this help I am getting here, I am on my
way, but struggling still. In the db now I have the file description,
but nothing else. I have so far::

// store the file information to variables for easier access 1
$tmp_name1 = $_FILES['form_data1']['tmp_name'];
$type1 = $_FILES['form_data1']['type'];
$name1 = $_FILES['form_data1']['name'];
$size1 = $_FILES['form_data1']['size'];

// store the file information to variables for easier access 2
$tmp_name2 = $_FILES['form_data2']['tmp_name'];
$type2 = $_FILES['form_data2']['type'];
$name2 = $_FILES['form_data2']['name'];
$size2 = $_FILES['form_data2']['size'];

// store the file information to variables for easier access 3
$tmp_name3 = $_FILES['form_data3']['tmp_name'];
$type3 = $_FILES['form_data3']['type'];
$name3 = $_FILES['form_data3']['name'];
$size3 = $_FILES['form_data3']['size'];

// store the file information to variables for easier access 4
$tmp_name4 = $_FILES['form_data4']['tmp_name'];
$type4 = $_FILES['form_data4']['type'];
$name4 = $_FILES['form_data4']['name'];
$size4 = $_FILES['form_data4']['size'];

// store the file information to variables for easier access 5
$tmp_name5 = $_FILES['form_data5']['tmp_name'];
$type5 = $_FILES['form_data5']['type'];
$name5 = $_FILES['form_data5']['name'];
$size5 = $_FILES['form_data5']['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name1) && file_exists($tmp_name2) &&
file_exists($tmp_name3) && file_exists($tmp_name4) &&
file_exists($tmp_name5)) {

// check to make sure that it is an uploaded file
if(is_uploaded_file($tmp_name1) && is_uploaded_file($tmp_name2) &&
is_uploaded_file($tmp_name3) && is_uploaded_file($tmp_name4) &&
is_uploaded_file($tmp_name5)) {

// open the file for a binary read
$file1 = fopen($tmp_name1,'rb');
$file2 = fopen($tmp_name2,'rb');
$file3 = fopen($tmp_name3,'rb');
$file4 = fopen($tmp_name4,'rb');
$file5 = fopen($tmp_name5,'rb');

// read the file content into a variable
$data1 = fread($file1,filesize($tmp_name1));
$data2 = fread($file2,filesize($tmp_name2));
$data3 = fread($file3,filesize($tmp_name3));
$data4 = fread($file4,filesize($tmp_name4));
$data5 = fread($file5,filesize($tmp_name5));

// close the file
fclose($file1) && fclose ($file2) && fclose($file3) && fclose
($file4) && fclose ($file5);

// now we encode it and split it into acceptable length lines
$data1 = chunk_split(base64_encode($data1));
$data2 = chunk_split(base64_encode($data2));
$data3 = chunk_split(base64_encode($data3));
$data4 = chunk_split(base64_encode($data4));
$data5 = chunk_split(base64_encode($data5));
}

}

else {
print("<br><b>Nope, not yet...</b><br>\n");
}

$query = "INSERT into $Tabelnaam (adres,verkocht,
description1,bin_data1,filename1,filesize1,filetyp e1,
description2,bin_data2,filename2,filesize2,filetyp e2,
description3,bin_data3,filename3,filesize3,filetyp e3,
description4,bin_data4,filename4,filesize4,filetyp e4,
description5,bin_data5,filename5,filesize5,filetyp e5)
values
('$adres', '$verkocht',
'$form_description1', '$data1', '$name1', '$size1', '$type1',
'$form_description2', '$data2', '$name2', '$size2', '$type2',
'$form_description3', '$data3', '$name3', '$size3', '$type3',
'$form_description4', '$data4', '$name4', '$size4', '$type4',
'$form_description5', '$data5', '$name5', '$size5', '$type5')";
$Opdracht = mysql_query($query) or die(mysql_error());

print ("De opdracht is:<br />$Opdracht<P>\n");

if(mysql_affected_rows($Verbinding) == 1)
{
print ("De opdracht is met succes uitgevoerd!<br>\n");
}
else
{
print ("De opdracht kon niet worden uitgevoerd!<br>\n");
}
mysql_close ($Verbinding);

And this is the returned error:

Nope, not yet...
De opdracht is:
1
De opdracht is met succes uitgevoerd!

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.