Page 1 of 1

uploading images

Posted: Thu Nov 14, 2002 1:57 am
by gecko
this script should upload images to my database. although i am using a form tag, i am not really passing any variables to an other page ( i am saying this becuase most of the problems i have had recently involve superglobals). any way the script is not sending any info whatsoever to my database. any ideas what might be going wrong here.

i have included my script below.

regards

hansie


<?php
include "./db.dummy.inc";

if (empty($short) || empty($userfile))
{
?>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Upload an Image File</title>
</head>
<body bgcolor="white">
<form method="post" action="insert.php" enctype="multipart/form-data">
<h1>Upload an Image File</h1>
<h3>Please fill in the details below to upload your file.
Fields shown in <font color="red">red</font> are mandatory.</h3>
<table>
<col span="1" align="right">

<tr>
<td><font color="red">Short description:</font></td>
<td><input type="text" name="short" size=50></td>
</tr>

<tr>
<td><font color="red">File:</font></td>
<td><input name="userfile" type="file"></td>
</tr>

<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
</form>
<h3>Click <a href="index.php">here</a> to browse the images instead.</h3>
</body>
</html>
<?php
}
else
{
$short = clean($short, 50);
$userfile = clean($userfile, 50);

if (!($connection = mysql_pconnect($hostName,
$username,
$password)))
showerror();

if (!mysql_select_db("images_1", $connection))
showerror();

// Was a file uploaded?
if (is_uploaded_file($userfile))
{

switch ($userfile_type)
{
case "image/gif";
$mimeName = "GIF Image";
break;
case "image/jpeg";
$mimeName = "JPEG Image";
break;
case "image/png";
$mimeName = "PNG Image";
break;
case "image/x-MS-bmp";
$mimeName = "Windows Bitmap";
break;
default:
$mimeName = "Unknown image type";
}

// Open the uploaded file
$file = fopen($userfile, "r");

// Read in the uploaded file
$filecontents = fread($file, filesize($userfile));

// Escape special characters in the file
$filecontents = AddSlashes($filecontents);
}
else
$filecontents = NULL;

$insertQuery = "INSERT INTO files VALUES (NULL, \"{$short}\", \"{$userfile_type}\", \"{$mimeName}\", \"{$filecontents}\")";

if (( mysql_query ($insertQuery, $connection)) && mysql_affected_rows() == 1)
header("Location: receipt.php?status=T&file=" . mysql_insert_id($connection));
else
header("Location: receipt.php?status=F&file=" . mysql_insert_id($connection));
} // if else empty()
?>

Posted: Thu Nov 14, 2002 3:07 am
by twigletmac
You need to use a superglobal array to access values passed from a form because although it may be passing them back to the same script it is still passing them.

Have a read of this:
http://www.php.net/manual/en/features.file-upload.php

Mac

uploading images

Posted: Thu Nov 14, 2002 3:39 am
by gecko
i have followed your link, but did not really get it to work.

i also realize that the piece of code i indluded in my message was somewhat extensive. i have now included some simpler code, i am simply not too sure where the superglobals come in here. i hope you can help.

thanks

hans

<?php

?>

<HTML>
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
<BODY>

<?php

$submit = (isset($_GET['submit'])) ? $_GET['submit'] : '';
$PHP_SELF = $_SERVER['PHP_SELF'];
// code that will be executed if the form has been submitted:

if ($submit) {

// connect to the database


MYSQL_CONNECT("localhost","administrator","tuatara");
mysql_select_db("binary_data");

$data = mysql_escape_string(fread(fopen($form_data, "rb"), filesize($form_data)));

$result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
"VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");

$id= mysql_insert_id();
print "<p>This file has the following Database ID: <b>$id</b>";

MYSQL_CLOSE();

} else {

// else show the form to submit new data:
?>

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<p><input type="submit" name="submit" value="Send File">
</form>

<?php

}

?>

</BODY>
</HTML>

Posted: Thu Nov 14, 2002 3:57 am
by twigletmac
All the information about the uploaded file is stored in the $_FILES array, try this code to see where the form variables are going to:

Code: Select all

&lt;html&gt; 
&lt;head&gt;&lt;title&gt;Store binary data into SQL Database&lt;/title&gt;&lt;/head&gt; 

&lt;body&gt; 
&lt;?php 

if (!isset($_POST&#1111;'submit'])) { 

	// Page hasn't been submitted so show form
?&gt;
&lt;form method="post" action="&lt;?php echo $_SERVER&#1111;'PHP_SELF']; ?&gt;" enctype="multipart/form-data"&gt; 
	&lt;label for="filedesc"&gt;File Description:&lt;/label&gt;&lt;br /&gt; 
	&lt;input type="text" name="form_description" size="40" id="filedesc" /&gt; 
	&lt;label for="sendfile"&gt;Send this file:&lt;/label&gt;&lt;br /&gt;
	&lt;input name="userfile" type="file" id="sendfile" /&gt;
	&lt;input type="hidden" name="max_file_size" value="1000000" /&gt; 
	&lt;p&gt;&lt;input type="submit" name="submit" value="Send File" /&gt;&lt;/p&gt;
&lt;/form&gt; 
&lt;?php
} else {
	/* Show $_FILES */
	echo '&lt;pre&gt;';
	print_r($_FILES);
	echo '&lt;/pre&gt;';
	/* Show $_POST */
	echo '&lt;pre&gt;';
	print_r($_POST);
	echo '&lt;/pre&gt;';
}
?&gt; 

&lt;/body&gt; 
&lt;/html&gt;
Mac

uploading images

Posted: Thu Nov 14, 2002 4:34 am
by gecko
hi mac,

using your code as it is simply returns the following:

Array
(
)

Array
(
[form_description] => wef
[max_file_size] => 1000000
[submit] => Send File
)

so it seems to pass the values form form_description etc. the top array stays empty however. has this got to do with the fact that this is an image?

hans

Posted: Thu Nov 14, 2002 5:42 am
by twigletmac
So when you choose a file nothing gets put in the $_FILES array but stuff is being sent in the $_POST array - have you checked in the folder that is supposed to be receiving the uploaded file to see if anything gets put there.

Mac

Hi Gecko...

Posted: Thu Nov 14, 2002 6:13 am
by Fari
...have you checked your php.ini file for all those settings you have to get right to pass global variables / enable file uploads etc????

I had the same problem i.e. not being able to load images to a database but circumnavigated the problem by copying them in a folder on the server and adding just the path to the files in a string in the _db_ . not so elegant for a solution but works!

Fari

uploading images

Posted: Thu Nov 14, 2002 3:29 pm
by gecko
i have now got the following result:

Array
(
[userfile] => Array
(
[name] => logo.gif
[type] => image/gif
[tmp_name] => C:\Apache\htdocs\images_2\temp\php94.tmp
[error] => 0
[size] => 5042
)

)

Array
(
[form_description] => image_1
[max_file_size] => 1000000
[submit] => Send File
)

but there is no image uploaded to the specified file. i am working on windows 2000, apache, php4.2. any ideas why this is not working for me.
i have checked the section on upload files on php.net and followed all the threads. there is one interesting link there to http://dave.imarc.net/php.php but that doesn't help me much for now.

hope anyone can help.

regards

hans