I'm trying to load a xml file from an external ftp server. Sadly the filename contains a space between two words.
Homepage-Filename Statistics-170210.xml
I'm able to load the file with simplexml_load_file, if there is an underscore a dash. For example:
Homepage-Filename_Statistics-170210.xml
simplexml_load_file('ftp://username:password#ftp.domain.com/Homepage_Filename_Statistics-170210.xml');
But I'm not able to change the file name, so I have to load the file with spacing inside.
I have tried to replace the space with %20 or backslash / , but it isn't working either. For example:
Homepage_Filename%20Statistics-170210.xml
or
Homepage_Filename/ Statistics-170210.xml
Someone has an idea, how load something like this?
Thanks!
I can also reproduce this problem with the simplexml_load_file.
Interestingly the file_get_contents works:
$url = 'ftp://username:password#ftp.domain.com/Homepage_Filename/ Statistics-170210.xml';
simplexml_load_string(file_get_contents($url));
Related
I am opening a file on the server with PHP. The file seems ordinary. It opens in Notepad and Textedit on a PC. Even PHP can display it without any issue in a web browser when we echo out.
But when I try searching it with strpos() it can’t find anything except single characters. if i search for a string with 2 or more characters, it doesn’t find anything.
I have tried encoding it to UTF-8, and it detects it as ASCII. so everything seems right there.
I have also isolated the part of the file that I am trying to read down to only 250 characters. They all look fine on the screen.
But strpos can’t find it. I’ve run tests on every part of my code and I believe everything is fine with my code. The problem I believe derives from that the characters I see on the screen are not exactly matching what those characters really are.
My last resort is to write a function which converts each character into an integer array (if that’s even possible), and then convert all that back to a string. This way, we’ll know 100% that the characters we see are real.
Hoping that somebody has a better approach or perhaps an idea for something I missed?
I'll post the code below:
$content = file_get_contents($file->getPathname()); // get the file contents
$content = substr($content, 30, 300); // reduce the large file to just the first few lines
$content = htmlspecialchars($content); // try to remove any special characters from the file
$content = iconv('ASCII', 'UTF-8//IGNORE', $content); // encode to a friendly format
$string = "JobName"; // this is the string i'm searching for
if (strpos($content, $string) !== false) {
echo "bingo";
}
else {
echo " not found ";
}
Just to be clear, the file I'm opening is generated from a PC program that stores its data in .DAT format. Like I said, I can see and read the content very easily using any program, including PHP. but when I try to search, its as if it doesn't recognize the content at all.
I am not aware of how to upload a file on StackOverflow, but if someone can tell me how to do it then I will gladly post the file itself.
Thank you very much for your help ARKASCHA. I was able to find an online HexEditor and when I saw the characters, it seems there is a NUL character between every single character in this file. that's probably why I couldn't see it with a regular view. I just had to run an additional function to remove NUL characters from the file, and then it works as its supposed. Thanks again.
I wrote a webservice which allows (after authentication of course) to transfer file contents via post, which gets written into a file. Everything works fine, unless the last char(s) are a tab or a tab followed by a linebreak, which both get stripped. This causes serious problems with csv files, separated by a tab.
A I don't know the file contents in advance, I cannot just manually add a tab at the end.
Multipart/form-data for a file upload is not possible, as the other client doesn't support it.
fopen + fwritwe produce exactly the same issue.. Any ways around this?
use the trim() function to trim the leading and tailing spaces. For tail space use rtrim(), you also can specify the char you want to drop.
file_put_contents($filepath, rtrim("with tab\t", "\t"));
I am running PHP locally (no webserver involved at all). I am having trouble accessing a file with spaces in the path.
My bare bones case is
$indexFile = "file:///Users/username/Documents/My Folder/test.txt";
echo file_exists($indexFile);
or:
$indexFile = "file:///Users/username/Documents/My‰20Folder/test.txt";
echo file_exists($indexFile);
AFAICT this latter case is a well-formed file scheme URI. It's exactly what appears in the browser location field if I drag the file in there.
URI's without spaces don't have any problem. Unfortunately I am not at liberty to change "My Folder" to "MyFolder", and besides, I want to find a solution.
I tried using urlencode, rawurlencode, escapeshellarg, I've tried replacing %20 with a backslash-escaped space "My\ Folder" but none of this works. I've also hunted through google and stackoverflow, but while there are many suggstions, the question remains unanswered:
How to access an arbitrary file (on the local host) using a path which contains spaces?
This looks like a Mac OSX file system and so this should work:
$indexFile = "/Users/username/Documents/My Folder/test.txt"
echo file_exists("${indexFile}");
Remove the first file:// from the first code snippet. It'll work.
$indexFile = "file:///Users/username/Documents/My Folder/test.txt";
echo file_exists(str_replace("file://", "", $indexFile));
My server has been hit with a nasty javascript iframe virus. The Trojan injects itself in to every index.php, index.html, & login.php files. The virus looks like <script>VirusCodeCrap</script>
Is there anyway I could use PHP's str_replace function to search my server and delete the virus? Would anyone know wher I could find some examples on how to do this?
Thanks,
Albert
If you are on a Unix server, sed is the best way to find and replace text in files.
If you must use PHP, the algorithm will be:
Read a file into a variable using file_get_contents()
$file_contents = file_get_contents( $filename );
Search for the replace the offending string
$file_contents = str_replace( $the_offending_text, $the_replacement_text, $file_contents);
Write $file_contents back to the file using file_put_contents:
file_put_contents( $file_contents );
str_replace() may be insufficient if the string is not precisely the same in every case. If there are variations in the offending string, you may need to use a regular expression to locate and remove them.
I would immediately delete all files from your server and re-upload clean copies from wherever your code repository is. I would also find where the code was exploited and patch that security hole.
Going about it by editing the virus out of your files seems like the long and hard way when you should have a backup of your files ( that haven't been on a live server ).
I use a script to get an image from another server and store it in the db, the problem is that when the url has a space in it, the function grabs nothing.
I tried to encode the url and to simply replace all spaces with %20 but with no success.
I'm running out of options, if any of you could give me some ideas would be great!
Thanks!
$thumb=imagecreatefromjpeg(http://www.dummysite.ca/imageone.jpg); //->WORKS
$thumb=imagecreatefromjpeg(http://www.dummysite.ca/image one.jpg); //->DOESN'T WORK
EDIT: more info: I'm running a CentOS machine, php 5.2.17
EDIT: found the answer, replacing spaces with %20 actually WORKS but I was foolish and only replace it before the imagecreatefromjpeg call, it turns out getimagesize needs it as well
So for those who will have a similar problem
replacing spaces with %20 actually WORKS but I was foolish and only replace it before the imagecreatefromjpeg call, it turns out getimagesize needs it as well
I would do everything in my power to keep spaces out of filenames. At whatever point the file enters your server it should be renamed to something with underscores. Personally For file uploads I rename every file to a combination of timestamp and the uploader's ip address. Grabbing from another server could use the same logic. If you need to save the original filename just save it as a text string associated with the DB entry.