Problem uploading an image

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
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Problem uploading an image

Post by QbertsBrother »

hello all

i am having a problem uploading an image. could someone look at this code and maybe tell me where the error is? i have been :banghead: for the past few hours now.

it gets to the point of where it checks the the file type and it tells me that i can only upload jpegs or gifs even when i try to upload a jpeg or a gif.

any help would be great.

you can see where i am trying to do this on this page:

http://www.fireplacefacelift.com/index.php

thanks

Code: Select all

<?php
session_name('USESSID');
session_start();
include("include/dbconn.php");
$errors = array();
$fields = array();
$enter_contest_success_message = "";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
  <title>Fireplace Facelift</title>
 
    <link rel="stylesheet" type="text/css" href="css/style.css" />
<map name="fireside">
<area shape="rect" coords="414,17,585,74" href="http://www.fireside.com">
</map>
<script language="JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
 
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
 
//-->
</script>
</head>
 
<body bgcolor="#ffffff" onLoad="MM_preloadImages('images/layout/specialoffers_over.jpg','images/layout/why_over.jpg','images/layout/locations_over.jpg','images/layout/contact_over.jpg','images/layout/enter_over.jpg','images/layout/about_over.jpg','images/layout/rules_over.jpg','images/layout/winner_over.jpg');">
<div id="all">
<?php include('include/header.php'); ?>
 
<div id="navall">
<div id="nav1">
<img name="fpfl_r2_c1" src="images/layout/left_red.jpg" width="200" height="50" border="0" alt=""></div>
<div id="nav2">
<a href="specialoffers.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('fpfl_r2_c3','','images/layout/specialoffers_over.jpg',1);"><img name="fpfl_r2_c3" src="images/layout/specialoffers.jpg" width="149" height="50" border="0" alt=""></a></div>
<div id="nav3">
<a href="whyfireside.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('fpfl_r2_c4','','images/layout/why_over.jpg',1);"><img name="fpfl_r2_c4" src="images/layout/why.jpg" width="150" height="50" border="0" alt=""></a></div>
<div id="nav4">
<a href="locations.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('fpfl_r2_c5','','images/layout/locations_over.jpg',1);"><img name="fpfl_r2_c5" src="images/layout/locations.jpg" width="150" height="50" border="0" alt=""></a></div>
<div id="nav5">
<a href="contact.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('fpfl_r2_c6','','images/layout/contact_over.jpg',1);"><img name="fpfl_r2_c6" src="images/layout/contact.jpg" width="149" height="50" border="0" alt=""></a></div>
<div id="nav6">
<img name="fpfl_r2_c9" src="images/layout/right_red.jpg" width="202" height="50" border="0" alt=""></div>
</div>
 
 
<div id="leftspace">
<img src="images/layout/leftspace.jpg"></div>
 
 
<?php
if (isset($_POST['send-entercontest'])){
require("include/validation.php");
 
$rules = array();
//$rules[] = "required,first_name,Please enter your first name.";
//$rules[] = "required,last_name,Please enter your last name.";
//$rules[] = "required,email,Please enter your email address.";
//$rules[] = "valid_email,email,Please enter a valid email address.";
//$rules[] = "required,story,Please tell us your story.";
//$rules[] = "required,dreamfireplace,Please tell us about your dream fireplace.";
 
$errors = validateFields($_POST, $rules);
 
  // if there were errors, re-populate the form fields
  if (!empty($errors))
  {  
    $fields = $_POST;
  }
  
  // no errors! redirect the user to the thankyou page (or whatever)
  else 
  {
    $enter_contest_success_message = "Thank you! Your entry has been recieved.";
  }
  
if (!empty($errors))
{
  $outputerrors = "<div class='error'>Please fix the following errors:\n<ul>";
  foreach ($errors as $error)
    $outputerrors .= "<li>$error</li>\n";
 
  $outputerrors .= "</ul></div>";
  
}
if (!empty($enter_contest_success_message)){
function findexts ($filename) {
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
} 
if (!($_FILES['oldfireplaceimage']['type'] == "image/gif" || $_FILES['oldfireplaceimage']['type'] == "image/pjpeg" || $_FILES['oldfireplaceimage']['type'] == "image/jpeg")) {
$outputuploaderrors = "You may only upload JPEG or GIF files.<br>";
echo "filename: ".$_FILES['oldfireplaceimage']['name'];
$enter_contest_success_message = '';
}
else {
$ext = findexts ($_FILES['oldfireplaceimage']['name']);
    $ran = rand () ;
    $ran2 = $ran.".";
    $target = "images/fireplaceimages/";
    $target = $target.$ran2.$ext; 
if(move_uploaded_file($_FILES['oldfireplaceimage']['tmp_name'], $target)){
 
$fireplaceimage = $ran2.$ext;
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$story = $_POST['story'];
$dreamfireplace = $_POST['dreamfireplace'];
//$first_name = str_replace("'","",$_POST['first_name']);
//$last_name = str_replace("'","",$_POST['last_name']);
//$email = str_replace("'","",$_POST['email']);
//$phone = str_replace("'","",$_POST['phone']);
//$interestedvehicle = str_replace("'","''",$_POST['interestedvehicle']);
//$currentvehicle = str_replace("'","''",$_POST['currentvehicle']);
$userip = $_SERVER['REMOTE_ADDR'];
 
 
$queryinsertentry = "insert into entries (entry_first_name, entry_last_name, entry_address, entry_city, entry_state, entry_zip, entry_phone, entry_email, entry_image, entry_story, entry_dreamfireplace, entry_ip) values ('$first_name','$last_name','$address','$city','$state','$zip','$phone','$email','$fireplaceimage','$story','$dreamfireplace','$userip')";
$resultinsertentry = mysql_query($queryinsertentry) or die(mysql_error());
}
else {
$outputuploaderrors = "Sorry, there was a problem uploading your file.";
}
if ($resultinsertentry){
echo "<div id=\"flash\">
$enter_contest_success_message
</div>";
}
}
}
}
if (empty($enter_contest_success_message)){
if (isset($_SESSION['instate'])){
$outputform = "<div id=\"flash\">
$outputuploaderrors
$outputerrors
<form action=\"\" method=\"post\" onsubmit=\"return validateFields(this, fieldInfo)\">
<div class=\"textheader\">
Please complete the form to enter:</div>
<div class=\"leftform\">
First Name:<br/>
<input type=\"text\" name=\"first_name\" size=\"50\" value=\"".$fields['first_name']."\"><br/>
Address:<br/>
<input type=\"text\" name=\"address\" size=\"50\" value=\"".$fields['address']."\"><br/>
State:<br/>
<input type=\"text\" name=\"state\" size=\"50\" value=\"".$_SESSION['instate']."\"><br/>
Phone:<br/>
<input type=\"text\" name=\"phone\" size=\"50\" value=\"".$fields['phone']."\"><br/>
Fireplace Image:<br/>
<input type=\"file\" name=\"oldfireplaceimage\" size=\"30\"><br/>
</div>
<div class=\"rightform\">
Last Name:<br/>
<input type=\"text\" name=\"last_name\" size=\"50\" value=\"".$fields['last_name']."\"><br/>
City:<br/>
<input type=\"text\" name=\"city\" size=\"50\" value=\"".$fields['city']."\"><br/>
Zip Code:<br/>
<input type=\"text\" name=\"zipcode\" size=\"50\" value=\"".$_SESSION['inzip']."\"><br/>
Email:<br/>
<input type=\"text\" name=\"email\" size=\"50\" value=\"".$fields['email']."\"><br/>
</div>
<div class=\"bottomform\">
Tell us your story:<br/>
<textarea name=\"story\" cols=\"75\" rows=\"3\">".$fields['story']."</textarea><br/>
Tell us about your dream fireplace:<br/>
<textarea name=\"dreamfireplace\" cols=\"75\" rows=\"3\">".$fields['dreamfireplace']."</textarea><br/><br/>
<input type=\"Submit\" value=\"Submit\" name=\"send-entercontest\">
</form>
</div></div>";
}
else {
$outputform = "<div style=\"float: left; background-color: #DFDBD8; width: 685px; height: 350px !important; height: 400px; text-align: center; padding: 50px 0px 0px 0px;\">
<div class=\"textheader\">
Enter your zip code to continue.</div>
<form action=\"locatorindex.php\">
<input type=\"hidden\" name=\"dist\" value=\"1\">
<input type=\"hidden\" name=\"mile\" value=\"30\">
<input type=\"text\" name=\"zip\" size=\"10\" value=\"\">
<input type=\"submit\" name=\"send-zip\" value=\"go\">
</form>
</div>";
}
}
print $outputform;
?>
 
 
<div id="middlespace">
<img src="images/layout/middlespace.jpg"></div>
 
<div id="formspace">
<table border="0" cellpadding="0" cellspacing="0" width="205">
  <tr>
   <td><img name="fpfl_r1_c1" src="images/layout/fpfl_r1_c1.jpg" width="205" height="128" border="0" alt=""></td>
  </tr>
  <tr>
   <td><a href="entercontest.php"><img name="enter" src="images/layout/enter_active.jpg" width="205" height="31" border="0" alt=""></a></td>
  </tr>
  <tr>
   <td><a href="aboutcontest.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('about','','images/layout/about_over.jpg',1);"><img name="about" src="images/layout/about.jpg" width="205" height="30" border="0" alt=""></a></td>
  </tr>
  <tr>
   <td><a href="rules.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('rules','','images/layout/rules_over.jpg',1);"><img name="rules" src="images/layout/rules.jpg" width="205" height="31" border="0" alt=""></a></td>
  </tr>
  <tr>
   <td><a href="winner.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('winner','','images/layout/winner_over.jpg',1);"><img name="winner" src="images/layout/winner.jpg" width="205" height="30" border="0" alt=""></a></td>
  </tr>
  <tr>
   <td><img name="fpfl_r6_c1" src="images/layout/fpfl_r6_c1.jpg" width="205" height="150" border="0" alt=""></td>
  </tr>
</table></div>
 
<div id="rightspace">
<img src="images/layout/rightspace.jpg"></div>
 
<div id="footer">
<img src="images/layout/footer.jpg" usemap="#fireside" border="0">
</div>
</div>
 
</body>
</html>
 
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Problem uploading an image

Post by yacahuma »

What ar eyou getting if you echo $_FILES['oldfireplaceimage']['type'] ??
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Problem uploading an image

Post by markusn00b »

You need to use the enctype when uploading files.
ie

Code: Select all

 
<form enctype="multipart/form-data" ...
 
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Problem uploading an image

Post by QbertsBrother »

thanks for your replies.

yacahuma
the value for that was empty

markusn00b
thats it. thank you. i cant believe i forgot that. i was going crazy.

thanks
Post Reply