I am using php for one of my projects. I have a URl which has the set of XML of a specific site. The URL is in the format www.sitefeed.com/somekey&&gZipCompress=yes. I want to read the contents of this file with out downloading it into my server.
I tried compress.zlib: in front of the URL but it returned an empty array.
Thanks
If you have your php variable "allow_url_fopen" on, you can use:
$lrc = gzopen($the_link, "r");
$text ="";
while(!gzeof($lrc)){
$text .= gzread($lrc, 1024);
}
gzclose($lrc);
If not, you can use file_get_contents, or even curl .
Related
Good Day All
I have a .php file which I want to edit via fopen() and file_get_content() functions. However, my file contains some php codes as well and I managed to get the content out of my file but without the php part. Also, I have tried the eval() (I know it's not suggested!) function with same results. I was wondering if there could be a way to get whatever is inside that file regardless wether it's text or codes.
Thanks
Here is the code I used:
public function editwarning()
{
$filename = "http://www.parkho.ir/admin/templates/pm/email_warning.php";
$content = file_get_contents($filename);
echo $content;
}
You have two options:
1) pass the file PATH to the $filename var:
$filename = "/var/www/app/email_warning.php"; // <--- replace /var/www/app for your path
2) Or You need to use htmlentities():
<?php
$content = htmlentities(file_get_contents($filename));
echo $data;
I want to store some data retrieved using an API on my server. Specifically, these are .mp3 files of (free) learning tracks. I'm running into a problem though. The mp3 link returned from the request isn't to a straight .mp3 file, but rather makes an ADDITIONAL API call which normally would prompt you to download the mp3 file.
file_put_contents doesn't seem to like that. The mp3 file is empty.
Here's the code:
$id = $_POST['cid'];
$title = $_POST['title'];
if (!file_exists("tags/".$id."_".$title))
{
mkdir("tags/".$id."_".$title);
}
else
echo "Dir already exists";
file_put_contents("tags/{$id}_{$title}/all.mp3", fopen($_POST['all'], 'r'));
And here is an example of the second API I mentioned earlier:
http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts
Is there some way to bypass this intermediate step? If there's no way to access the direct URL of the mp3, is there a way to redirect the file download prompt to my server?
Thank you in advance for your help!
EDIT
Here is the current snippet. I should be echoing something, correct?
$handle = fopen("http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts", 'rb');
$contents = stream_get_contents($handle);
echo $contents;
Because this echos nothing.
SOLUTION
Ok, I guess file_get_contents is supposed to handle redirects just fine, but this wasn't happening. So I found this function: https://stackoverflow.com/a/4102293/2723783 to return the final redirect of the API. I plugged that URL into file_get_contents and volia!
You seem to be just opening the file handler and not getting the contents using fread() or another similar function:
http://www.php.net/manual/en/function.fread.php
$handle = fopen($_POST['all'], 'rb')
file_put_contents("tags/{$id}_{$title}/all.mp3", stream_get_contents($handle));
I want to download a remote file and put it in my server directory with the same name the original has. I tried to use file_get_contents($url).
Problem is that the filename isn't included in $url, it is like: www.domain.com?download=1726. This URL give me, e.g.: myfile.exe, so I want to use file_put_contents('mydir/myfile.exe');.
How could I retrieve the filename? I tried get_headers() before downloading, but I only have file size, modification date and other information, the filename is missing.
I solved it another way. I found that if there is no content-disposition in url headers, then filename exists in URL. So, this code works with any kind of URL's (no cURL needed):
$url = "http://www.example.com/download.php?id=123";
// $url = "http://www.example.com/myfile.exe?par1=xxx";
$content = get_headers($url,1);
$content = array_change_key_case($content, CASE_LOWER);
// by header
if ($content['content-disposition']) {
$tmp_name = explode('=', $content['content-disposition']);
if ($tmp_name[1]) $realfilename = trim($tmp_name[1],'";\'');
} else
// by URL Basename
{
$stripped_url = preg_replace('/\\?.*/', '', $url);
$realfilename = basename($stripped_url);
}
It works! :)
Based on Peter222 's code i wrote a function to get the filename.
You can use the $http_response_header variable:
function get_real_filename($headers,$url)
{
foreach($headers as $header)
{
if (strpos(strtolower($header),'content-disposition') !== false)
{
$tmp_name = explode('=', $header);
if ($tmp_name[1]) return trim($tmp_name[1],'";\'');
}
}
$stripped_url = preg_replace('/\\?.*/', '', $url);
return basename($stripped_url);
}
Usage: ($http_response_header will be filled by file_get_contents())
$url = 'http://example.com/test.zip';
$myfile = file_get_contents($url);
$filename = get_real_filename($http_response_header,$url)
file_get_contents() over the HTTP wrapper does not directly download the file if the file has been pre-parsed by the webserver.
Take the following example: if you call file_get_contents() on a remove webpage (example.com/foobar.php), you won't be presented with the source code of foobar.php, but how the webserver of example.com parses the PHP file. So you will only be able to retrieve the generated HTML output.
If the filename is not present in the URL and there is no way that you can fetch it from anywhere, then you are in a dead end. Data can't be just summoned from the transcendental field of datum.
For alternative solutions, I can only suggest using the cURL library (it is used to handle queries from your server (as it were a client) to other servers with using URL, hence the name cient URL) or file sockets. Here is another question's answer on Stack Overflow which describes how to fetch filename using cURL.
Also, you might try getting in touch with the administrator/maintainer/webmaster team of domain.com, asking if they had a publicly available API to fetch filenames and other metadata.
This might sound really "nooby" but I need to find a way for PHP to download an XLS file to a server folder. This file is not stored in another server, it is dynamically generated with another PHP script.
This is what I got from browsing the web but it's not working:
<?php
$url = "http://localhost/ProyectoAdmin/admin/export_to_excel.php?id=1&searchtype_id=2";
$local_file_path = './xls_tmp/Report.xls';
$xlsFile = file_get_contents($url);
file_put_contents($file_path,$xlsFile);
?>
I'd really appreciate any hint.
You're missing an end quote on your second line.
It should be: $local_file_path = './xls_tmp/Report.xls';
I have a simple code written (based on some tutorials found around the internet) to parse and display an XML file. However, I only know how to reference an XML file stored on my server and I would like to be able to use an XML file that is being returned to me from a POST.
Right now my code looks like this:
if( ! $xml = simplexml_load_file('test.xml') )
{
echo 'unable to load XML file';
}
else
{
foreach( $xml as $event)
{
echo 'Title: ';
echo "$event->title<br />";
echo 'Description: '.$event->info.'<br />';
echo '<br />';
}
}
Is there some way I can replace the simpleXML_load_file function with one that will allow me to point to the POST URL that returns the XML file?
Use simplexml_load_string instead of loadfile:
simplexml_load_string($_POST['a']);
If you get the url to the file in the POST you can propably use the simplexml_load_file function with the url, but if that doesn't work you can use the file_get_contents in combination with the simplexml_load_string:
//say $_POST['a'] == 'http://example.com/test.xml';
simplexml_load_file($_POST['a']); // <-- propably works
simplexml_load_string(file_get_contents($_POST['a'])); //<-- defenitly works (propaly what happens internally)
also getting contents of external files could be prohibited by running PHP in safe mode.
If you are receiving a file that's been uploaded by the user, you can find it (the file) looking at the content of the $_FILES superglobal variable -- and you can read more about files uploads here (for instance, don't forget to call move_uploaded_file if you don't want the file to be deleted at the end of the request).
Then, you can work with this file the same way you already do with not-uploaded files.
If you are receiving an XML string, you can use simplexml_load_string on it.
And if you are only receiving the URL to a remote XML content, you have to :
download the file to your server
and, then, parse its content.
This can be done using simplexml_load_file, passing the URL as a parameter, if your server is properly configured (i.e. if allow_url_fopen is enabled).
Else, the download will have to be done using curl -- see curl_exec for a very basic example, and curl_setopt for the options you can use (you'll especially want to use CURLOPT_RETURNTRANSFER, to get the XML data as a string you can pass to simplexml_load_string).
From http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=php4:
If you do not want to save the
uploaded file directly but to process
it, the PHP functions
file_get_contents() and fread() can
help you. The file_get_contents()
function returns a string that
contains all data of the uploaded
file:
if (is_uploaded_file($_FILES['myFile']['tmp_name']))
$fileData = file_get_contents($_FILES['myFile']['tmp_name']);
That will give you a handle on the raw text within that file. From there you will need to parse through the XML. Hope that helps!
Check out simplexml_load_string. You can then use cURL to do the post and fetch the result. An example:
<?php
$xml = simplexml_load_string($string_fetched_with_curl);
?>