views:

369

answers:

4

Trying to build a admin panel for uploading text and images here is the html page. The Html page is working fine it's the php page that is broken the html is just here for reference.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>Administartor Panel</title>
<head>
<link rel="stylesheet" type="text/css" href="admin.css" />
</head>
<body>
<h1>Heritage House Administartor Panel</h1>
<br/>
<h2>
<a href="#eone">Event One</a>&nbsp;
<a href="#etwo">Event Two</a>&nbsp;
<a href="#ethree">Event Three</a>&nbsp;
<a href="#efour">Event Four</a>&nbsp;
<a href="#efive">Event Five</a>&nbsp;
<a href="#esix">Event Six</a>&nbsp;
</h2>
<br/>
<table>
<tr>
<td id="eone">
<br/>
<p>Event One</P>
<p> Please name the picture file1 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="afile" id="afile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt1.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical One:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="etwo">
<p >Event Two</P>
<p> Please name the picture file2 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="bfile" id="bfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt2.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Two:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="ethree" >
<p >Event Three</P>
<p> Please name the picture file3 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="cfile" id="cfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt3.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Three:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efour" >
<p >Event Four</P>
<p> Please name the picture file4 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="dfile" id="dfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt4.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Four:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efive" >
<p >Event Five</P>
<p> Please name the picture file5 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="efile" id="efile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt5.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Five:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="esix" >
<p >Event Six</P>
<p> Please name the picture file6 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="ffile" id="file6" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt6.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Six:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
</body>
</html>

trying to rename the files uploaded based on which form they come from on the html page afile,bfile etc. I have tried this php file a number of different ways. I can get it to work when I eliminate the large IF statement and make 6 separate files for uploading but I was hoping to make it one file This is what I believe makes it fail moving the global variables in to normal vars?

        <?php
    //This function separates the extension from the rest of the file name and returns it
    function findexts ($filename)
    {
    $filename = strtolower($filename) ;
    $exts = split("[/\\.]", $filename) ;
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts;
    } 
    //This if statement assigns the new file name to a variable and displays a message.
    if (file_exists($_FILES["afile"]["tmp_name"]))
      {
      $new = "file1.";
      $type = $_FILES ["afile"] ["type"];
      $size = $_FILES ["afile"] ["size"];
      $error = $_FILES ["afile"] ["error"];
      $name = $_FILES ["afile"] ["name"];
      $tmpname = $_FILES["afile"]["tmp_name"];
      $names = "afile";
      echo "file1 Uploaded <br />";
      }
    elseif (file_exists($_FILES["bfile"]["tmp_name"]))
      {
      $new = "file2.";
      $type = $_FILES ["bfile"] ["type"];
      $size = $_FILES ["bfile"] ["size"];
      $error = $_FILES ["bfile"] ["error"];
      $name = $_FILES ["bfile"] ["name"];
      $tmpname = $_FILES["bfile"]["tmp_name"];
      $names = "bfile";
      echo "file2 Uploaded <br />";
      }
    elseif (file_exists($_FILES["cfile"]["tmp_name"]))
      {
      $new = "file3.";
      $type = $_FILES ["cfile"] ["type"];
      $size = $_FILES ["cfile"] ["size"];
      $error = $_FILES ["cfile"] ["error"];
      $name = $_FILES ["cfile"] ["name"];
      $tmpname = $_FILES["cfile"]["tmp_name"];
      $names = "cfile";
      echo "file3 Uploaded <br />";
      }
    elseif (file_exists($_FILES["dfile"]["tmp_name"]))
      {
      $new = "file4.";
      $type = $_FILES ["dfile"] ["type"];
      $size = $_FILES ["dfile"] ["size"];
      $error = $_FILES ["dfile"] ["error"];
      $name = $_FILES ["dfile"] ["name"];
      $tmpname = $_FILES["dfile"]["tmp_name"];
      $names = "dfile";
      echo "file4 Uploaded <br />";
      }
    elseif (file_exists($_FILES["efile"]["tmp_name"]))
      {
      $new = "file5.";
      $type = $_FILES ["efile"] ["type"];
      $size = $_FILES ["efile"] ["size"];
      $error = $_FILES ["efile"] ["error"];
      $name = $_FILES ["efile"] ["name"];
      $tmpname = $_FILES["efile"]["tmp_name"];
      $names = "efile";
      echo "file5 Uploaded <br />";
      }
    elseif (file_exists($_FILES["ffile"]["tmp_name"]))
      {
      $new = "file6.";
      $type = $_FILES ["ffile"] ["type"];
      $size = $_FILES ["ffile"] ["size"];
      $error = $_FILES ["ffile"] ["error"];
      $name = $_FILES ["ffile"] ["name"];
      $tmpname = $_FILES["ffile"]["tmp_name"];
      $names = "ffile";
      echo "file6 Uploaded <br />";
      }

    //This applies the function to our file
    $ext = findexts ($_FILES ['$names'] ['name']) ;
        //This assigns the subdirectory you want to save into.
        $targett = "forms/upload/";
        //This combines the directory, the new file name, and the extension
        $target = $targett . $new.$ext; 
        // makes sure image meets specs
    if ((($type == "image/gif")
    || ($type == "image/jpeg")
    || ($type == "image/pjpeg"))
    && ($size < 2000000))
      {
      if ( $error > 0)
        {
        echo "Return Code: " . $error . "<br />";
        }
      else
        {
        echo "Thank You! <br />";
        }
    //saves uploaded file
        if (file_exists("forms/upload/" . $name))
          {
          move_uploaded_file( $tmpname, $target);
          echo $name . " Old File Over Written. ";
          }
        else
          {
          move_uploaded_file( $tmpname, $target);
          echo "Stored in: " . "forms/upload/" . $name;
          }   
?>

Maybe is my elseif statement broken?

I have only been playing with this php stuff for about a week so if I'm way off mark here sorry for wasting your time.

Maybe if I try making 6 functions with the globals move_uploaded_file($_FILES["file"]["tmp_name"], $target); than placeing them in the elseif statement it could work?

this is a copy of the working php page

<?php
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
} 
//This applies the function to our file
$ext = findexts ($_FILES['afile']['name']) ;
    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
    $new = "file1.";
    //This assigns the subdirectory you want to save into... make sure it exists!
    $targett = "forms/upload/";
    //This combines the directory, the random file name, and the extension
    $target = $targett . $new.$ext; 
if ((($_FILES["afile"]["type"] == "image/gif")
|| ($_FILES["afile"]["type"] == "image/jpeg")
|| ($_FILES["afile"]["type"] == "image/pjpeg"))
&& ($_FILES["afile"]["size"] < 2000000))
  {
  if ($_FILES["afile"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["afile"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["afile"]["name"] . "<br />";
    echo "Type: " . $_FILES["afile"]["type"] . "<br />";
    echo "Size: " . ($_FILES["afile"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["afile"]["tmp_name"] . "<br />";

    if (file_exists("forms/upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["afile"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["afile"]["tmp_name"], $target);
      echo "Stored in: " . "forms/upload/" . $_FILES["afile"]["name"];
      }
    else
      {
      echo "Invalid file";
      }
A: 

Maybe you should use

isset($_FILES["afile"])

instead of

file_exists($_FILES["afile"]["tmp_name"])

--edit

to answer your question in the title: it is possible, if, e.g $_FILES["afile"], is set. That way you'll have an array in your new variable.

stefita
A: 

Very close to working actually.

//Uses $names as a key in $_FILES - no single quotes for variable expansion....
$ext = findexts($_FILES[$names]['name']);

Might I also suggest using some sort of loop instead of a big elseif with a bunch of pasted code. Two improvements used: foreach on the $_FILES array, and extract

foreach ($_FILES as $fileKey => $values)
{
  // Sets $type, $size, $name, $tmp_name, $error
  extract($values);
  //This applies the function to our file
  $ext = findexts($name) ;

  //This assigns the subdirectory you want to save into.
  $targett = "forms/upload/";
  //This combines the directory, the new file name, and the extension
  $target = $targett . $new.$ext; 
  // makes sure image meets specs
  if ((($type == "image/gif")
    || ($type == "image/jpeg")
    || ($type == "image/pjpeg"))
    && ($size < 2000000))
  {
    if ($error > 0)
    {
      echo "Return Code: " . $error . "<br />";
    }
    else
    {
      echo "Thank You! <br />";
    }
    //saves uploaded file
    if (file_exists("forms/upload/" . $name))
    {
      move_uploaded_file( $tmp_name, $target);
      echo $name . " Old File Over Written. ";
    }
    else
    {
      move_uploaded_file( $tmp_name, $target);
      echo "Stored in: " . "forms/upload/" . $name;
    }         
  }
}
gnarf
Oh, missed that one :). It should be double quotes or no quotes at all.
stefita
+1  A: 

That is a heck of a lot of repeated code! Do you really need 6 separate HTML forms? Why not have one form, with a dropdown for choosing the event?

<select name="event">
    <option value="afile">Event One</option>
    <option value="bfile">Event Two</option>
    ...etc
</select>

For the input, use:

<input type="file" name="userfile" id="file" />

Then instead of your lengthy if/else clauses, simply do something like this:

if ( isset($_POST['event']) && file_exists($_FILES['userfile']['tmp_name']) )
{
    // base this code on the value of $_POST['event']
}
DisgruntledGoat
A: