 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
03-09-2011, 02:33 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Feb 2010
Posts: 69
Thanks: 16
|
Creating and calling functions
Ok so now im at the next level where i need to take it to the next step. Over the past i have used Multiple pages for scripts etc.. now what i need to do is put them all on the same page rather than having to redirect place to place then to redirect back to the original page. Only thing is im having trouble on how to construct what i have and looking at examples once again getting confused. So if someone could show me how to construct this.??
Ok so on the main page i have:
PHP Code:
<?php
$myFile = "../includes/docs/sdata.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo '<form action="Scripts/edittemplates.php" method="post" enctype="multipart/form-data"><label for="templateedit">Edit template script</label><br /><textarea name="templateedit" cols="90" rows="20" id="templateedit">' . $theData . '</textarea><br><br><button>Save Template script</button></form>'
?>
Its basicaly just displaying what i have in that text file... with a button that submits the change. The button takes you to the file Scripts/edittemplates.php which contains the code to edit the file and put on there the new content.
PHP Code:
<?php
//lets delete old file
$myFile = "../../includes/docs/sdata.txt";
unlink($myFile);
//now create a new 1
$ourFileName = "../../includes/docs/sdata.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
//now write the data
$thescript = $_REQUEST["templateedit"];
$myFile = "../../includes/docs/sdata.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "" . $thescript . "";
fwrite($fh, $stringData);
fclose($fh);
//pend to admin log
$ip=$_SERVER['REMOTE_ADDR'];
$myFile = "../../includes/docs/adminlog.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "<div class='qname'>sb0t Template script edited by: " . $ip . " at: " . date("d-M-Y-h-i-a") . "</div><hr class='custhr' />";
fwrite($fh, $stringData);
fclose($fh);
header( 'refresh: 0; url=http://www.sb0t.tentun.co.uk/Admin/edits.php' );
echo '<div class="motdadded"><h3>Template script edited...</h3></div>';
?>
And then redirects back to the original page... so now what i need to do is put this in to the 1 page only but have no idea how to construct it and how to call it from the form so could someone edit this and construct it to see how such a thing would be done? Thanks! would appriciate it a lot so i can see how it works.
|
|
|
|
03-09-2011, 03:37 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
if you want to combine this 2 files in 1, you would need to add an if statement checking for the submition of the form.
first change the form's action to your current file (and maybe closing the php tag before the html, that way you can take advantage of html highlighting instead of a string):
php Code:
/** edit-text.php **/<?php $myFile = "../includes/docs/sdata.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); ?> <form action= "<?php echo __FILE__; ?>" method= "post" enctype= "multipart/form-data"> <label for= "templateedit">Edit template script</label><br / <textarea name= "templateedit" cols= "90" rows= "20" id= "templateedit"> ' . $theData . '</textarea><br><br> <input type= "submit" name= "save" value= "Save Template script" /> <!--changed the button for an actual submit button--> </form>
Then what you would need is have a big if statement making sure if the submit button value is empty or not, if it is not. If it is not empty (or not set) then the form hasn't been submitted. something like this:
php Code:
<?phpif(isset($_POST[ 'save'] ) && ! empty($_POST[ 'save'] )) { //put here what would you need to do to process the submitted info //in this case, what you have in your second file in your example, without the header() call}$myFile = "../includes/docs/sdata.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); ?> <form action= "<?php echo __FILE__; ?>" method= "post" enctype= "multipart/form-data"> <label for= "templateedit">Edit template script</label><br / <textarea name= "templateedit" cols= "90" rows= "20" id= "templateedit"> ' . $theData . '</textarea><br><br> <input type= "submit" name= "save" value= "Save Template script" /> <!--changed the button for an actual submit button--> </form>
To close I would suggest using $_POST instead of $_REQUEST to access the form info. Request mangles all types of request together, which IMO brings an obscurity when handling requests.
Also take a look to the functions file_get_contents and file_put_contents, they might save you some file handling coding.
|
|
|
|
|
The Following User Says Thank You to tony For This Useful Post:
|
|
03-14-2011, 03:28 PM
|
#3 (permalink)
|
|
The Contributor
Join Date: Feb 2010
Posts: 69
Thanks: 16
|
Ah great stuff that does make sense... i did do a bit of a short test on this with just
PHP Code:
<form action="<?php echo __FILE__; ?>" method="post" enctype="multipart/form-data">
<input name="test1" type="text" />
<input type="submit" name="save" value="Save Template script" />
</form>
<?php
if(isset($_POST['save']) && !empty($_POST['save'])) {
$displaytext = $_POST["test1"];
}
echo $displaytext;
?>
and this pulled out a stupid url of page not found with the url of http://www.tutorials.tentun.co.uk/ho...tdocs/test.php
which made me replace the __FILE__ with 'test.php' instead which made it work... any idea why __FILE__ is doing this? and is the 'test.php' ok to use? never the less i am please with the outcome its exactly what i need!
|
|
|
|
03-14-2011, 03:49 PM
|
#4 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
I was just mentioning __FILE__ so it is dynamically given, so it is not a static name like test.php
I am just to program the more dynamic to the context, the easier to port code somewhere else.
But that shouldn't be a problem to put the actual static name of the file.
And my bad on __FILE__ that was the wrong case to use it. It returns the absolute path based on the file system so in your case is returning /home/fhlinux010/t/tutorials.tentun.co.uk/user/htdocs/test.php
which a public website can't show, since the public doesn't have access like that.
In this case you can use $_SERVER['PHP_SELF'] instead of __FILE__ which would return just test.php or if you have it in a subfolder it would return subfolder/test.php
I don't think I explained it well since I am in a hurry, but if you check PHP_SELF in http://www.php.net/manual/en/reserve...les.server.php it will explain the differences.
|
|
|
|
|
The Following User Says Thank You to tony For This Useful Post:
|
|
03-14-2011, 11:29 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: Feb 2010
Posts: 69
Thanks: 16
|
Ok so doing that i fix it to how i need it
PHP Code:
<?php
if(isset($_POST['save']) && !empty($_POST['save'])) {
//lets delete old file
$myFile = "../../includes/docs/sdata.txt";
unlink($myFile);
//now create a new 1
$ourFileName = "../../includes/docs/sdata.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
//now write the data
$thescript = $_REQUEST["templateedit"];
$myFile = "../../includes/docs/sdata.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "" . $thescript . "";
fwrite($fh, $stringData);
fclose($fh);
//pend to admin log
$ip=$_SERVER['REMOTE_ADDR'];
$myFile = "../../includes/docs/adminlog.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "<div class='qname'>sb0t Template script edited by: " . $ip . " at: " . date("d-M-Y-h-i-a") . "</div><hr class='custhr' />";
fwrite($fh, $stringData);
fclose($fh);
echo '<div class="motdadded"><h3>Template script edited...</h3></div>';
}
$myFile = "../includes/docs/sdata.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo '<form action="<?php echo edits.php; ?>" method="post" enctype="multipart/form-data">
<label for="templateedit">Edit template script</label><br /
<textarea name="templateedit" cols="90" rows="20" id="templateedit">' . $theData . '</textarea><br><br>
<input type="submit" name="save" value="Save Template script" />
<!--changed the button for an actual submit button-->
</form>'
?>
which is roughly how it needs to be... but pulls out error when i press submit
trys going to the url http://www.tutorials.tentun.co.uk/sb...ts.php;%20?%3E
so im guessing its to do with
PHP Code:
echo '<form action="<?php echo edits.php; ?>" method="post" enctype="multipart/form-data">
<label for="templateedit">Edit template script</label><br /
<textarea name="templateedit" cols="90" rows="20" id="templateedit">' . $theData . '</textarea><br><br>
<input type="submit" name="save" value="Save Template script" />
<!--changed the button for an actual submit button-->
</form>'
How would i fix this problem??
also what if i wanted it to call something like...
PHP Code:
<?php
if(isset($_POST['addtop'])) {
$ctoadd = file_get_contents("../includes/docs/sdata.txt");
$ourtmpfile = "../includes/docs/tmp.txt";
$ourtmpFileHandle = fopen($ourtmpfile, 'w') or die("can't open file");
fclose($ourtmpFileHandle);
//write to temp file
$gettmpfile = "../includes/docs/tmp.txt";
$fh = fopen($gettmpfile, 'w') or die("can't open file");
fwrite($fh, $ctoadd);
//now delete the old file
$delfile = "../includes/docs/sdata.txt";
unlink($delfile);
//now its time to create the whole file again
//first things first create the original file but blank
$ouroriginalfile = "../includes/docs/sdata.txt";
$ouroriginalFileHandle = fopen($ouroriginalfile, 'w') or die("can't open file");
fclose($ouroriginalFileHandle);
//now lets writ all the data from here onwards
} else {
}
//-------------------------------------------FILE UPLOAD HERE-------------------------------------------------------
// Configuration - Your Options
$allowed_filetypes = array('.rar', '.zip', 'sfx', '7z', '.tar', '.gzip'); // These will be the types of file that will pass the validation.
$max_filesize = 2097152; // Maximum filesize in BYTES (currently 2MB).
$upload_path = 'RIPQ03L9GH/'; // The place the files will be uploaded to (currently a 'files' directory).
$templatename = $_REQUEST["template"];
$authorname = $_REQUEST["author"];
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('<div class="motdadded">The file you attempted to upload is not allowed.</div><br><br>');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('<div class="motdadded">The file you attempted to upload is too large.</div><br><br>');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('<div class="motdadded">You cannot upload to the specified directory, please CHMOD it to 777.</div><br><br>');
// Lets check to see if the motd already is on server and if it is bail out
$checkfile = 'RIPQ03L9GH/' . $filename;
if (file_exists($checkfile)) {
die('There is already a template called' . $filename . 'Please go back on your browser, change the file name and try again');
} else {
}
// We'll start handling the upload in the next step
?>
<?php
// Configuration - Your Options
$allowed_filetypes = array('.rar', '.zip', 'sfx', '7z', '.tar', '.gzip'); // These will be the types of file that will pass the validation.
$max_filesize = 2097152; // Maximum filesize in BYTES (currently 2MB).
$upload_path = 'RIPQ03L9GH/'; // The place the files will be uploaded to (currently a 'files' directory).
$templatename = $_REQUEST["template"];
$authorname = $_REQUEST["author"];
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('<div class="motdadded">The file you attempted to upload is not allowed.</div><br><br>');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('<div class="motdadded">The file you attempted to upload is too large.</div><br><br>');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('<div class="motdadded">You cannot upload to the specified directory, please CHMOD it to 777.</div><br><br>');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo '<div class="motdadded">ZIP file upload was successful<br><br>'; // It worked.
else
echo '<div class="motdadded">There was an error during the file upload. Please try again.</div><br><br>'; // It failed :(.
//pend to text file
$myFile = "../includes/docs/sdata.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = '<div class="imageElement"><h3>' . $templatename . ' - Author: ' . $authorname . '</h3><p><a href="download.php?f=' . $filename . '"><img src="includes/images/dwnload1.png" alt="Download Template" title="Download Template" /></a></p><a href="#" title="Current template" class="open"></a>';
fwrite($fh, $stringData);
fclose($fh);
?>
<?php
///////////////////////////////////IMAGE UPLOAD HERE////////////////////////////////////////////////////
// Configuration - Your Options
$allowed_filetypes = array('.bmp', '.jpg','.png', '.gif'); // These will be the types of file that will pass the validation.
$max_filesize = 2097152; // Maximum filesize in BYTES (currently 2MB).
$upload_path = 'Previews/B184U4/'; // The place the files will be uploaded to (currently a 'files' directory).
$filename = $_FILES['imgfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('<div class="motdadded">The file you attempted to upload is not allowed.</div><br><br>');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['imgfile']['tmp_name']) > $max_filesize)
die('<div class="motdadded">The file you attempted to upload is too large.</div><br><br>');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('<div class="motdadded">You cannot upload to the specified directory, please CHMOD it to 777.</div><br><br>');
// Lets check to see if the motd already is on server and if it is bail out
$checkfile = 'files/' . $filename;
if (file_exists($checkfile)) {
die('There is already an Image called' . $filename . 'Please change the name and try again');
} else {
}
// We'll start handling the upload in the next step
?>
<?php
// Configuration - Your Options
$allowed_filetypes = array('.bmp', '.jpg','.png', '.gif'); // These will be the types of file that will pass the validation.
$max_filesize = 2097152; // Maximum filesize in BYTES (currently 2MB).
$upload_path = 'Previews/B184U4/'; // The place the files will be uploaded to (currently a 'files' directory).
$filename = $_FILES['imgfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('<div class="motdadded">The file you attempted to upload is not allowed.</div><br><br>');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['imgfile']['tmp_name']) > $max_filesize)
die('<div class="motdadded">The file you attempted to upload is too large.</div><br><br>');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('<div class="motdadded">You cannot upload to the specified directory, please CHMOD it to 777.</div><br><br>');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['imgfile']['tmp_name'],$upload_path . $filename))
echo '<div class="motdadded">IMAGE file upload was successful<br><br>'; // It worked.
else
echo '<div class="motdadded">There was an error during the file upload. Please try again.</div><br><br>'; // It failed :(.
//pend to text file
$myFile = "../includes/docs/sdata.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = '<img src="templates/Previews/B184U4/' . $filename . '" class="full" /><img src="templates/Previews/B184U4/' . $filename . '" class="thumbnail" /></div>';
fwrite($fh, $stringData);
fclose($fh);
if(isset($_POST['addtop'])) {
//now lets pull out the tmp data (old data)
$ctoadd = file_get_contents("../includes/docs/tmp.txt");
$ctoadd2 = file_get_contents("../includes/docs/sdata.txt");
//now input the old data to the file
$myoldfile = "../includes/docs/sdata.txt";
$ctoadd3 = $ctoadd2 . $ctoadd;
$fhtmp = fopen($myoldfile, 'w') or die("can't open file");
fwrite($fhtmp, $ctoadd3);
fclose($fhtmp);
//now remove the old tmp file
$deloldtmp = "../includes/docs/tmp.txt";
unlink($deloldtmp);
//$myFile = "../includes/docs/sdata.txt";
//$fh = fopen($myFile, 'r');
//$theData = fread($fh, filesize($myFile));
//fclose($fh);
//echo $theData;
} else {
}
//pend to admin log
$ip=$_SERVER['REMOTE_ADDR'];
$myFile = "../includes/docs/adminlog.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "<div class='qname'>New Template from: " . $ip . "</div><hr class='custhr' />";
fwrite($fh, $stringData);
fclose($fh);
?>
<?PHP
$templatename = $_REQUEST["template"];
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
echo $filename;
$ctoadd = file_get_contents("../Admin/Docs/latesttmps.txt");
$ourtmpfile = "../Admin/Docs/latesttmpstmp.txt";
$ourtmpFileHandle = fopen($ourtmpfile, 'w') or die("can't open file");
fclose($ourtmpFileHandle);
//write to temp file
$gettmpfile = "../Admin/Docs/latesttmpstmp.txt";
$fh = fopen($gettmpfile, 'w') or die("can't open file");
fwrite($fh, $ctoadd);
//now delete the old file
$delfile = "../Admin/Docs/latesttmps.txt";
unlink($delfile);
//now its time to create the whole file again
//first things first create the original file but blank
$ouroriginalfile = "../Admin/Docs/latesttmps.txt";
$ouroriginalFileHandle = fopen($ouroriginalfile, 'w') or die("can't open file");
fclose($ouroriginalFileHandle);
//now lets writ all the data from here onwards
//pend to text file
$myFile = "../Admin/Docs/latesttmps.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = '<a href="download.php?f=' . $filename . '">' . $templatename . '(sb0t)</a> • ' . "\r\n";
fwrite($fh, $stringData);
fclose($fh);
//now lets pull out the tmp data (old data)
$ctoaddtmp = file_get_contents("../Admin/Docs/latesttmpstmp.txt");
$ctoaddorg = file_get_contents("../Admin/Docs/latesttmps.txt");
//now input the old data to the file
$myoldfile = "../Admin/Docs/latesttmps.txt";
$ctoadd3 = $ctoaddorg . $ctoaddtmp;
$fhtmp = fopen($myoldfile, 'w') or die("can't open file");
fwrite($fhtmp, $ctoadd3);
fclose($fhtmp);
//now remove the old tmp file
$deloldtmp = "../Admin/Docs/latesttmpstmp.txt";
unlink($deloldtmp);
?>
Which needs to be wrapped in 2 individual php tags or would my best bet be to get a new upload script. If this is so it would still be greatful to know how to do such a thing.
All the help is much appriciated! 
|
|
|
|
03-15-2011, 02:05 AM
|
#6 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
the error is in the line with echo '<form action="<?php echo edits.php; ?>"...
if you already are inside the php tags you don't need to put them. So it should be echo '<form action="edits.php"...
About your second question, I don't seem to understand what you are trying to do with that code. Can you post the pseudo-code steps of what you want the second code to do?
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|