PHP write file... need help - php4

<?php
$title = $_POST['title'];
$filename = $title , ".php";
$fh = fopen($filename, 'w') or die ("can't open file");
$stringData = $title;
fwrite($fh, $stringData);
$stringData = $blog;
fwrite($fh, $stringData);
fclose($fh);
?>
This is only a sample. What is the correct code for that?

You are using the correct code there, what is the point?
Also note that you are using a comma rather than a dot to concatenate strings at:
$filename = $title , ".php";

In your example opening a file using POST is an insecure method, so don't even think about this kinda tricks :P
You can use file read and write in simple method
file_get_contents();
echo $fileData = file_get_contents('filename.txt');
file_put_contents();
$data= 'some data';
// Write the contents back to the file
file_put_contents("filename.txt", $data);

Related

Cant Get data from explode function

I tried to echo a row from html data but it shows blank page . Please help.
<?php
$url_data = file_get_contents('http://www.nowgoal.com/analysis/1374177.html');
$rows = explode("\n",$url_data);
echo $rows[1];?>
Try this code
$f = fopen ("file.html", "r");
$ln = 0;
while ($line= fgets ($f)) {
if ($line===FALSE) print ("FALSE\n");
else $rows[$ln] = htmlspecialchars($line);
$ln++;
}
print $rows[1];
fclose ($f);

htmlspecialchars($_SERVER['PHP_SELF'])

if($_POST) {
$mysql_pass = '';
$connect = #mysql_connect($_POST['dbHost'], $_POST['dbUser'], $mysql_pass);
if($connect) {
if(#mysql_select_db($_POST['dbName'])) {
$dir = $_SERVER['SERVER_NAME']. $_SERVER['REQUEST_URI'];
$redirectUrl = str_replace("configForm.php","site/index", $dir);
print $redirectUrl; exit;
$dbConf = dirname(__FILE__).'/dbConfig/dbconf.php';
$handle = fopen($dbConf, 'w') or die('Cannot open file: '.$dbConf);
$data = '<?php $userName="'.$_POST['dbUser'].'";'."\n";
$data .= '$passWord=" ";'."\n";
$data .= '$dbName="'.$_POST['dbName'].'";'."\n";
$data .= '$host="'.$_POST['dbHost'].'"; ?>';
fwrite($handle, $data);
header('Location: site/index');
}
else {
$error = 'Could not select database.';
}
}
else
$error = 'Not connected';
}
when the code above was executed, my webpage just displayed "localhost/www-edusec/site/index". i do not know where i do it wrong. my form action is like below:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
The action attribute defaults to the current page anyway, so that piece of code is surely redundant.
The problem is PHP_SELF returns the path to the file too. Just get rid of the action line completely and it should work fine.
EDIT:
Also, the reason your script is cutting off half way, you have an exit; after it prints out the directory.

Downloading attachment from Exchange message with Perl

I am automatically downloading mails from an Exchange 2010 server via perl. So far I have managed to access the message via Exchange Web Services (EWS) and parse headers. Now I wonder how I can download the attachments of a message to a local temporary folder.
I am new to Perl language and cannot find the source code or documentation for the message data structure. Any help is appreciated.
use Email::Folder::Exchange;
use Email::Simple;
# some more code here....
my $folder = Email::Folder::Exchange->new($url, $user, $pass);
for my $message ($folder->messages) {
if ($message->header('Subject') =~ /Downloadable Message/) {
// How to access message's attachments?
}
}
So basically the trick is to convert the Email::Simple to Email::MIME and use Email::MIME::Attachment::Stripper to parse through each attachment. Easy ;-)
! I only copied the relevant parts... so you might need to extend it a little for reuse.
use Email::Folder::Exchange;
use Email::Simple;
use Email::MIME::Attachment::Stripper;
# some more code here....
my $folder = Email::Folder::Exchange->new($url, $user, $pass);
for my $message ($folder->messages) {
my $tmpMsg = Email::MIME->new($message->as_string);
my $stripper = Email::MIME::Attachment::Stripper->new($tmpMsg);
for my $a ($stripper->attachments()) {
next if $a->{'filename'} !~ /csv/i; #only csv attachments
my $tempdir = "C:\\temp\\";
my $tmpPath = $tmpdir . $a->{'filename'};
# Save file to temporary path
my $f = new IO::File $tmpPath, "w" or die "Cannot create file " . $tmpPath;
print $f $a->{'payload'};
}
}

Unable to upload web page

I am trying to upload a file from a web page. I use CGI to query for all of the input fields but "my $upload_filehandle = $query->upload("fileInput");" is always empty even when "my $file = $query->param("fileInput");" has properly fetched the file name from the same field. Here is the code:
use CGI;
$CGI::POST_MAX = 1024 * 5000;
my $query = CGI->new;
my $url = $query->param("urlInput");
my $file = $query->param("fileInput");
my $upload_filehandle = $query->upload("fileInput");
my $text = $query->param("textInput");
my $k = $query->param("kInput");
Any advice is appreciated.
Regards.
I will repost my comment, as an answer:
make sure your HTML form contains the enctype attribute, and that it's set to enctype="multipart/form-data"

Why can't Perl's WWW::Mechanize find the form by field names?

#!/usr/bin/perl
use WWW::Mechanize;
use Compress::Zlib;
my $mech = WWW::Mechanize->new();
my $username = ""; #fill in username here
my $keyword = ""; #fill in password here
my $mobile = $ARGV[0];
my $text = $ARGV[1];
$deb = 1;
print length($text)."\n" if($deb);
$text = $text."\n\n\n\n\n" if(length($text) < 135);
$mech->get("http://wwwl.way2sms.com/content/index.html");
unless($mech->success())
{
exit;
}
$dest = $mech->response->content;
print "Fetching...\n" if($deb);
if($mech->response->header("Content-Encoding") eq "gzip")
{
$dest = Compress::Zlib::memGunzip($dest);
$mech->update_html($dest);
}
$dest =~ s/<form name="loginForm"/<form action='..\/auth.cl' name="loginForm"/g;
$mech->update_html($dest);
$mech->form_with_fields(("username","password"));
$mech->field("username",$username);
$mech->field("password",$keyword);
print "Loggin...\n" if($deb);
$mech->submit_form();
$dest= $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
$dest = Compress::Zlib::memGunzip($dest);
$mech->update_html($dest);
}
$mech->get("http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0");
$dest= $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
$dest = Compress::Zlib::memGunzip($dest);
$mech->update_html($dest);
}
print "Sending ... \n" if($deb);
$mech->form_with_fields(("MobNo","textArea"));
$mech->field("MobNo",$mobile);
$mech->field("textArea",$text);
$mech->submit_form();
if($mech->success())
{
print "Done \n" if($deb);
}
else
{
print "Failed \n" if($deb);
exit;
}
$dest = $mech->response->content;
if($mech->response->header("Content-Encoding") eq "gzip")
{
$dest = Compress::Zlib::memGunzip($dest);
#print $dest if($deb);
}
if($dest =~ m/successfully/sig)
{
print "Message sent successfully" if($deb);
}
exit;
When run this code halts with an error saying:
There is no form with the requested fields at ./sms.pl line 65
Can't call method "value" on an undefined value at /usr/share/perl5/vendor_perl/WWW/Mechanize.pm line 1348.
I would guess that there is no form with the fields "MobNo" & "textArea" in http://wwwl.way2sms.com//jsp/InstantSMS.jsp?val=0 which indeed there aren't since the page at that URL lacks even a <body> tag.
$dest =~ s/<form name="loginForm"/<form action='..\/auth.cl' name="loginForm"/g;
find the above line in the script and replace it with the following
$dest =~ s/<form name="loginForm"/<form action='..\/Login1.action' name="loginForm"/ig;
This is required since recently way2sms has restructured its home page and hence the auth.cl form was renamed to Login1.action
When I run into these sorts of problems, I print the entire HTML page so I can look at it. The form you are expecting probably isn't there. I suspect that you aren't getting the page that you think you are.
The first page does quite a bit of JavaScript processing to submit the form. Since WWW::Mechanize doesn't handle any of those bits for you, I'm guessing that your first form submission is somehow incomplete or invalid, so the next page you get is some sort of error page. That's a quite common problem with dynamic websites.
You can also compare what Mech does with what a JavaScript-enabled browser does. Use some sort of HTTP sniffing tool to watch the transactions. Is the interactive browser doing something extra that Mech isn't?