PHP: file_put_contents() strips last tab (\t) - php

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"));

Related

Opening an encoded file with PHP

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.

Load file name with spaces from FTP

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));

Special characters in file name - download vs delete

I am trying to create a file management system on my website. Problem is with downloading files that contain special characters (other work correctly).
If I use file_exists($mypath) the result is true therefore file exists.
When deleting this file with unlink($mypath) it also works fine.
Only thing that doesn´t work is downloading the file.
The download is done via href link where I echo the path but it somehow converts the characters so the link doesn´t work. The solution is in some conversion but I had no success yet.
I suspect that php is converting the special characters into html entities.
You should use the 'rawurlencode' php method to keep the special characters.
The following link talks about you issue (special chars appearing in file name and wanting to create a link):
http://www.dxsdata.com/2015/03/html-php-mailto-content-link-with-special-characters/
Their solution shows the use of rawurlencode, the following was copied from above link just incase the link goes dead:
Snip start...
Scenario
On your website, you want to offer a link which opens a mail client like Outlook with mail and content suggestion. The content contains a link to a file with special characters in its name, which causes Outlook to break the link, e.g. if it contains spaces or german Umlaute.
Solution
Using PHP, write
<?php
$fullPath = $yourAbsoluteHttpPath . "/" . rawurlencode(rawurlencode($filename));
$mailto = "mailto:a#b.com?subject=File for you&body=Your file: ".$fullPath;
?>
Generate Mail
Note the double usage of “rawurlencode” function. The first one is needed for HTML output, the second one will be needed for the part when Outlook takes the link code into its mail window.
Snip end ;-)

Is there a way in PHP to strip whitespace from JSON without using ob_gzhandler?

I'm grabbing some JSON data using file_get_contents and I need to compress it so I can add it as a data attribute on an HTML element in my page. Basically I just need to strip out line breaks, extra spaces or tabs. Everybody seems to suggest using ob_gzhandler. But I can't do that - I don't have control over the modules that are enabled on our production environment. Can anybody suggest a PHP snippet that'll do what I want without ob_gzhandler?
If you want the data written as JSON you could do a simple:
echo json_encode(json_decode($data));
This will strip all whitespaces

PHP: imagecreatefromjpeg($url) doesn't work if $url contains spaces?

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.

Categories