SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form submitted file but $_FILES[][] is empty

    i have a form that posts to a page where i try to echo
    "\ntype ".$_FILES["file"]["type"].
    "\nsize ".$_FILES["file"]["size"];
    and it seems that this array is empty. here is the form:
    Code:
    	echo "
    <h1>add to ".$mysqlTableName."</h1>
     <form action=\"post.php\" method=\"post\">
      <label for=\"title\">Title</label>
      <input type=\"text\" name=\"title\" />
      <br />
      <label for=\"text\">Description</label>
      <input type=\"text\" name=\"text\" id=\"text\" />
      <br />
      <label for=\"price\">Price</label>
      <input type=\"text\" name=\"price\" id=\"price\" />
      <br />
      <label for=\"image\">Image</label>
    	<input type=\"file\" name=\"image\" id=\"image\" /> 
    	<br />
    
      <input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\" />
     </form>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You really need to read the manual before attempting this, the first link explains where you are going wrong.

    File uploading is one of the most complex things to do if you do not create time for yourself to read the manual very carefully, especially the part about common pitfalls.

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Just to give a more obvious hint, on the first link on the page @Cups ; provided, look for "enctype"

  4. #4
    SitePoint Member emoviefreak's Avatar
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use this code in your form tag, enctype="multipart/form-data".

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_FILES[][] is empty

    srry for repost
    why is $_FILES[][] is empty?
    form:
    Code:
    <form enctype=\"multipart/form-data\" action=\"post.php\" method=\"post\">
      <label for=\"title\">Title</label>
      <input type=\"text\" name=\"title\" />
      <br />
      <label for=\"text\">Description</label>
      <input type=\"text\" name=\"text\" id=\"text\" />
      <br />
      <label for=\"price\">Price</label>
      <input type=\"text\" name=\"price\" id=\"price\" />
      <br />
      <label for=\"file\">Image</label>
      <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"200000000\" />
      <input type=\"file\" name=\"file\" id=\"file\" /> 
      <br />
      <input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\" />
    </form>
    post.php (file upload management starts line 65):
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Item Post</title>
    <link rel="stylesheet" type="text/css" href="/css/mw3dailymedia.css?a=<%=now%>" />
    </head>
    <body>
    
    
    
    
    <div id="content">
    <div id="admin">
    <table id="table">
     <?php
      
    	// redifine variables for different server
    	require_once "mysqlconfig.php";  
    	require_once "textprep.php";  
    	require_once "pagevars.php";
    	$mysqlTableName=MYSQLTABLENAME;
    	
    	// connect to database
    	$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS); 
    	if (!$connection)
    	{
    		die("Database connection failed: " . mysql_error());
    	}
     
    	// select database
    	$db_select = mysql_select_db(DB_NAME,$connection);
    	if (!$db_select)
    	{
    		die("Database selection failed: " . mysql_error());
    	}
     	
    	//get top rank
    	$result = mysql_query("SELECT * FROM ".$mysqlTableName."", $connection);
    	if (!$result) 
    	{
    		die("Database query failed: " . mysql_error());
    	}
    	$topRank=0;
    	while ($row = mysql_fetch_array($result))
    	{
    		if($row["rank"]>$topRank)
    		{
    			//debug echo "<p>top rank ".$topRank.", current rank ".$row["rank"]."</p>";
    			$topRank=$row["rank"];
    		}
    	}
    	$rank=$topRank+1;
    
    	// move all other ranks up one so we can fit new vid at number 1
    		for($i=($rank-1);$i>=1;$i--)
    		{
    			$query="UPDATE ".$mysqlTableName." SET rank='".($i+1)."' WHERE rank='".$i."'";
    			$result = mysql_query($query, $connection);
    			if (!$result) 
    			{
    				die("Database UPDATE failed: " . mysql_error());
    			}
    		}
    	// store picture
    	$allowedExts = array("jpg", "jpeg");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 200000000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
        if (file_exists($_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "/menu/" . $_POST["title".".jpg"]);
          echo "Stored in: " . $_POST["title".".jpg"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      echo
    "\ntype ".$_FILES["file"]["type"].
    "\nsize ".$_FILES["file"]["size"];
      }
    
    
    	// put variables into table
    	$result = mysql_query("INSERT INTO ".$mysqlTableName." (name,description,price,rank) VALUES ('".mysql_prep($_POST["title"])."','".mysql_prep($_POST["text"])."','".mysql_prep($_POST["price"])."','".(1)."')", $connection);
    	if (!$result)
    	{
    		die("Database INSERT failed: " . mysql_error());
    	}
     
    	// confirm that variables are on our server
    	$result = mysql_query("SELECT * FROM ".$mysqlTableName."", $connection);
    	if (!$result)
    	{
    		die("Database query failed: " . mysql_error());
    	}
    	while ($row = mysql_fetch_array($result)) {
    		echo "<tr><td>name: ".$row["name"]."</td></tr><tr><td>description: ".$row["description"]."</td></tr><tr><td>price: ".$row["price"]."</td></tr><tr><td>rank: ".$row["rank"]."</td></tr>";
    	}
     
    ?>
    </table>
    </div>
    </div>
    </body>
    </html>

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wtf its working now...

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Maybe you were seeing a cached version of the form that still didn't contain the enctype=\"multipart/form-data\"?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •