Renaming an xml doc filename [duplicate] - php

This question already has answers here:
PHP rename file on same host
(3 answers)
Closed 8 years ago.
I want to rename a file in php but it does not change the filename with what I have tried below.
$xmlDoc->formatOutput = true;
$incident = $xmlDoc->createElement("Incident");
$root->appendChild($incident);
blah blah......
$tmp = split(" ", $entryTime);
$dateString = $tmp[0] . "T" . $tmp[1];
$entryTimeNode = $xmlDoc->createElement("EntryTime", $dateString);
.........
$xmlDoc->formatOutput = true;
$xmlDoc->save($xmlFullFilename);
$xmlDoc->rename("$xmlFullFilename","$entryTime_$xmlFullFilename");

The DOMDocument object does not have a rename method; you should use PHP's standard file rename:
$xmlDoc->save($xmlFullFilename);
rename("$xmlFullFilename","$entryTime_$xmlFullFilename");

Instead of $xmlDoc->rename() try php native rename function.
A sample.
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
This should work.
In your code,
rename($xmlFullFilename,$entryTime_$xmlFullFilename);

DOMDocument doesn't have rename method. Use PHP function rename for that.

Related

How to rename extension of file in directory with PHP? [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
I have a file in a directory
name.processing and I want rename this file in name.processed
I have this code:
$fn1 = str_replace(".processed", ".processing", $fn);
rename($fn1,$fn);
$fn contain the complete path.
Why the file did not got rename in directory?
I think this is not a duplicate because i know how replace a part of string but i don't know how replace an extension of file in directory
PHP is not always predictable in parameter sequence, you should consult the documentation.
// processing -> processed
// str_replace($search, $replace, $haystack)
$fn_new = str_replace(".processing", ".processed", $fn);
// rename($oldname, $newname)
$rename = rename($fn,$fn_new);
echo "old: $fn, fn_new: $fn_new, rename: ", $rename ? 'success' : 'failure';
http://php.net/manual/en/function.str-replace.php
http://php.net/manual/en/function.rename.php
Edit:
If it still doesn't work: https://stackoverflow.com/search?q=php+rename
Looks like you have the paramters for str_replace backwards.http://php.net/manual/en/function.str-replace.php
Try $fn1 = str_replace(".processing", ".processed", $fn);

How to slice an input value? [duplicate]

This question already has answers here:
How do I get a file name from a full path with PHP?
(14 answers)
Closed 8 years ago.
My input value is just like this: Oppa/upload/default.jpeg
I want to slice the value of an input according by / cause i want to get the image file name. Does anyone know some tricks to do this?
example: i want to get default.png
<input type="text" value="Oppa/upload/default.png" id="fileLink" name="fileLink" />
Use basename():
$path = "Oppa/upload/default.jpeg";
echo basename($path); //will output "default.jpeg"
echo basename($path, '.jpeg'); //will output "default"
The first parameter is the path of which the trailing component will be removed. If the first parameter ends in the optional second parameter, the second parameter will also be cut off.
On Windows, both slash (/) and backslash (\) are used as directory
separator character. In other environments, it is the forward slash
(/).
- PHP manual
You should use basename() PHP function.
This will work for you
$path = "Oppa/upload/default.jpeg";
echo basename($path);
Use pathinfo() php function
$path = "http://domain.tld/Oppa/upload/default.png";
$info = pathinfo ( $path, PATHINFO_BASENAME ); // returns default.png
Yet another solution, using preg_match()
<?php
$path = "http://domain.tld/Oppa/upload/default.png"; // or "C:\\domain.tld\Oppa\upload\default.jpg";
$pattern = '/[\/|\\\\]((?:.(?!\/|\\\\))+)$/';
if(preg_match($pattern, $path, $matches)){
echo $matches[1]; // default.png or default.jpg
}
?>
Note: People claim to have problems using basename() with asian characters
I am supposing that if your image path will not be change from Oppa/upload/ than this Should work using explode :
$str = "Oppa/upload/default.jpeg";
$s= explode("Oppa/upload/",$str);
echo $s[1];
Another Best thing you can do with defining the relative path as a constant, so :
const path = "Oppa/upload/";
$str = "Oppa/upload/default.jpeg";
$s= explode(path,$str);
echo $s[1];
will also work.

Is there an antipode of the PHP file() function? [duplicate]

This question already has answers here:
Writing a string array to text file separated by new line character
(3 answers)
Closed 9 years ago.
file() reads a file into an array reading each line of a file as an element of an array. Is there exactly the same function to write an array into a file?
It doesn't exist.
But you can use file_put_contents and implode:
$file = '/tmp/test.txt';
$data = array('foo', 'bar');
file_put_contents($file, implode(PHP_EOL, $data));
Also you can also specify the $data parameter as a single dimension array:
file_put_contents($file, $data);
This is equivalent to:
file_put_contents($file, implode('', $data));
Is there exactly the same function to write an array into a file?
Nope I don't think so.. However you can do something like ..
<?php
file_put_contents('yourfile.txt',implode(PHP_EOL,$yourarray));

Convert string in php [duplicate]

This question already has an answer here:
How to decode something beginning with "\u" with PHP
(1 answer)
Closed 8 years ago.
I have one string:
"Hello\u00c2\u00a0World"
I would like convert in:
"Hello World"
I try :
str_replace("\u00c2\u00a0"," ","Hello\u00c2\u00a0World");
or
str_replace("\\u00c2\\u00a0"," ","Hello\u00c2\u00a0World");
but not work!
Resolve!
str_replace(chr(194).chr(160)," ","Hello\u00c2\u00a0World");
If you would like to remove \u.... like patterns then you can use this for example:
$string = preg_replace('/(\\\u....)+/',' ',$input);
You are most of the way there.
$stuff = "Hello\u00c2\u00a0World";
$newstuff = str_replace("\u00c2\u00a0"," ",$stuff);
you need to put the return from str_replace into a variable in order to do something with it later.
This should work
$str="Hello\u00c2\u00a0World";
echo str_replace("\u00c2\u00a0"," ",$str);
You may try this, taken from this answer
function replaceUnicode($str) {
return preg_replace_callback("/\\\\u00([0-9a-f]{2})/", function($m){ return chr(hexdec($m[1])); }, $str);
}
echo replaceUnicode("Hello\u00c2\u00a0World");

Get extension from filename like variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to extract a file extension in PHP?
I have a variable $filename="filename.ext" or $filename="filena.m.e.ext" or so on.. How can i extract the extension (here ext) from the variable / string? The variable may change or may have more than one dots.. In that case, i want to get the part after the last dot..
see the answer :
$ext = pathinfo($filename, PATHINFO_EXTENSION);
You can use the path info interrogation.
$info = pathinfo($file);
where
$info['extension']
contains the extension
you could define a function like this:
function get_file_extension($filename)
{
/*
* "." for extension should be available and not be the first character
* so position should not be false or 0.
*/
$lastDotPos = strrpos($fileName, '.');
if ( !$lastDotPos ) return false;
return substr($fileName, $lastDotPos+1);
}
or you could use the Spl_FileInfo object built into PHP
You want to use a regular expression:
preg_match('/\.([^\.]+)$/', $filename);
You can test it out here to see if it gets you the result you want given your input.
There are many ways to do this, ie with explode() or with a preg_match and others.
But the way I do this is with pathinfo:
$path_info = pathinfo($filename);
echo $path_info['extension'], "\n";
You could explode the string using ., then take the last array item:
$filename = "file.m.e.ext";
$filenameitems = explode(".", $filename);
echo $filenameitems[count($filenameitems) - 1]; // .ext
// or echo $filenameitem[-1];

Categories