Jump to content

Recommended Posts

Ok so Ive got an upload form for a recipes website im creating.

This handler works perfect in Google Chrome,

No errors in firefox but doesnt work.

IE gives a filesize error.

 

How is this possible?

 

Newrecipe.php:

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<?php 
$title = $_POST['title'];
$smalldesc = $_POST['smalldesc'];
$ingredients = $_POST['ingredients'];
$method = $_POST['method'];
$time = $_POST['time'];
$amount = $_POST['amount'];
$keywords = $_POST['keywords'];
?>

<?php
//check that we have a file
if((!empty($_FILES["uploaded_pic"])) && ($_FILES['uploaded_pic']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
$file = basename($_FILES['uploaded_pic']['name']);
$file = "testingtxt.jpg";
list($file, $ext) = explode(".", $file);
$filename = $_POST['title'].".".$ext;
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_pic"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_pic"]["size"] < 350000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/_images/_pic/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_pic']['tmp_name'],$newname))) {
	   echo "It's done! The file has been saved as: ".$newname;
	   
	   $thumb = dirname(__FILE__).'/_images/_pic/'.$_POST['title']." _thumb.".$ext;
           createThumb($newname, $thumb);
	   
	   $img = dirname(__FILE__).'/_images/_pic/'.$_POST['title'].".".$ext;
           resize($newname, $img);
	   
	   $query = "INSERT INTO recipes (
			Title, Smalldesc, Ingredients, Method, Time, Amount, Keywords, Created_at, Picture, Picturesmall
		) VALUES (
		'{$title}', '{$smalldesc}', '{$ingredients}', '{$method}', '{$time}', '{$amount}', '{$keywords}', NOW(), '/_images/_pic/$title.jpg', '/_images/_pic/$title _thumb.jpg')";
		if (mysql_query($query, $connection)) {
			header("Location: allrecipes.php");
			exit;
		} else {
				echo "<p>Recipe not entered.</p>";
				echo "<p>" . mysql_error() . "</p>";
			}
	   
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_pic"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350Kb are accepted for upload";
  }
} else {
$query = "INSERT INTO recipes (
			Title, Smalldesc, Ingredients, Method, Time, Amount, Keywords, Created_at, Picture, Picturesmall
		) VALUES (
		'{$title}', '{$smalldesc}', '{$ingredients}', '{$method}', '{$time}', '{$amount}', '{$keywords}', NOW(), '/_images/_pic/noimage.jpg', '/_images/_pic/noimage_thumb.jpg')";
		if (mysql_query($query, $connection)) {
			header("Location: allrecipes.php");
			exit;
		} else {
				echo "<p>Recipe not entered.</p>";
				echo "<p>" . mysql_error() . "</p>";
			}
echo "Error: No file uploaded";
}
?>

<?php mysql_close($connection); ?>

 

 

 

 

For IE, the code is testing both the ['type'] and ['size'] in one conditional statement and reports - Error: Only .jpg images under 350Kb are accepted for upload. So, you will never know which comparison failed. Rewrite the code so that you test for and report each possible error separately (hint: the ['type'] that IE sends is probably not "image/jpeg".) Which beings up another point, error messages that don't have anything to due with security should display the actual values so that you can see what was being compared.

 

For FF, define: "but doesnt work" What does it do? What do you see in front of you?

Just to let you know that IE inserts the info fine when not uploading a picture, so Im assuming it is that

 

if (($ext == "jpg") && ($_FILES["uploaded_pic"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_pic"]["size"] < 350000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/_images/_pic/'.$filename;

 

bit of code, im not sure how to rework that however

For FF, does the move_uploaded_file() produce a file? Which code is responsible for the redirect to share.php? The header("Location: allrecipes.php"); or something that <?php confirm_logged_in(); ?> does?

 

The easiest and best way to test for multiple possible values for the ['type'] is to make an array of the possible values and use in_array in the conditional test.

Just a side note, not related to the problem:

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<?php 

 

Why do that? This would be much easier:

<?php 
require_once("includes/connection.php"); 
require_once("includes/session.php"); 
require_once("includes/functions.php"); 
confirm_logged_in(); 

 

There is really no reason to put them in their own <?php ?> tags...

Thanks for the help thus far people, Ive managed to workaround it so it works perfectly in both Google and now IE!!!

 

trouble is firefox is still playing up  >:(

 

if (($ext == "jpg") && ($_FILES["uploaded_pic"]["size"] < 350000)) {
    //Determine the path to which we want to save this file

 

I removed the image verification completely as the above code checks if its a jpg anyway.

 

Any ideas what problem firefox has with this now? Im so close!!!

 

If anyone would like my work files then let me know

 

Honestly, the three browsers should not make a difference in either.

 

What most likely is the issue is your form, so post the HTML code for that form/page that you use for the upload process.

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>What Can I Cook | Upload Your Recipe</title>

<link REL="SHORTCUT ICON" HREF="_images/favicon.ico">

<link href="/_css/base.css" rel="stylesheet" type="text/css" />
<link href="/_css/uploadrecipe.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
  <div id="header">
   	<h1>What Can I Cook?</h1>
   	<div id="logoImage"></div>
  <ul id="mainNav">
    <li><a href="index.html">Home </a></li>
    <li><a href="allRecipes.php">Recipes </a></li>
    <li><a href="features.php">Features </a></li>
    <li><a href="blogs.php">Blogs </a></li>
    <li><a href="forum.php">Forum </a></li>
    <li><a href="share.php">Share </a></li>
    <li><a href="shop.html">Shop </a></li>
  </ul>
  </div>
  
  <div id="content">    	
      <h1>Add your Recipe</h1>
      <p>We have tried to make this as painless as possible for you, below are the fields you can enter your information into and we have written tips and examples to the right of them to help smooth the process.</p>
<form enctype="multipart/form-data" form id="form1" name="form1" method="post" action="newrecipe.php">
        <table width="801" border="0" id="recipeupload2">
          <tr>
            <td width="169" valign="top"><h1>
              
              Recipe Title:
              <label></label>
            </h1></td>
            <td width="622" valign="top"><input name="title" type="text" id="title" size="60" maxlength="60" />            </td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>Please choose your title carefully, it should be descriptive to other users.</p></td>
          </tr>
          <tr>
            <td valign="top"><h1>Small Description:
              <label></label>
            </h1></td>
            <td valign="top"><textarea name="smalldesc" cols="60" rows="3" id="smalldesc"></textarea>            </td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>This will appear when a user searches for a recipe, this small description could be why you like this recipe or why others will want to try it. e.g. a Quick and Simple hearty meal etc.</p></td>
          </tr>
          <tr>
            <td valign="top"><h1>Ingredients:
              <label></label>
            </h1></td>
            <td valign="top"><textarea name="ingredients" cols="60" rows="5" id="ingredients">100g pasta, 4 chicken breasts, grated cheese</textarea>            </td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>NOTE: To seperate each ingredient use a comma (,). If you use a comma then anything after that will be shown as a new item, above is the way you should type your ingredients.</p></td>
          </tr>
          <tr>
            <td valign="top"><h1>Method:
              <label></label>
            </h1></td>
            <td valign="top"><textarea name="method" cols="60" rows="5" id="method"></textarea>            </td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>Separate steps using paragraphs, hit "enter" key. Do not number steps (we'll do that for you). </p></td>
          </tr>
          <tr>
            <td valign="top"><h1>Time:
              <label></label>
            </h1></td>
            <td valign="middle"><input type="text" name="time" id="time" /></td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>How long will it take to make this recipe? This is a total figure with preperation taken into account.</p></td>
          </tr>
          <tr>
            <td valign="top"><h1>Servings:
              <label></label>
            </h1></td>
            <td valign="middle"><input type="text" name="amount" id="amount" /></td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>How many servings will this recipe make? e.g You could type 'X Portions', 'X Helpings' or 'X Cookies'</p></td>
          </tr>
          <tr>
            <td valign="top"><h1>Upload Picture:</h1></td>
            <td valign="top"><label>
            	<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
            	<input name="uploaded_pic" type="file" />              
            </label></td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>This is greatly encouraged as it will make your recipe really stand out from the crowd, the dimensions will be 235px by 215px. It must be a jpeg image and not exceed 350kb in size.</p></td>
          </tr>
          <tr>
            <td valign="top"><h1>Keywords:
              <label></label>
            </h1></td>
            <td valign="top"><textarea name="keywords" cols="60" rows="2" id="keywords"></textarea></td>
          </tr>
          <tr>
            <td valign="top"> </td>
            <td valign="top"><p>This is for our search engine, it could be anything you like and will mean that the words above will make your recipe appear when someone searches for that word. You could leave this blank if want.</p></td>
          </tr>
          <tr>
            <td colspan="2" valign="top"><label>
              <input type="button" name="cancel" id="cancel" value="Cancel" onclick="history.back()" />
              <input type="submit" name="continue" id="continue" value="Continue" />
            </label></td>
          </tr>
        </table>
    </form>
      </div>
  <div id="clear"></div>
  <div id="footer">© 2007 Cheek Chastain Gallery. No portion of this  website or artwork portrayed herein may be redistributed of republished  without the expressed consent of Cheek Chastain Gallery. View our full  legal disclaimer here.</div>
</div>
</body>
</html>

<form enctype="multipart/form-data" form id="form1" name="form1" method="post" action="newrecipe.php">

 

Should be:

<form enctype="multipart/form-data" id="form1" name="form1" method="post" action="newrecipe.php">

 

The rest look fine, try changing that and see if it fixes your results.

 

still no luck in firefox, cant for the live of me work this out

 

Try searching the forums, about 2-3 days ago a guy had the same problem with uploading and firefox, I cannot remember what the solution was, but maybe it will work here too.

 

Found it:

http://www.phpfreaks.com/forums/index.php/topic,230310.msg1067826.html#msg1067826

 

Edit:

Linked to the answer of the post.

 

I think I just found out what is happening.  FF is reading ">= 20000" as 20000 bytes, and IE is reading it as kb.  I don't know why.  But that is what seems to be happening from my tests.  so I changed it to 21000000 and it works in FF, but now people can upload massive files in IE.  Oh well, I guess as close to a fix as I can hope.

Well, I highly doubt this is the problem, cause I do it all the time too, but who knows.

 

Try changing each form field id="fieldname_id" and see what that does. If that still does not work, are you sure your errors are turned on to display errors and see if any errors are coming through?

 

The next step would be, in the PHP file, try removing an include and see if it displays anything, then add it back in and try the next. It could be an error in your pages, but why it would trigger on separate browsers I do not know.  You could also try adding ob_start before the first include and see if it works or not.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.