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';
Related
I am trying to extract the contents of a folder within a tarball using PHP. I am using the following PHP to download and extract the archive:
<?php
function wget($address,$filename) {
file_put_contents($filename,file_get_contents($address));
}
$newdir = 'test';
echo '<br>Downloading latest gzipped WordPress tarball';
wget('http://wordpress.org/latest.tar.gz', 'latest.tar.gz');
echo '<br>about to Extract from gz';
// decompress from gz
$p = new PharData('latest.tar.gz');
$p->decompress(); // creates files.tar
echo '<br>Extracted from gz';
// unarchive from the tar
$phar = new PharData('latest.tar');
echo '<br>Un-TARd';
$phar->extractTo($newdir);
echo '<br>Complete';
?>
My problem is that this script extracts the tarball into /test/wordpress whereas I need it to extract to /test/. I have read through this documentation on the PHP.net Manual and replaced part of my code to meet one of the examples there. The code I had was:
$phar->extractTo($newdir);
And I changed that to:
$phar->extractTo($newdir, 'wordpress');
But that didn't work. The PHP script processed through to the end but the /test/ directory was empty.
The aim of this is to create a one-click WordPress install on our local dev server.
I know the thread is very old and you probably found solution but maybe I'll save someone else time.
Funcion extractTo expects slash at the end of directory name you want to extract.
So $phar->extractTo($newdir, 'wordpress/'); should work.
I am able to put the following url in any browser and the xml appears after a few seconds...
ftp://USER:PASSWORD#aphrodite.WEBSITE.net/exports/xml/products.xml
I tried the following code in a php file so I can run a cron daily at midnight and for it to save the xml file on my server. There is a xml file being saved in my data directory but it is blank. Any ideas?
<?php
$content = file_get_contents('ftp://USER:PASSWORD#aphrodite.WEBSITE.net/exports/xml/products.xml');
file_put_contents('./data/products.xml', $xml);
?>
Try this:
<?php
$xml = file_get_contents('ftp://USER:PASSWORD#aphrodite.WEBSITE.net/exports/xml/products.xml');
file_put_contents('./data/products.xml', $xml);
?>
Your $content is not used in the file_put_contents method call so you are not writing anything to the file. I changed the code so that the data gets written to the file.
Nothing wrong with your code since file_get_contents almost supports all the protocols, But you may need to change $xml to $content because as I see $xml variable does not exists in your code.
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 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 .
How can i print my html file via php script? I just want to run it in background without any prompt. I have read other posts regarding this but still didnt find anything working.
I tried this one :
<?php
$dir = "temp"; // the folder that you are storing the file to be printed
$file = "file.html"; //change to proper file name
$file_dir = $dir.$file;
$server = "home_computer"; //name of the computer you are printing to on your network
$printer = "HP"; //printers shared name
$command = "print $file_dir /d:\\$server\\$printer";
exec($command) or die("File failed to print");
?>
got this example here http://www.phpfreaks.com/forums/index.php/topic,207946.0.html
here is what i got working :
$html = "testing print";
$handle = printer_open();
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, $html);
printer_close($handle);
We require php_printer.dll php extension to make this work in php5. :)
You can't print html pages with php. Php is a server-side language, it runs on the server.
The printer is on the client's machine. Meaning you'll need a client-side language to accomplish this.
If you want to print the source code then it should be be doable by writing a program that prints the passed string and then calling it via a system call. On windows it appears to have an extension for that.
If you want to print a rendered version, then you have to know that for that you need some kind of a rendering engine. While not impossible its probably more work than what you want to get into.