PHP: Storing Form Variables

Discussion in 'OT Technology' started by frustrated inc., May 16, 2005.

  1. I have 2 fields in my form. One is a FILE field, and the other is HIDDEN.

    I want to be able to take the -filename- of the FILE field, and store it in the HIDDEN field.

    I have:

    Code:
    <input type="hidden" name="info_name" value= "<?php echo $fileName; ?>" >
    But then I'd need to define $fileName.. how can I get the file name of my FILE field into the variable $fileName

    Am I even on the right track?
     
  2. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    Let's assume that $fileName is bob.

    That code above will run through your server and send to the client:
    <input type="hidden" name="info_name" value="bob">

    To get the value bob, the php variable would be info_name.

    I don't really understand what you're trying to do. Are you trying to create an upload script and have the user upload a file?
     
  3. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,924
    Likes Received:
    11
    Location:
    Los Angeles
    You can get the file name in the file field by accessing it's variable. The variable would be the actual "name" of the field. Just as MrMan said, to get the value of bob, name is the variable. Something like this would be quite easy, but it depends how exactly you are doing it.

    PHP:
    <?php
    $fileName 
    "Some Value";
    echo 
    '<input type="file" name="$fileName" value="$fileName">';
    ?>
    <input type="hidden" name="info_name" value="<?=$fileName?>">
    But then again, it differs, depending on exactly what you're doing.
     
  4. Shibboleth

    Shibboleth teh mad Plato skillz

    Joined:
    Sep 29, 2004
    Messages:
    1,769
    Likes Received:
    0
    $string = sprintf("%s",$_SERVER['SCRIPT_FILENAME']);
    $string = strrchr($string,"/");

    assuming the file is index.php, that will give you "/index.php"
     
  5. Thanks for all the help offered.

    I'm combining a file uploader with a form that will add an entry in a database.

    What I wanted was to let the user browse and upload a file (so the file path could be anything - and not just a server address). Then I wanted the filename that was uploaded(WITHOUT the path of the file where the file was uploaded from) to be added to the database, so the image could be shown on the appropriate detail page.

    What I'm going to do instead of storing the name of the file in the database, is change the name of the file to the ID of the entry being added. So if the highest ID in the database is 180, the file will be renamed to 181.jpg when it's uploaded to the server, and I can store this info into the hidden form field by using
    Code:
    value = "<?php echo $row_last_number['info_id']+1; ?>.jpg"
     
  6. Shibboleth

    Shibboleth teh mad Plato skillz

    Joined:
    Sep 29, 2004
    Messages:
    1,769
    Likes Received:
    0
    ahh, I see your dilemma. You want to choose a file to be uploaded, and have it sent via the hidden field?
    you should be fine. take a look at the $_FILES global associative array, more info here http://www.php.net/manual/en/reserved.variables.php#reserved.variables.files

    use echo as you were using above, and at the page where the form's action method points, retrieve the hidden from $_POST and store it. If we're still miscommunicating post again and I'll try and help again.
     
  7. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    Take a look at this:
    http://us2.php.net/features.file-upload

    Basically, HTML has it's own built in file upload form.
    <form enctype="multipart/form-data" action="__URL__" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
    </form>

    And with PHP, you can do a lot to this file. I've done something like this before. Note that the filename will just be the file name and not the full path. As for avoiding duplicate names on the database, I wrote a PHP script that will check if the filename already exists in the database. If it does, increment a # to the end of the filename. Or add the date and time to the end of the name (using some PHP date/time functions).

    So if bob1.jpg exist, make it bob2.jpg
    or if date/time method: bob051605184330.jpg (which means filename bob 05/16/03 6:43:30PM).
     
  8. Everything is almost working now thanks to the help I got here.. so thanks everyone.

    I am using a form to upload the file to the server, and am using the following code to give a value to a hidden field so I can use the info to add a record to the database.

    Code:
     <input type="hidden" name="imagename" value = "<? echo "".$HTTP_POST_FILES['userfile']['name']."" ?>">
     
    The problem is that this code doesn't evaluate what the file name of the file that is currently being uploaded, but rather uses the name of the file that was previously loaded.

    In other words, a value is given to the field before a file is even selected.

    If I could somehow get the above line of code to re-evaluate when the form is submitted, everything would work exactly how I'd want it.....
     
  9. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    Why not save the uploaded file name into a variable and use that?
     
  10. The question I was posing in my first post is how I would do that. How would I get the filename of the uploaded file into $fileName :dunno:

    I need a file to upload, and a database to update in a single form. And I nearly have that, with the exception of the form passing stale $HTTP_POST_FILES data to the database.
     
  11. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,924
    Likes Received:
    11
    Location:
    Los Angeles
    Post your code.
     
  12. Code:
         <?php require_once('Connections/uploads.php'); ?>
         <?php
         function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
         {
           $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
         
           switch ($theType) {
         	case "text":
         	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
         	  break;	
         	case "long":
         	case "int":
         	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
         	  break;
         	case "double":
         	  $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
         	  break;
         	case "date":
         	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
         	  break;
         	case "defined":
         	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
         	  break;
           }
           return $theValue;
         }
         
         $editFormAction = $_SERVER['PHP_SELF'];
         if (isset($_SERVER['QUERY_STRING'])) {
           $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
         }
         
         if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
           $insertSQL = sprintf("INSERT INTO tbl_info (info_pic, info_desc) VALUES (%s, %s)",
     					 GetSQLValueString($_POST['imagename'], "text"),
     					 GetSQLValueString($_POST['description'], "text"));
         
           mysql_select_db($database_uploads, $uploads);
           $Result1 = mysql_query($insertSQL, $uploads) or die(mysql_error());
         }
         ?>
         <FORM name="form1" ENCTYPE="multipart/form-data" ACTION="<?php echo $editFormAction; ?>" METHOD="POST">
           <p>The file: 
         	<INPUT TYPE="file" NAME="userfile">
         </p>
           <p>Description:
         	<textarea name="description" cols="100"></textarea>
         </p>
           <p>&nbsp;</p>
           <p>
         	<input type="hidden" name="imagename" value = "<? echo $_FILES['userfile']['name'] ?>">
         	<input name="submit" type="submit" value="Upload">
           </p>
           <input type="hidden" name="MM_insert" value="form1">
         </FORM>
         
         <?php
         
         $path = "images/";
         $max_size = 200000;
         
         if (!isset($HTTP_POST_FILES['userfile'])) exit;
         
         if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
         
         if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>\n"; exit; }
     if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {
         
         if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }
         
         $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
         $HTTP_POST_FILES['userfile']['name']);
         if (!$res) { echo "upload failed!<br>\n"; exit; } else { echo "upload sucessful<br>\n"; }
         
         echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
         echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
         echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
         } else { echo "Wrong file type<br>\n"; exit; }
         
         }
         
         ?>
         
    The problem is here:
    Code:
       <input type="hidden" name="imagename" value = "<? echo $_FILES['userfile']['name'] ?>">
      
      
    This value is defined when the page loads, as opposed to when the info is being submitted. Basically, I'm looking for the value to be re-evaluated when the info is sent, and not when the page loads.
     
  13. I have it working now. For anyone interested in my solution:

    Code:
     
     if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
       $insertSQL = sprintf("INSERT INTO tbl_info (info_pic, info_desc) VALUES (%s, %s)",
     					   //GetSQLValueString($_POST['imagename'], "text"),
     					   [b]GetSQLValueString($_FILES['userfile']['name'], "text"),[/b]
     					   GetSQLValueString($_POST['description'], "text"));
     
     
     

Share This Page