So i have been at this for a day I know its simple but my brain is no longer functioning correctly. I have a script that uploads a picture, writes some contents to a file. I need to access the variable "ext" below so i can write it and save it into the new file that is being created. Problem is that the only way i have been able to write a variable to the file is if it is defined within the actual funtion being sent to the file in the variable "somecontent". I have messed with globals, sessions and sending it through the url. I think im on the wrong track, anyway heres the code.

<?php
if ($REQUEST_METHOD == "POST")

{
$ecardfolder = rand();
$oldumask = umask(0);
mkdir($ecardfolder, 0777);

echo("Your new personalized website and e-card is now available at http://www.xxxxxxxx.com/ecard1/$ecardfolder");
echo("/ecard.php");

$efile = fopen("$ecardfolder/ecard.php","w+");
chmod("$ecardfolder/ecard.php", 0777);
}
?>

<?

if ($REQUEST_METHOD == "POST")
{

/ SUBMITTED INFORMATION - use what you need
temporary filename (pointer): $imgfile
original filename : $imgfile_name
size of uploaded file : $imgfile_size
mime-type of uploaded file : $imgfile_type
/

/*== upload directory where the file will be stored
     relative to where script is run ==*/

$uploaddir = "$ecardfolder";

/== get file extension (fn at bottom of script) ==/
/== checks to see if image file, if not do not allow upload ==/
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
if (($pext != "jpg") && ($pext != "jpeg") && ($pext != "gif"))
{
print "<h1>ERROR</h1>Image Extension Unknown.<br>";
print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
print "The file you uploaded had the following extension: $pext</p>\n";

   /*== delete uploaded file ==*/
   unlink($imgfile);
   exit();

}

/== setup final file location and name ==/
/== change spaces to underscores in filename ==/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";

/== do extra security check to prevent malicious abuse==/
if (is_uploaded_file($imgfile))
{

  /*== move file to proper directory ==*/
  if (!copy($imgfile,"$newfile"))
  {
     /*== if an error occurs the file could not
          be written, read or possibly does not exist ==*/
     print "Error Uploading File.";
     exit();
  }
}

/== delete the temporary uploaded file ==/
unlink($imgfile);

print("<img src=\"$ecardfolder/$final_filename\">");
}
?>

<h2>Upload and Resize an Image</H2>

<form action="<?=$SCRIPT_NAME; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">

<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>
<?
echo("http://www.petslifestories.com/ecard1/$ecardfolder/ecard.php?ext=$ext");
?>
<?php
if ($REQUEST_METHOD == "POST")
{
$filename = "$ecardfolder/ecard.php";

$somecontent = '<?php require("http://www.xxxxxxxxxxxxxxx.com/ecard1/header.html");?><?php echo("kihdkdh $ext");?>';

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
}

?>

<?
/== FUNCTIONS ==/

function getFileExtension($str) {
global $ext;
$i = strrpos($str,".");
if (!$i) { return ""; }

   $l = strlen($str) - $i;
   $ext = substr($str,$i+1,$l);

   return $ext;

}
?>

    I should mention that some of the stuff in there might seem a little out of place or not needed or turned around. It all works as is but some stuff is in there just for the sake of testing.

      anyone? Maybe its not as easy as i thought?

        Honestly, it seems that it should be simple. But the code you posted is long and not displayed with correct formatting. It would be nice if you could shave it down to a smaller amount of code which still has the issue, and surround it with the

         tags when posting it.

          Thanks for the response, The following function produces "ext", I desperately need to get ext into a format that can be written directly to the new file that was created.

          <? 
          
          function getFileExtension($str) { 
          global $ext; 
          $i = strrpos($str,"."); 
          if (!$i) { return ""; } 
          
          $l = strlen($str) - $i; 
          $ext = substr($str,$i+1,$l); 
          
          return $ext; 
          
          } 
          ?>
          

          In the following I need $somecontent to write $ext's value directly to the new file.

          <?php 
          if ($REQUEST_METHOD == "POST") 
          { 
          $filename = "$ecardfolder/ecard.php"; 
          
          
          $somecontent = '<?php require("http://www.xxxxxxxxxxxxxxx.com/ecard1/header.html");?><?php echo("kihdkdh $ext");?>'; 
          
          // Let's make sure the file exists and is writable first. 
          if (is_writable($filename)) { 
          
          // In our example we're opening $filename in append mode. 
          // The file pointer is at the bottom of the file hence 
          // that's where $somecontent will go when we fwrite() it. 
          if (!$handle = fopen($filename, 'a')) { 
          echo "Cannot open file ($filename)"; 
          exit; 
          } 
          
          // Write $somecontent to our opened file. 
          if (fwrite($handle, $somecontent) === FALSE) { 
          echo "Cannot write to file ($filename)"; 
          exit; 
          } 
          
          echo "Success, wrote ($somecontent) to file ($filename)"; 
          
          fclose($handle); 
          
          } else { 
          echo "The file $filename is not writable"; 
          } 
          } 
          
          ?> 
          

          Thanks a bunch!

            The problem is on this line:

            $somecontent = '<?php require("http://www.xxxxxxxxxxxxxxx.com/ecard1/header.html");?><?php echo("kihdkdh $ext");?>';

            When you specify a string using single quotes php doesn't expand variables for you. You could change the above to

            $somecontent = '<?php require("http://www.xxxxxxxxxxxxxxx.com/ecard1/header.html");?><?php echo("kihdkdh '.$ext.'");?>';

            By the way, whenever possible it's a good idea to avoid global variables. Your function returns the extension, and you assign it to $pext. Any reason you don't just use $pext?

              Thank you sooo much. The only reason I was messing with globals was to test different things out to get it to work.

              I would have single quoted it although php usually hates me and returns the wrong value when i single quote so i just stayed away.

              Anyway, problem definately solved. I will find a question in the newbie forum to answer now since I got great help.

                Write a Reply...