Saving SimpleXMLElement as an xml file node attachment in Drupal 7 - php

I have my DOMDocument saved as $xml.
I have the node loaded, I also have a custom field i've made which is called 'field_xml_file'.
But I can't quite seem to get how to save the file and insert it into the node.
I currently have this:
$fileName = 'file.xml';
$file = file_save_data($xml, 'public://', $fileName);
$newRevision->field_xml_file[LANGUAGE_NONE][] = (array)$file;
node_save($node);
Any help?

$fileName is not supposed to be third parameter. Change that line to:
$file = file_save_data($xml, 'public://'.$fileName);
second parameter is full file name, using drupal stream wrappers.
also, if this is literally code you are using, you need to change $newRevision to $node in order to save changes

Related

Saving image file name exactly as URL image file into folder in PHP

I have visited this article previously and found it useful, but i would like to add more functionality to it by having it save an image file name according to the URL name.
This is what I've done so far and it works.
$contents=file_get_contents('http://www.domain.com/logo.png');
$save_path="C:/xampp/htdocs/proj1/download/[logo.png]";
file_put_contents($save_path,$contents);
Basically, where I have put square brackets around I want to have that dynamic based on the URL file name. For example, if i have an image url such as this: https://cf.dropboxstatic.com/static/images/brand/glyph-vflK-Wlfk.png, I would like it to save the image into the directory with that exact image name which in this case is glyph-vflK-Wlfk.png.
Is this possible to do?
I would do this way
$url = "http://www.domain.com/logo.png";
$file = file_get_contents($url);
$path = "C:/xampp/htdocs/proj1/download/". basename($url);
return !file_exists($path) ? file_put_contents($path, $file) : false;
From what I understand, what you're trying to do is the following :
$url = 'http://www.domain.com/logo.png';
$contents=file_get_contents($url);
$posSlash = strrpos($url,'/')+1);
$fileName = substr($url,$posSlash);
$save_path="C:/xampp/htdocs/proj1/download/".$fileName;
file_put_contents($save_path,$contents);
There is a function for that, basename():
$url="http://www.domain.com/logo.png";
$contents=file_get_contents($url);
$save_path="C:/xampp/htdocs/proj1/download/".basename($url);
file_put_contents($save_path,$contents);
You might want to check if it already exists with file_exists().

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

Deleting XML node with PHP

I'm trying to simply save an XML file from the web locally (this part works fine) and then delete a node of the XML and resave it. However, the local xml file ends up blank when I do the following:
$xml = file_get_contents($xmlurl);
file_put_contents('187file.xml', $xml);
$rep187 = simplexml_load_file('187file.xml');
unset($rep187->ComparableSalesReport->ComparableSales->ComparableSale);
file_put_contents('187file.xml', $rep187);
file_put_contents does not accept an object as the second argument (it only accepts a string, an array or a stream resource).
You can pass a string instead by using SimpleXMLElement::asXML on your $rep187 document, like so:
$xml = file_get_contents($xmlurl);
file_put_contents('187file.xml', $xml);
$rep187 = simplexml_load_file('187file.xml');
unset($rep187->ComparableSalesReport->ComparableSales->ComparableSale);
file_put_contents('187file.xml', $rep187->asXML());

PHP: SimpleXML writing issue

I am using SimpleXML to write to my XML file on my Apache Server. Here is my PHP code:
<?php
$xmlFile = 'http://localhost/database.xml';
//$xml = new SimpleXMLElement($xmlFile, NULL, TRUE);
$xml = simplexml_load_file($xmlFile);
$xml->addChild("User", "TestUser2");
file_put_contents($xmlFile, $xml->asXML());
?>
My XML file code:
<Usernames>
<User>TestUser1</User>
</Usernames>
The problem I am having is that SimpleXML WILL NOT write to my XML file. I have tried many different methods ($xml->asXML($xmlFile), DOMDocument ... ->save) and none of them are working. I changed the permissions on my file and STILL I cannot write to it:
I have spent hours today trying to get this to work with no success. If anyone has any type of solution it would be great to hear.
When you write the contents to the file, you should pass a system filepath as the first variable, your $xmlFile variable is a URL. Change this to the local file name and it should save.
Based on your comments, the following should work
<?php
$xmlFile = 'http://localhost/database.xml';
$xml = simplexml_load_file($xmlFile);
$xml->addChild("User", "TestUser2");
file_put_contents('/Applications/MAMP/htdocs/DataBase/database.xml', $xml->asXML());
But, I would double check the $xmlFile URL - from what you have said, your local URL could be http://localhost/DataBase/database.xml - you should check that you can open your XML file in Safari using the $xmlFile URL.

Protection for reading files dynamically

What's the best way to protect against dynamic file access using URL variables? I'm concatenating two URL variables that will form the filename I want to access which will load XML.
$type = $_REQUEST['type']
//(ie. AB);
$timeframe = $_REQUEST['timeframe']
//(ie. 00.04);
//create XML document object model (DOM)
$main_doc = new DOMDocument();
$s = SITE_DIR."/data/file.".$type.".".$timeframe.".xml";
// example file.AB.00.04.xml)
// will be adding test to see if file exists
$main_doc->load($s);
You should check that the request string does not contain ".." and also doesn't contain "/" (or "\" if you're on windows) so that the path does not point to a directory other than one you are referencing.
Perhaps try this:
$timeframe = str_replace(array('..','/','\'),array('','',''),$timeframe);

Categories