Page 1 of 1

File Upload from Form not returning resource correctly

Posted: Tue Feb 04, 2003 9:22 am
by danielw1
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:

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>
And the code for uploading it:

Code: Select all

function content_admin_add_story()
&#123;
	if($_POST&#1111;"storytype"]=="feature")
	&#123;
		mysql_connect("localhost", "smnw_global", "rogue333");
		mysql_select_db("smnw_content");

		$feature_pic = $_POST&#1111;"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))
		//&#123;
			// 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>";
		//&#125;
		//else
		//	die("Error: Feature picture not set correctly.  Please report this problem and try again.");


		$query="INSERT INTO feature_stories VALUES ('" . $_POST&#1111;"headline"] . "', '" . $_POST&#1111;"byline"] . "', '" . $_POST&#1111;"date"] . "', '" . $_POST&#1111;"teaser"] . "', '" . $_POST&#1111;"bodytext"] . "', NULL, '" . $fileContents . "')";
		
		echo $query;
		
		if(mysql_query($query))
		&#123;
			echo "The story ", $headline, " has been added.";
		&#125;
		else
		&#123;
			echo "There was an error in inserting the story.  Please report this and try again.";
		&#125;
	&#125;
	else if($_POST&#1111;"storytype"]=="generic")
	&#123;
		mysql_connect("localhost", "smnw_global", "rogue333");
		mysql_select_db("smnw_content");

		$query="INSERT INTO generic_stories VALUES ('" . $_POST&#1111;"headline"] . "', '" . $_POST&#1111;"byline"] . "', '" . $_POST&#1111;"date"] . "', '" . $_POST&#1111;"teaser"] . "', '" . $_POST&#1111;"bodytext"] . "', '" . $_POST&#1111;"section"] . "', NULL" . ");";

echo $query;

		if(mysql_query($query))
		&#123;
			echo "The story ", $headline, " has been added.";
		&#125;
		else
		&#123;
			echo "There was an error in inserting the story.  Please report this and try again.";
		&#125;
	&#125;
	else
	&#123;
		echo "There is an error.  Somewhere, somehow, something went horribly wrong.  Please report this and try again.  From the beginning.";
  echo $storytype;
	&#125;
&#125;
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.

Posted: Tue Feb 04, 2003 12:06 pm
by daven
You need to add <input type="hidden" name="MAX_FILE_SIZE" value="your-max-allowed-size"> when you have a form type="multipart/etc".

I also like using the _FILES variable for uploading stuff, but that is just a preference. Anyhow, try the below tweaks to the form. I also included code using _FILES, just because I can. :-)

The form (MAX_FILE_SIZE should be the first item in your form):

Code: Select all

&lt;form type="multipart/form-data" action="action" method="post"&gt;
  &lt;input type="hidden" name="MAX_FILE_SIZE" value="1000000"&gt;
  . . . the rest of your code . . .
  &lt;input type="file" name="local_file"&gt;
&lt;/form&gt;
Processing code using $_FILES[]

Code: Select all

<?php
if($_FILES['local_file']['name']!=""){ 
  if(filesize($_FILES['local_file']['tmp_name'])>0){
    $file_Name=$_FILES['icon_path']['name'];
    $file_Path="/absolut/path/to/file/".$Icon;
    copy($_FILES['local_file']['tmp_name'], $file_Path) or die("did not upload");
   }
}
?>

Posted: Tue Feb 04, 2003 1:26 pm
by danielw1
I checked my working program... here's the form from that:

Code: Select all

<html>
<head>
<title>smnw.com :: Art Gallery :: Admin :: New Image</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" leftmargin=0 rightmargin=0 marginwidth=0 marginheight=0 topmargin=0>
<p><img src="http://www.smnw.com/gallery/graphics/artgal.png" width=400 height=100> 
</p>
<table width=100% border=0>
<tr>
<td width=10%>
</td>
    <td width=90%>


	Adding an image<img src="http://www.smnw.com/images/ellipsis.gif" height=10 width=30>
	
<form name="artistjump" action="add_image2.php" method="post" enctype="multipart/form-data">

<select name="artistID">
<!-- selects go here, I deleted them for forum since they are irrelevant -->
</select>
<input type="submit" value="Go">
</form>

<form action="add_image.php" method="post" enctype="multipart/form-data" name="AddImage">
<input type="hidden" value="3" name="artistID">

3
<table width=100%>

<tr>
<td width=20%>
Image Title:
</td>
<td>
 <input name="title" type="text" size="30" maxlength="50">
      </td>
</tr>

<tr>
<td width=20% valign=top>
Image Description:
</td>
            <td valign="top"><textarea name="desc" cols="60" rows="20"></textarea> 
            </td>
</tr>


<tr>
	<td width=20% valign=top>
		Media:
	</td>
	<td valign="top">
		<input type="text" name="media" size="30" maxlength="20">
    </td>
</tr>


<tr>
<td width=20% valign=top>
Image:
</td>
<td valign="top">
 <input name="image" type="file" size="30">
      </td>
</tr>

<tr>
<td width=20% valign=top>
Thumbnail:
</td>
<td valign="top">
 <input name="thumb" type="file" size="30">
      </td>
</tr>

<tr>
<td width=20% valign=top>

</td>
<td valign="top">
 <table width="200">
  <tr>
    <td><label>
      <input type="radio" name="archive" value="TRUE">
      Archive</label></td>
  </tr>
  <tr>
    <td><label>
      <input type="radio" name="archive" value="FALSE">
      Do Not Archive</label></td>
  </tr>
</table>
<input type="submit" value="Submit">
      </td>
</tr>

</table>
</form>


</td>
</tr>
</table>
</body>
</html>
that seems to work, but the form above that I posted doesn't. Is there anywhere that I can look at a more in-depth tutorial on using files in PHP? I'll check the manual after I get done writing here, but if there's anyplace else that might be handy, I would appreciate letting me know.