[SOLVED] $_FILES not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jdeutsch
Forum Newbie
Posts: 2
Joined: Tue Mar 01, 2005 7:19 pm

[SOLVED] $_FILES not working

Post by jdeutsch »

I have the following two php files that I'm using together to update records in a news database for a website I'm working on (update.php and updated.php - both below).

However, the $_FILES variable, when used with the array I've got and the ['type'] setting does not appear to be working. Specifically what is happening is this: The records get called from the database, however, if I try and change the image by uploading it through the file input field in the form, I receive the error that the file type is not in the array I have set up (regardless of the extension).

The array specifies jpg, gif and png images, however files with any of these extensions will not be uploaded using this form (but will be using other forms and the same $_FILES settings and array).

I am using the same array and ['type'] attribute on two other pages, and both work flawlessly. I've been going crazy trying to figure out what the problem is and was hoping that someone might be able to tell me, or just let me know if there's some small thing I'm missing as I look over the files.

Thanks.

UPDATE.PHP

Code: Select all

<?php

$id=$_GET&#1111;'id'];

include("dbinfo.inc.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM news WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

$i=0;
while ($i < $num) &#123;
$id=mysql_result($result,$i,"id");
$headline=mysql_result($result,$i,"headline");
$name=mysql_result($result,$i,"author");
$date=mysql_result($result,$i,"date");
$time=mysql_result($result,$i,"time");
$section=mysql_result($result,$i,"section");
$story=mysql_result($result,$i,"story");
$blurb=mysql_result($result,$i,"blurb");
$subsection=mysql_result($result,$i,"subsection");
$image=mysql_result($result,$i,"image");
$caption=mysql_result($result,$i,"caption");

?>
<html>
<head>
<script src="login.js"></script>
<script language="JavaScript">
  checkCookie();
</script>
<title>The Wagnerian > Contact Administration > Update a Database Entry</title>
<link rel="stylesheet" href="../../css/main.css" type="text/css">
</head>
<h1>Update a News Database Entry</h1>

<form action="updated.php" method="post">
<input type="hidden" name="udid" value="<? echo "$id"; ?>">
<table>
<tr>
<td colspan="2"><font size="+1"><i>Please re-enter all of the information below.</i></font></td>
</tr>
<tr>
<td>Headline:</td><td><input type="text" name="udheadline" value="<? echo "$headline"; ?>" size="100"></td>
</tr>
<tr>
<td>Byline:</td><td><input type="text" name="udname" value="<? echo "$name"; ?>" size="100"></td>
</tr>
<tr>
<td>Date:</td><td><input type="text" name="uddate" value="<? echo "$date"; ?>"></td>
</tr>
<tr>
<td>Time:</td><td><input type="text" name="udtime" value="<? echo "time"; ?>"></td>
</tr>
<tr>
<td colspan="2"><i>Please choose the appropriate section that the article being submitted will appear in.</i><br /><font size="-1"><font class="red"><i>SECTION LISTING:</i></font><br /><b>BREAKING <font class="red">type: breaking</font></b><br /><b>NEWS <font class="red">type: news</font></b><br /><b>FEATURES <font class="red">type: features</font></b><br /><b>OPINIONS <font class="red">type opinion</font></b><br /><b>SPORTS <font class="red">type: sports</font></b></font></td>
</tr>
<tr>
<td>For Section:</td><td><input type="text" name="udsection" value="<? echo "$section"; ?>"></td>
</tr>
<tr>
<td colspan="2"><i>For each section, please choose the appropriate subsection that the story will fall into.<br /><font size="-1"> <font class="red">SUBSECTION LISTING:</font><br /><b>NEWS SUBSECTIONS:</b><br />Campus News <font class="red">type: campus</font><br /><b>FEATURES SUBSECTIONS:</b><br />Staff/Faculty Bios <font class="red">type: bios</font><br />Campus Events <font class="red">type: campevnt</font><br />Off-Campus Events <font class="red">type: offcampevnt</font><br />Movies <font class="red">type: movies</font><br />Music <font class="red">type: music</font><br /><b>OPINIONS SUBSECTIONS:</b><br />Editorials <font class="red">type: editorial</font><br />Letters to the Editors <font class="red">type: letters</font><br /><b>SPORTS SUBSECTIONS:</b><br />Men's Baseball <font class="red">type: menbaseball</font><br />Men's Basketball <font class="red">type: menbball</font><br />Men's Football <font class="red">type: menfball</font><br />Men's Other <font class="red">type: menothersport</font><br />Women's Basketball <font class="red">type: womenbball</font><br />Women's Softball <font class="red">type: womensoftball</font><br />Women's Other <font class="red">type: womenothersport</font></td>
</tr>
<tr>
<td>For Sub-Section:</td><td><input type="text" name="udsubsection" value="<? echo "$subsection"; ?>"></td>
</tr>
<tr>
<td>Story:</td><td><textarea name="udstory" rows="10" cols="50"><? echo "$story"; ?></textarea></td>
</tr>
<tr>
<td colspan="2"><i>Insert a short description of the story below:</i></td>
</tr>
<tr>
<td>Blurb:</td><td><textarea name="udblurb" rows="5" cols="50"><? echo "$blurb"; ?></textarea></td>
</tr>
<tr>
<td colspan="2"><i>At the moment, you may not update the file you have uploaded with this story.  Please check back as this is still under construction.</i></td>
</tr>
<tr>
<td>Image:</td><td><input type="file" name="userfile"></td>
</tr>
<tr>
<td colspan="2"><i>Please enter a caption below for your image:</i></td>
</tr>
<tr>
<td>Caption:</td><td><textarea name="udcaption" rows="5" cols="50"><? echo "$caption"; ?></textarea></td>
</tr>
<tr>
<td><input type="Submit" value="Update"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td> <form action="" name="frmLogoff">
  <input type="button" name="btLogoff" value="Log Out" onClick="logout();">
</form></td>
</tr>
</table>
</form>

<?
++$i;
&#125; 
?>

UPDATED.PHP

Code: Select all

<?

$allowed = array('image/gif', 'image/pjpeg', 'image/png');

if (in_array($_FILES&#1111;'userfile']&#1111;'type'], $allowed)) &#123;
   
   move_uploaded_file ($_FILES&#1111;'userfile'] &#1111;'tmp_name'], 
       "../images/&#123;$_FILES&#1111;'userfile'] &#1111;'name']&#125;");

$udid=$_POST&#1111;'udid'];
$udheadline=$_POST&#1111;'udheadline'];
$udname=$_POST&#1111;'udname'];
$uddate=$_POST&#1111;'uddate'];
$udtime=$_POST&#1111;'udtime'];
$udsection=$_POST&#1111;'udsection'];
$udstory=$_POST&#1111;'udstory'];
$udblurb=$_POST&#1111;'udblurb'];
$udimage=$_FILES&#1111;'userfile']&#1111;'name'];
$udcaption=$_POST&#1111;'udcaption'];

include("dbinfo.inc.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="UPDATE news SET  id='$udid',headline='$udheadline',author='$udname',date='$uddate',time='$udtime',section='$udsection',story='$udstory',blurb='$udblurb',image='$udimage',caption='udcaption' WHERE id='$udid'";
mysql_query($query);
mysql_close();

?>
<html><head><title>Update a Story Entry</title> <script src='login.js'></script> <script language='JavaScript'>  checkCookie(); </script></head><body><h1>Record Updated</h1><p>You've successfully updated the record for <b>$udheadline</b>.</p><p><a href=select.html>Click here</a> to return to the selection page.</p><p><a href=update.html>Click here</a> to update another record.  Or <a href=viewnews.php>click here</a> to see the information currently in the database.</p><p><a href=../../index.html>Click here</a> to return to the home page.</p>. <form action='' name='frmLogoff'>  <input type='button' name='btLogoff' value='Log Out' onClick='logout();'> </form></body></html>";
<?
&#125;
else &#123;
     echo "The database record for this story has not been set because the file type of the image you have included is not valid.  Please return to the <a href=add.html>add page</a> and try again using supported file types: (<b>.jpg</b>, <b>.gif</b> or <b>.png</b>)";
&#125;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it would appear you forgot to set enctype in the form.
jdeutsch
Forum Newbie
Posts: 2
Joined: Tue Mar 01, 2005 7:19 pm

Post by jdeutsch »

Thanks feyd. I feel just a little silly. I was going nuts for days trying to figure this out, and I didn't even think to check the HTML.

Thanks again.
Post Reply