Is it possible to base64 encode the file at a URI? - php

I've just been fiddling around with PHP, and I want to know if it's possible to do such a thing.
If so, can you please provide example code?
Update: What I mean, is I want to encode whatever file may be at the URI, not the URI itself. Sorry if I'm not clear.

You can put:
base64_encode(file_get_contents('http://www.google.com'));

I don't see why not...
<?php $file = file_get_contents("http://example.com/myfile.txt");
$encoded = base64_encode($file);

file_get_contents function will grab the HTML of given URL and base64_encode function will encode it.
<?php
$content = file_get_contents("http://google.com");
$encodedContent = base64_encode($content);
?>

You want to base64-encode a URL? Simple...
base64_encode($uri)

Related

Get text from URL txt

Is there any way to get a simple php code that just displays the text from a txt document, that's on a url?
This is what I've got so far. I'm sure I'm missing something~
<?
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
fopen ($text)
?>
Guessing you can't open it, since it's not on the drive. Any workaround that, or fix you guys could help me with?
Thanks (:
Too easy! :)
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
Explanation:
file_get_contents() will return the contents of the remote file and assign it to the $text variable. Then you'll just have to ouput these contents using the echo statement
Alternative: use the readfile() function. It will output the contents of the file directly:
readfile('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
You don't have to open it. It's already appended as a string to the variable $text.
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
//Outputs "“Why Don't You Get A Job?” ― The Offspring"

Why echo $this->uri->segment(3) output is different then what i see in url

So i have url like that:
http://localhost/testproject/data/getData/幻想
And then in function getData i have that:
public function getData($title){
echo $title;
}
And in output i see:
%E5%B9%BB%E6%83%B3
Same is if i try $this->uri->segment(3).
Why is that so, and how to fix it? I also noticed that if i paste whole url here i get:
http://localhost/testproject/data/getData/%E5%B9%BB%E6%83%B3
I'm realy at dead end here...
Btw. file is encoded in UTF8.
Use this
site_url('data/getData/'.urlencode('幻想'));
On php end use this
$segment = $this->uri->segment(3);
$value = urldecode($segment);
See if this works for you.
you can use urldecode look at this http://php.net/manual/fr/function.urldecode.php
because of spacial characters, white spaces.
try encode or decode your url to see correct one you can user urldecode() function or urlencode() depends on the way you want to display...
http://www.php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php

Get variables from base64_encode url?

I'm studying a way of encoding or encrypting my url variables, but I don't see how to get them back...
Having this
_link_=Home&nc=1&plw=950&pmw=0&prw=
I used to do $_GET['_link_']
but when base64_encode the url like
base64_encode("_link_=Home&nc=1&plw=950&pmw=0&prw=");
echo base64_decode($string);
how do I get my _link_ or my nc variables back?
Thanks
Use http://php.net/manual/en/function.parse-str.php after base64 decode
If you have something like http://www.example.com/?BASE64ENCODEDSTRINGHERE, you can do the following:
$query_string = base64_decode($_SERVER['QUERY_STRING']);
parse_str($query_string, $getdata);
In this case you will have same data in $getdata array as you would normally have in $_GET.

convert this to japanese character (encoding)

Please help me convert this... I dont know how to call this kind of data, is it hex?
2%E6%9C%8819%E6%97%A5
I believe that this should be printed as 2月19日
How can I convert 2%E6%9C%8819%E6%97%A5 to be 2月19日 using PHP?
Thank You
That looks like it's URL encoded http://en.wikipedia.org/wiki/Url_encoding
PHP UrlDecode might do the trick.
http://www.php.net/manual/en/function.urldecode.php
There's an example at that link which shows url decoding the querystring of a page. I don't know PHP, but you might want soemthing like the following:
<?php
$original = "2%E6%9C%8819%E6%97%A5";
$decoded = urldecode($original);
echo $decoded;
?>

How to URLENCODE url with variables?

I need to URLENCODE this:
<?php echo 'chart.php?api_url=http://0.chart.apis.google.com/chart?'.$chart1title.$chart1type.$chart1size.$chart1barsize.$chart1gridlines.$chart1data.$chart1color.$chart1bgcolor.$chart1visibleaxis.$chart1axislabels.$chart1axisdatascale.$chart1axisranges.'alt="answeredcalls"';?>
And then decode it on the other side. How can I do this???
'chart.php?api_url=' . urlencode('http://0.chart.apis.google.com/chart?'.$chart1title.$chart1type.$chart1size.$chart1barsize.$chart1gridlines.$chart1data.$chart1color.$chart1bgcolor.$chart1visibleaxis.$chart1axislabels.$chart1axisdatascale.$chart1axisranges.'alt="answeredcalls"');
Generally you won't have to use urldecode() when accessing $_GET parameters in PHP. It will be, essentially, decoded for you.
The previous answer is a good solution to the encoding part of the question.

Categories