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

Uploads with MySQL

P: n/a
Troubles getting a file upload into a MySQL DB to work. Any help is
appreciated.

It keeps arriving at up.php without any get data, but has worked before.
It's unpredictable based on slight form and php variations, I can't get it
running smoothly again.

<?php if (empty($image) || empty($userfile))
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PhotoShare</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="rstyle1.css" />
</head>
<body>
<div id="frame">
<div id="head">
<img src="./img/ps.jpg"/>
</div>
<div id="menu">
<font face="Comic Sans MS, Arial" size="+1" color="#FFFFFF"><font
color="#0099FF">view & edit album</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add annotations
</font>
</div>
<div id="content"><font color="#FFFFFF"><center>
<h4>Add a picture.</h4><br/>
<form action="up.php" enctype="multipart/form-data" method="post">
<h4>Image name: </h4><input name="name" type="text"><br/>
<h4>Image(JPEG) to Upload</h4><input name="userfile" type="file"><br/><br/>
<input name="submit" type="submit">
</form>
</center></font></div>
</div>
</body>
</html>

<?php
} else {
$connection = mysql_connect ("localhost", "USERNAME", "PASS") or die ('I
cannot connect to the database because: ' . mysql_error());
mysql_select_db ("DATABASE", $connection);
if (is_uploaded_file($userfile))
{
// Open the uploaded file
$file = fopen($userfile, "r");

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

// Escape special charcters in the file
$fileContents = AddSlashes($fileContents);
}
else
$fileContents = NULL;

$album = $HTTP_GET_VARS['albumname'];
if ((@ mysql_query ("INSERT INTO image VALUES (\"$name\", \"$fileCont
ents\", \"$album\", \"\")",
$connection))
&& @ mysql_affected_rows() == 1) {
header("Location: up.php?" .
"sqlID=". mysql_insert_id($connection) .
"&status=T");
}
else {
header("Location: up.php?" .
"status=F" . "&sql=" . mysql_error());
}
}
?>

Thanks,
Robert
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.