PHP - Get all XML files in directory from external URL - php

I'm trying to use glob() to get all of the XML files from an external URL, but I'm not getting any results back
foreach(glob("http://www.externalsite.com/files/*xml") as $filename) {
$xml_file = file_get_contents($filename, FILE_TEXT);
echo $filename;
}
Is this possible? Or is there another method of doing this?

Related

Iterate thru possible file extension to get the proper extension

I have a folder with many files in various formats eg .jpg, .png, .pdf, .doc etc... The files are on a remote server. I have a json file with list of filenames and its location but missing the extensions.
I want to rebuild the json file and add the proper extension to filename. How can I do this with php? Can anyone give me any ideas how to iterate thru possible extensions to get the right filename + ext on the server.
eg. I have a url like this - http://www.somesite.com/filename. I know on the server the file is pdf but how can I do this programatically for many files which may be different and rename the url?
Any ideas?
Use a For Loop To loop over all current know file extensions and execute a GET request to the $url . $extension and see if the server returns a file.
If the server returns a file, you can break the for loop.
You can nest 2 for loops in each other to do this far all know urls.
Example
$files = [
"http://test.com/test",
"http://test.com/test2"
];
$extensions = [
".jpg",
".docx"
];
foreach ($files as $file)
{
foreach ($extensions as $extension)
{
$foundFile = // Get requests here
if(FILE_IS_FOUND){
// Store file where ever you need it
break;
}
}
}
This example uses a Foreach Loop

PHP How to read entire TXT file

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;

Not able to read file in php

I am facing the issue with this code:
<?php
$files = scandir("D:/Dummy");
foreach($files as $file) {
$filenam = $file;
$path_to_file = $filenam;
$file_contents = file_get_contents($path_to_file);
echo "Hello ".$filenam;
$printFileName="";
if(strpos("9222339940", $file_contents) === false)
{
$printFileName=$filenam." ";
}
}
echo $printFileName;
?>
Basically, I have written this code to scan all the files in the directory and from the each file, I need to replace the mobile number. But for some reason, I'm not able to run the script. It is throwing error:
file_get_contents(name of the file) failed to open stream. No such file or directory error.
The scandir() function of PHP will only return the basenames of the files within the directory. That is, if your directory D:\Dummy contains a file test.txt, then scandir() will not return the full path D:\Dummy\test.txt, but only test.txt. So the PHP process will not find the file, because you need to provide the complete path of the file.

Copying XSL Feed from one URL to another URL

There is an RSS feed:
www.domain1.com/rss.xsl
I need to copy this xsl feed to another webhost to make
www.domain2.com/rsscopy.xsl
How can I do this? Can I do it via PHP?
Thanks :)
You need to use file_get_contents and file_put_contents functions.
<?php
$rss = file_get_contents('http://www.domain1.com/rss.xsl');
$filename = 'rssCopy.xsl';
$saveRss = file_put_contents($filename, $rss);
if($saveRss)
{
echo 'RSS Copied';
}
Save the above script in your domain2.com and call it via URL.

provide latest xml file php

maybe someone can help me, i provide xml files witch are generated from a PHP DB query and each xml file has a unique name. Now i want to prepare a function like "get the latest xml file" but I don't know whats the best way!
$xml = simplexml_load_file('test.xml');
I found this function but there i have to know the exact name!
or ist something like this possible:
$xml = simplexml_load_file('test.php');
and in the test.php i have a function to get the last name, but how to i provide the xml data?
Some keywords how i can find a solution in google would be very helpful!
The first parameter to that function is a string of the filename. The file should be the XML file to load, so you cant use another php file.
http://php.net/manual/en/function.simplexml-load-file.php
So you need to get the filename as a string first by using a variable. You should be able to copy the code in your test.php file, then save the filename instead of echoing it out. Then you use that variable when loading the xml file.
e.g.
function get_latest_filename()
{
//contents of your test.php file should set this variable
$latest_filename = 'the_latest_file.xml';
return $latest_filename;
}
$latest = get_latest_filename();
$xml = simplexml_load_file($latest);
here the finish solution that worked for me
i protected the directory with .htaccess and inside i store all my generated xml files and also the getLastXml.php file!
the getLastXml.php
function get_last_file() {
$lastFileTime = 0;
foreach (glob("*.xml") as $filename) {
if ($lastFileTime<filemtime($filename))
{
$lastFileTime = filemtime($filename);
$lastFileName = $filename;
}
}
return $lastFileName;
}
$lastXmlFile = get_last_file();
header ("Content-Type:text/xml");
echo file_get_contents($lastXmlFile);
the functions get_last_file() returns the name of the latest created xml file and
header ("Content-Type:text/xml");
displays xml in the php file
echo file_get_contents($lastXmlFile);
loads the content of the xml file and display it
simplexml_load_file("http://username:passwort#urlToTheDirectory/getLastXml.php");
loads the xml data with

Categories