[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

upload file to server on the fly

I have a script that generates html on the fly, what I now need to do is take that html code save it as a php and upload to my server on the fly.

How can this be done?
0
submissiontechnology
Asked:
submissiontechnology
  • 7
  • 6
1 Solution
 
Diablo84Commented:
Is the script that generates the html on the same server space that you wish to save the html in?

If so, you could just assign the generated html to a string and write the string to a new file. (Using 'w' mode in fopen).

For example:

<?php
$filename = $_SERVER['DOCUMENT_ROOT'].'/test/mynewfile.html';
$string = "<html> etc </html>"; //this would be your generated html assigned to a string

$handle = fopen($filename,'w');
if (fwrite($handle, $string) === FALSE) {
 echo 'Error saving data';
 exit;
}
echo 'Done';
fclose($handle);
?>

Note: The directory you are saving the file in (/test/ in my example) would have to have write permission otherwise you will see permission errors.

Diablo84
0
 
submissiontechnologyAuthor Commented:
What about if the html had php in it? ie <?=$fname?> would it work still?
0
 
Diablo84Commented:
As long as it is stored as a string, you will probably want to make use of single quotes which are not parsed, for example:

$string = '
<div id="someid">
<?php
$var = "some value";
echo $var;
?>
</div>
';

You could then add more to the output to be saved as it's generated  like this for example:

$string .= '
<morehtml>etc
';

Note that, if the update is being done via a form, all you would need to do is write:

stripslashes($_POST['name_of_the_textarea'])

to the file.

Diablo84
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
submissiontechnologyAuthor Commented:
Say the html was:

<html>
hello <?=$f?>
</html>

and this file was made on the fly from your above snipit.

If I run the url: http://www.*****.co.uk/email/test.php?f=Paul

all i see is the hallo and not hello Paul

Why?
0
 
Diablo84Commented:
It should be:

<html>
hello <? echo $_GET['f']; ?>
</html>

If you have register globals off...
0
 
submissiontechnologyAuthor Commented:
ok

I'm doing this:

<?php
$filename = $_SERVER['DOCUMENT_ROOT'].'/email/test.php';
$string = "
<html>
<?php
$f = $_GET['f'];
?>
hello <?=$f?>
</html>
"; //this would be your generated html assigned to a string

$handle = fopen($filename,'w');
if (fwrite($handle, $string) === FALSE) {
echo 'Error saving data';
exit;
}
echo 'Done';
fclose($handle);
?>




and i get this error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/virtual/site4/fst/var/www/html/email/file.php on line 6
0
 
Diablo84Commented:
It's best to use single quotes so that the string is not parsed and then escape the two single quotes used with the $_GET part, for example:

$string = '
<html>
<?php
$f = $_GET[\'f\'];
?>
hello <?=$f?>
</html>
';
0
 
submissiontechnologyAuthor Commented:
Bingo works a treat thanks mate :)
0
 
Diablo84Commented:
no problem :)
0
 
submissiontechnologyAuthor Commented:
I'm trying to put the date as the file name but it just wont let me do it:

<?php
$date = date("d-m-Y");
$filename = $_SERVER['DOCUMENT_ROOT'].'/email/$date.php';
$string = '
<?php
$f= $_GET[\f\'];
?>
<html>
hello <?=$f?>
</html>
'; //this would be your generated html assigned to a string

$handle = fopen($filename,'w');
if (fwrite($handle, $string) === FALSE) {
echo 'Error saving data';
exit;
}
echo 'Done';
fclose($handle);
?>
0
 
submissiontechnologyAuthor Commented:
I know why it was cos of $filename was using ' not " so it could not pass $date
0
 
Diablo84Commented:
Apologies for the delayed reply, i have been offline since my last comment.

If you was to use single quotes for it you would have to concatenate the variable, for example:

$filename = $_SERVER['DOCUMENT_ROOT'].'/email/'.$date.'.php';

Otherwise, and probably a little tidier in this case, you have to use double quotes as you realised:

$filename = $_SERVER['DOCUMENT_ROOT']."/email/$date.php";

Diablo84
0
 
submissiontechnologyAuthor Commented:
Yeah got it thanks mate :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now