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

Image storing issues / File handling

P: n/a
I am still struggling to get some files uploaded to a database as BLOB
data. I have 5 includes which allows the user to upload 5 files,
numbered accordingly. However, there are script errors. Duhh, why else
would I post? Well, here goes:

CODE
$adres = trim (htmlspecialchars($_POST["adres"]));
$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 succeeded, 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 and not a system 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>Is this thing working? At all?</b><br>\n");
}
}
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:
QUOTE
Nope, not yet...
De opdracht is:
1
De opdracht is met succes uitgevoerd!
What's wrong with checking if the file exists, because that is where
the error message seems to come from...

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


P: n/a
pescott <go****@pescott.com> wrote:
I am still struggling to get some files uploaded to a database as BLOB
data. I have 5 includes which allows the user to upload 5 files,
numbered accordingly. However, there are script errors. Duhh, why else
would I post? Well, here goes:
Why not start with 1 file first?

[snip] // if the upload succeeded, 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)) { [snip] What's wrong with checking if the file exists, because that is where
the error message seems to come from...


You do know what that line of code represents? It appears the condition
isn't true, how do you know that it indeed should be true?

Jul 17 '05 #2

P: n/a
Yes I understand. The problem I have, even when trying to upload one
file, taht it does not seem to ne opened, but for the life of me, I
cannot figure out why that is. Unless of course I am missing vital
informattion in this handlescript...
So the error I get indeed suggests that file_exist() is false, for one
or all of the files. But browsing the file and passing it on to the
handlescript, including a file description, return an empty field in
the db where the data should be stored, but the description makes it
in. None of the other info does, however, i.e. name, size, type, and
binary data. So what am I missing here?

Jul 17 '05 #3

P: n/a
On 25 Apr 2005 07:35:45 -0700, "pescott" <go****@pescott.com> wrote:
Yes I understand. The problem I have, even when trying to upload one
file, taht it does not seem to ne opened, but for the life of me, I
cannot figure out why that is. Unless of course I am missing vital
informattion in this handlescript...
So the error I get indeed suggests that file_exist() is false, for one
or all of the files. But browsing the file and passing it on to the
handlescript, including a file description, return an empty field in
the db where the data should be stored, but the description makes it
in. None of the other info does, however, i.e. name, size, type, and
binary data. So what am I missing here?


When you don't know what you're missing, there's a couple of things to try
first before taking the code apart too much: start printing things out, or
simplify.

You could print out the $_FILES array, this is likely to give various bits of
information.

print "<pre>";
var_dump($_FILES); // or even: print_r($_FILES); - depends which you find more
readable.
print "</pre>";

If that doesn't help, print out more variables.

To simplify, you could cut it down to just one file instead of five.

You are uploading all five files when you test it, right? Since that's what
you've got a condition for.

--
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
The answer to the dump command is:

array(0),

so I gather that nothing makes is to the handling page. Does that mean
teh problem is located elsewhere? I don't know how to fix this thing...

Jul 17 '05 #5

P: n/a
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:4k********************************@4ax.com...
On 25 Apr 2005 07:35:45 -0700, "pescott" <go****@pescott.com> wrote:
Yes I understand. The problem I have, even when trying to upload one
file, taht it does not seem to ne opened, but for the life of me, I
cannot figure out why that is. Unless of course I am missing vital
informattion in this handlescript...
So the error I get indeed suggests that file_exist() is false, for one
or all of the files. But browsing the file and passing it on to the
handlescript, including a file description, return an empty field in
the db where the data should be stored, but the description makes it
in. None of the other info does, however, i.e. name, size, type, and
binary data. So what am I missing here?
When you don't know what you're missing, there's a couple of things to

try first before taking the code apart too much: start printing things out, or
simplify.

You could print out the $_FILES array, this is likely to give various bits of information.

print "<pre>";
var_dump($_FILES); // or even: print_r($_FILES); - depends which you find more readable.
print "</pre>";

If that doesn't help, print out more variables.

To simplify, you could cut it down to just one file instead of five.

You are uploading all five files when you test it, right? Since that's what you've got a condition for.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool


You will never be able to check for the files in that manner - you are
assuming that 5 files will be uploaded every time... try using a loop
instead

for ($loop = 1; $loop <= 5; $loop++)
{

if (isset($_FILES["form_data$loop"]) &&
file_exists($_FILES["form_data$loop"]) &&
is_uploaded_file($_FILES["form_data$loop"]))
{
${form_description$loop} =
trim(htmlspecialchars($_POST["form_description$loop"]));

${tmpname$loop} = $_FILES["form_data$loop"]['tmp_name']);
${type$loop} = $_FILES["form_data$loop"]['type']);
${name$loop} = $_FILES["form_data$loop"]['name']);
${size$loop} = $_FILES["form_data$loop"]['size']);

${file$loop} = fopen(${tmpname$loop},'rb');
${data$loop} = fread(${file$loop},filesize(${tmpname$loop}));
fclose(${file$loop});
${data$loop} = chunk_split(base64_encode(${data$loop}));
}
else
{
${form_description$loop} = 'no description';
${tmpname$loop} = 'no file';
${type$loop} = 'no type';
${name$loop} = 'no name';
${size$loop} = '0';
}
}

$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());

---

....untested... but should work... I love dynamic variables! ;)

Norm
---
FREE Avatar Hosting at www.easyavatar.com

Jul 17 '05 #6

P: n/a
<?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"]));
$postcode = trim (htmlspecialchars($_POST["postcode"]));
$stad = trim (htmlspecialchars($_POST["stad"]));
$wijk = trim (htmlspecialchars($_POST["wijk"]));
$vraagprijs = trim (htmlspecialchars($_POST["vraagprijs"]));
$type = trim (htmlspecialchars($_POST["type"]));
$oppervlakte = trim (htmlspecialchars($_POST["oppervlakte"]));
$perceel = trim (htmlspecialchars($_POST["perceel"]));
$inhoud = trim (htmlspecialchars($_POST["inhoud"]));
$kamers = trim (htmlspecialchars($_POST["kamers"]));
$omschrijving = trim (htmlspecialchars($_POST["omschrijving"]));
$bouwjaar = trim (htmlspecialchars($_POST["bouwjaar"]));
$kenmerken = trim (htmlspecialchars($_POST["kenmerken"]));
$balkon = trim (htmlspecialchars($_POST["balkon"]));
$tuin = trim (htmlspecialchars($_POST["tuin"]));
$garage = trim (htmlspecialchars($_POST["garage"]));
$berging = trim (htmlspecialchars($_POST["berging"]));
$verwarming = trim (htmlspecialchars($_POST["verwarming"]));
$cv = trim (htmlspecialchars($_POST["cv"]));
$soortcv = trim (htmlspecialchars($_POST["soortcv"]));
$bouwjaarcv = trim (htmlspecialchars($_POST["bouwjaarcv"]));
$isolatie = trim (htmlspecialchars($_POST["isolatie"]));
$oplevering = trim (htmlspecialchars($_POST["oplevering"]));
$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"]));

for ($loop = 1; $loop <= 5; $loop++)
{

if (isset($_FILES["form_data$loop"]) &&
file_exists($_FILES["form_data$loop"]) &&
is_uploaded_file($_FILES["form_data$loop"]))
{
${form_description$loop} =
trim(htmlspecialchars($_POST["form_description$loop"]));

${tmpname$loop} = $_FILES["form_data$loop"]['tmpname']);
${type$loop} = $_FILES["form_data$loop"]['type']);
${name$loop} = $_FILES["form_data$loop"]['name']);
${size$loop} = $_FILES["form_data$loop"]['size']);

${file$loop} = fopen(${tmpname$loop},'rb');
${data$loop} = fread(${file$loop},filesize(${tmpname$loop}));
fclose(${file$loop});
${data$loop} = chunk_split(base64_encode(${data$loop}));
}
else
{
${form_description$loop} = 'no description';
${tmpname$loop} = 'no file';
${type$loop} = 'no type';
${name$loop} = 'no name';
${size$loop} = '0';
}

}

$query = "INSERT into $Tabelnaam
(adres,postcode,stad,wijk,vraagprijs,type,oppervla kte,perceel,
inhoud,kamers,omschrijving,bouwjaar,kenmerken,balk on,tuin,garage,
berging,verwarming,cv,soortcv,bouwjaarcv,isolatie, oplevering,verkocht,
description1,bin_data1,filename1,filesize1height,
filesize1width,filetype1,
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', '$postcode', '$stad', '$wijk', '$vraagprijs', '$type',
'$oppervlakte', '$perceel',
'$inhoud', '$kamers', '$omschrijving', '$bouwjaar', '$kenmerken',
'$balkon', '$tuin', '$garage',
'$berging', '$verwarming', '$cv', '$soortcv', '$bouwjaarcv',
'$isolatie', '$oplevering', '$verkocht',
'$form_description1', '$data1', '$name1', '".$size1[1]."',
'".$size1[0]."', '$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);
?>

Using this code I now get a parse error on line 65 which is this line

${form_description$loop} =
trim(htmlspecialchars($_POST["form_description$loop"]));
Am I supposed to cut the section which the trimming of the description
out of the code? And I do not understand the loop very well... Thank
you for your input!

Jul 17 '05 #7

P: n/a
<?php

$Host = "localhost";
$Gebruiker = "";
$Wachtwoord = "";
$DBNaam = "";
$Tabelnaam = "aanbod";

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

mysql_select_db ("$DBNaam");

$adres = trim (htmlspecialchars($_POST["adres"]));
$postcode = trim (htmlspecialchars($_POST["postcode"]));
$stad = trim (htmlspecialchars($_POST["stad"]));
$wijk = trim (htmlspecialchars($_POST["wijk"]));
$vraagprijs = trim (htmlspecialchars($_POST["vraagprijs"]));
$type = trim (htmlspecialchars($_POST["type"]));
$oppervlakte = trim (htmlspecialchars($_POST["oppervlakte"]));
$perceel = trim (htmlspecialchars($_POST["perceel"]));
$inhoud = trim (htmlspecialchars($_POST["inhoud"]));
$kamers = trim (htmlspecialchars($_POST["kamers"]));
$omschrijving = trim (htmlspecialchars($_POST["omschrijving"]));
$bouwjaar = trim (htmlspecialchars($_POST["bouwjaar"]));
$kenmerken = trim (htmlspecialchars($_POST["kenmerken"]));
$balkon = trim (htmlspecialchars($_POST["balkon"]));
$tuin = trim (htmlspecialchars($_POST["tuin"]));
$garage = trim (htmlspecialchars($_POST["garage"]));
$berging = trim (htmlspecialchars($_POST["berging"]));
$verwarming = trim (htmlspecialchars($_POST["verwarming"]));
$cv = trim (htmlspecialchars($_POST["cv"]));
$soortcv = trim (htmlspecialchars($_POST["soortcv"]));
$bouwjaarcv = trim (htmlspecialchars($_POST["bouwjaarcv"]));
$isolatie = trim (htmlspecialchars($_POST["isolatie"]));
$oplevering = trim (htmlspecialchars($_POST["oplevering"]));
$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"]));

for ($loop = 1; $loop <= 5; $loop++)
{

if (isset($_FILES["form_data$loop"]) &&
file_exists($_FILES["form_data$loop"]) &&
is_uploaded_file($_FILES["form_data$loop"]))
{
${form_description$loop} =
trim(htmlspecialchars($_POST["form_description$loop"]));

${tmpname$loop} = $_FILES["form_data$loop"]['tmpname']);
${type$loop} = $_FILES["form_data$loop"]['type']);
${name$loop} = $_FILES["form_data$loop"]['name']);
${size$loop} = $_FILES["form_data$loop"]['size']);

${file$loop} = fopen(${tmpname$loop},'rb');
${data$loop} = fread(${file$loop},filesize(${tmpname$loop}));
fclose(${file$loop});
${data$loop} = chunk_split(base64_encode(${data$loop}));
}
else
{
${form_description$loop} = 'no description';
${tmpname$loop} = 'no file';
${type$loop} = 'no type';
${name$loop} = 'no name';
${size$loop} = '0';
}

}

$query = "INSERT into $Tabelnaam
(adres,postcode,stad,wijk,vraagprijs,type,oppervla kte,perceel,
inhoud,kamers,omschrijving,bouwjaar,kenmerken,balk on,tuin,garage,
berging,verwarming,cv,soortcv,bouwjaarcv,isolatie, oplevering,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', '$postcode', '$stad', '$wijk', '$vraagprijs', '$type',
'$oppervlakte', '$perceel', '$inhoud', '$kamers', '$omschrijving',
'$bouwjaar', '$kenmerken', '$balkon', '$tuin', '$garage', '$berging',
'$verwarming', '$cv', '$soortcv', '$bouwjaarcv', '$isolatie',
'$oplevering', '$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);
?>
Using this code I now get a parse error on line 65 which is this line

${form_description$loop} =
trim(htmlspecialchars($_POST["*form_description$loop"]));
Am I supposed to cut the section which the trimming of the description
out of the code? And I do not understand the loop very well... Thank
you for your input!

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.