File Upload from Form not returning resource correctly
Posted: Tue Feb 04, 2003 9:22 am
Currently I'm working on a script that uploads a file. It is basically two pages: The Form and The Script. The form has a few pieces of information for a news story, and then a type="file" form field. I've done this before with an art gallery script, and it worked just fine. Here is the form for the news one:
And the code for uploading it:
You will, of course, see quite a bit of my error checking printouts. Now, when I run the art gallery script, the uploaded file resource identifier (before fopen()) is this:
/tmp/phpFASdKq
After fopen() and the rest of the functions to prepare the file, it becomes a large blob of binary data.
In the new script (that doesn't work), the resource identifier used to be a local path to where the file is on the machine that is uploading. I added an enctype="multipart/form-data" and this simply made it null -- i.e. a blank space.
While this may be an HTML issue, I would appreciate any help and/or suggestions. The original code for the art gallery (which works) can be found at DevShed... I have class ending and must go, will return with the link after school.
Code: Select all
<html>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body>
<form action="admin_cms.php?operation=add_story&step=1" method="post" enctype="multipart/form-data" name="featureAdd>
<table width="800">
<tr>
<td valign=top>
Headline:
</td>
<td>
<input type="text" name="headline" size="50">
</td>
</tr>
<tr>
<td valign=top>
Date:
</td>
<td>
<input type="text" name="date" size="8">
</td>
</tr>
<tr>
<td valign=top>
Teaser:
</td>
<td>
<textarea name="teaser" rows="3" cols="40"></textarea>
</td>
</tr>
<tr>
<td valign=top>
Byline:
</td>
<td>
<input type="text" name="byline" size="30">
</td>
</tr>
<tr>
<td valign=top>
Bodytext:
</td>
<td>
<textarea name="bodytext" rows="15" cols="50"></textarea>
</td>
</tr>
<tr>
<td valign=top>
Feature Pic: </td>
<td>
<input name="feature_pic" type="file" size="30"> </td>
</tr>
</table>
<input type="hidden" value="feature" name="storytype">
<input type="submit" name="submit" value="Submit story">
</form>
</body>
</html>Code: Select all
function content_admin_add_story()
{
if($_POSTї"storytype"]=="feature")
{
mysql_connect("localhost", "smnw_global", "rogue333");
mysql_select_db("smnw_content");
$feature_pic = $_POSTї"feature_pic"];
echo $feature_pic;echo "<br><br><br><br>";
clean($feature_pic, 50);
echo $feature_pic;echo "<br><br><br><br>";
//if (is_uploaded_file($feature_pic))
//{
// Open the uploaded file
$file = fopen($feature_pic, "r");
// Read in the uploaded file
$fileContents = fread($file, filesize($feature_pic));
echo $fileContents;echo "<br><br><br><br>";
// Escape special characters in the file
$fileContents = AddSlashes($fileContents);
echo $filecontents;echo "<br><br><br><br>";
//}
//else
// die("Error: Feature picture not set correctly. Please report this problem and try again.");
$query="INSERT INTO feature_stories VALUES ('" . $_POSTї"headline"] . "', '" . $_POSTї"byline"] . "', '" . $_POSTї"date"] . "', '" . $_POSTї"teaser"] . "', '" . $_POSTї"bodytext"] . "', NULL, '" . $fileContents . "')";
echo $query;
if(mysql_query($query))
{
echo "The story ", $headline, " has been added.";
}
else
{
echo "There was an error in inserting the story. Please report this and try again.";
}
}
else if($_POSTї"storytype"]=="generic")
{
mysql_connect("localhost", "smnw_global", "rogue333");
mysql_select_db("smnw_content");
$query="INSERT INTO generic_stories VALUES ('" . $_POSTї"headline"] . "', '" . $_POSTї"byline"] . "', '" . $_POSTї"date"] . "', '" . $_POSTї"teaser"] . "', '" . $_POSTї"bodytext"] . "', '" . $_POSTї"section"] . "', NULL" . ");";
echo $query;
if(mysql_query($query))
{
echo "The story ", $headline, " has been added.";
}
else
{
echo "There was an error in inserting the story. Please report this and try again.";
}
}
else
{
echo "There is an error. Somewhere, somehow, something went horribly wrong. Please report this and try again. From the beginning.";
echo $storytype;
}
}/tmp/phpFASdKq
After fopen() and the rest of the functions to prepare the file, it becomes a large blob of binary data.
In the new script (that doesn't work), the resource identifier used to be a local path to where the file is on the machine that is uploading. I added an enctype="multipart/form-data" and this simply made it null -- i.e. a blank space.
While this may be an HTML issue, I would appreciate any help and/or suggestions. The original code for the art gallery (which works) can be found at DevShed... I have class ending and must go, will return with the link after school.