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;
}
?>