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()
?>
uploading images
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
Have a read of this:
http://www.php.net/manual/en/features.file-upload.php
Mac
uploading images
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>
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>
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
Mac
Code: Select all
<html>
<head><title>Store binary data into SQL Database</title></head>
<body>
<?php
if (!isset($_POSTї'submit'])) {
// Page hasn't been submitted so show form
?>
<form method="post" action="<?php echo $_SERVERї'PHP_SELF']; ?>" enctype="multipart/form-data">
<label for="filedesc">File Description:</label><br />
<input type="text" name="form_description" size="40" id="filedesc" />
<label for="sendfile">Send this file:</label><br />
<input name="userfile" type="file" id="sendfile" />
<input type="hidden" name="max_file_size" value="1000000" />
<p><input type="submit" name="submit" value="Send File" /></p>
</form>
<?php
} else {
/* Show $_FILES */
echo '<pre>';
print_r($_FILES);
echo '</pre>';
/* Show $_POST */
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
</body>
</html>uploading images
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
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Hi Gecko...
...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
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
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
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