Special characters in file name - download vs delete - php

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

Related

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

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

How to remove Junk characters coming in gmail attachments in php?

I have marked the junk characters in the image and I want the code to remove it and start reading the data after it.
That ugly looking text is not junk but something that makes a *.doc file a DOC file that it is (i.e. formatting). You can't really just echo that file using PHP.
You can display it using a some PHP doc viewer library though or if you can find some API online to convert DOC to TXT.
You can also make the user download it. Use file_put_content() to store that attachment into a doc file like below :
if(file_put_content("attachment.doc", $email['attachment'])){
header("Location: attachment.doc");
}
The binary data represents a *.doc file. If you really want to extract plain text from it, you could do some fuzzy logic, and extract the lines that do not contain any characters with low ASCII codes (except for CR and LF).
Assuming your data structure is in $data, you could do this:
foreach($data as $element) {
$element["attachment"] = preg_replace(
"/^.*?[\x01-\x09,\x0B,\x0C,\x0E-\x1F].*?$\R?/m",
"", $element["attachment"]);
}
Again, this is just "fuzzy" logic, so you still might get some meaningless text that is not removed.

using PHP include and symbol outputs

I'm building a website for a friend using Wordpress. There is one particular element he is going to need to update and he's not used to code so I have created a file that he can edit without getting involved in wordpress.
In wordpress I call this file using PHP include which works wonderfully until there are any £ symbols in the file. At this point the £ symbol gets replaced with a ? in a square thats been rotated 45 degrees.
I have looked on this site and found this, the problem being, that doesn't help me.
I have tried replacing the £ symbols for £ & £ both of which give the same output.
Anyone got any ideas?
EDIT: As requested;
<p class="menu_item">Americano - £2.00<br><span id="item_description">A double espresso stretched</span></p>
there are various lines like the one above in the file being called. I then call the file in wordpress using <?php include './wp-content/menu_content.txt'?> - I have tried calling it as a txt file, php and html.
I think this is not a problem with wordpress but rather with web server that tries to serve this site in some other encoding. Can you provide response/request headers and a raw html page heading?

Escaping white space : %20 or + - smarty

I am using smarty as a template engine. I have to escape an image file path {$filepath|urlencode}, the problem is that the white space are converted into a '+', which prevent the image to be reached on the server : %20 would work, how to escape correctly my path ?
Edit : more precisely, I use the facebook share link
I use a facebook share as so and it doesn't display the image when shared :
``
The final code looks like for my specific usage :
<a href="http://www.facebook.com/dialog/feed?app_id=...&link=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18&picture=http%3A%2F%2Fmysite.org%2Ffiles%2Fcampaign%2Fimage%2Foriginals%2F18%2FSans+titre-3.jpg&name=Some text "Text d'Text", Text&description=Rejoignez%20la%20campagne%21&redirect_uri=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18"onclick="window.open(this.href);return false;">
on the same site, all the facebook share link works perfectly and the image displays well ! Reason why I thought it was the link of that specific image that is not working
escape is what you're searching for. Take a look at:
http://www.smarty.net/docsv2/en/language.modifier.escape.tpl
{$filepath|escape:"url"}
urlencode is used to encode (not escape!) a string to be used as a query part inside an URL passed as GET var: http://php.net/manual/en/function.urlencode.php
URL encoded space is either a plus sign or %20. They are equivalent, and are both interpreted as a space on the server.
If you see either in the URL, then the server will see a space.
You say that the plus sign is preventing the image from being loaded. This sounds like a deeper problem than simply using the wrong encoding. Possibly it's being double-encoded?
What is the actual URL being requested in the browser? Open the dev tools/Firebug, and look at the requests to find out. If the URL includes %2B then the plus sign is being double-encoded. This is the problem you need to solve.
The other solution, of course, is not to use spaces in filenames on the web. The only reason one would want spaces in filenames is for readability, but since the web requires spaces to be urlencoded, it removes that readability anyway. Take away the spaces, and the problem will go away by itself.

Using PHP to search a text file

I'm trying to create a code to download mp3 files embedded in a page. It starts out as a submit form. You input the URL and submit it, and it writes the HTML source of that page to a text file. I also set the script to search the source to see if there is an audio file embedded. I suppose I should include that it's not in the format of filename.mp3. The format is:
embed type="application/x-shockwave-flash" src="http://diaryofthedead.tumblr.com/swf/audio_player_black.swf?audio_file=http://www.tumblr.com/audio_file/1435664895/tumblr_lb2ybulZkt1qb5hrc&color=FFFFFF" height="27" width="207" quality="best"
So here's the thing, there's just a certain string you have to add to the end of the file, for it to redirect to the mp3 file. I know the string. What I want to do is extract, for example "http://www.tumblr.com/audio_file/1435664895/tumblr_lb3ybulZkt1q5hrc" from the middle of this. I know how to read from files but I have no idea how to extract certain parts from it without knowing the exact filename already. So is there any way I can have it search the source for "audio_file" and if it finds the string, extract the audio file?
If your program is just a parser for extracting MP3 files embedded in a webpage you don't even need to save the contents of the webpage onto a file, you can work with the page source inside just your server's memory.
If you want to detect paths to MP3 inside flashes, provided you know how does it match a regular expression, you are done.
If you don't know much about rgular expressions, you should look at them.
If you don't want as much power as a regular expression can give to you, you can always find strings by position, like:
$pos = strpos($haystack, $needle);
Beware: strpos() will find the first (strrpos will find the last) occurrence of a string. So you need to make it as explicit as you can, or you might end up capturing something unwanted.
Take a look at http://www.regular-expressions.info/quickstart.html or something similar.
I can't post more links because I don't have enough reputation yet
You can try using preg_match (http://php.net/manual/en/function.preg-match.php) to get the contents between "audio_file=" and "&".
Or you can also use a string between function to get the contents between those two strings:
http://www.php.net/manual/en/function.substr.php#89493

Categories