I have a problem with my images not being displayed when they have a # or % symbol.
I am using PHP to read a directory and display all images but any with those symbols just have broken links. The images are uploaded to the server fine but wont display.
I think you'll need to write a function which replaces the % and # characters with their corresponding url-encoding symbols, you can find a reference here:
http://www.w3schools.com/tags/ref_urlencode.asp
you should compare the output of your script to the directory list you get for that dir in your browser, then you will obviously see the correct mapping for your special chars.
are you uploading images via php? then you could maybe map special chars to spaces or dashes. don't think having special chars in file names is a good idea
Best will be to strip out all those characters before uploading the images. (If you are using FTP, rename your files. If you are using PHP to upload, write a function which does it)
Otherwise you will need to escape the symbols: Take a look at urlencode
Related
I have a directory that contains several pdfs, I have a script that sets the path for access and everything else,
Baixar
however I am having a 404 error in files that contain accents of type (comma and percent),
File with accent:
http://exodocientifica.com.br//_fispq/ALARANJADO%20DE%20METILA%200,2%.pdf
other files without accent are normally accessed,
File without accent:
http://exodocientifica.com.br//_fispq/ALARANJADO%20DE%20METILA.pdf
already I tried using functions like urlencode, htmlentities, hand did not get results
You should encode % with %25 since it is a special character in URL. Try using this URL instead:
http://exodocientifica.com.br//_fispq/ALARANJADO%20DE%20METILA%200,2%25.pdf
On my CDN, I have some old files with names that were created using rawurlencode function. E.g. one of the files has this name:
Cat%20presentation.pdf
Now, when I try to read this file, I get a "File not found" error:
GET cdn.example.com/documents/Cat%20presentation.pdf
I believe that's because of the encoded space character in the name - %20. For the browser (and the CDN) what I'm asking for is this:
Cat presentation.pdf
while the "%20" part is actually in the filename.
Is there any way I can get around this and access the file?
You need to URL-encode the "%" character into "%25":
GET cdn.example.com/documents/Cat%2520presentation.pdf
But if I were you I'd just fix their filenames instead.
There is an upload box where users upload the image of their document ids. The thing is that whoever implemented this upload box, did not program it to encode the filename if there is special characters in it. So now there are a bunch of uploaded files in the server with special characters such as
14592-Carte d'identité-072616143337.pdf
So when they tried to visualize it in the browser, the link they have is:
See File
Which is not working. I tried to use urlencode() to encode the filename but it is still not working.
How can I display those files with special characters?
Do I have to change the filenames first of all those files uploaded with special characters, so for example:
14592-Carte d'identité-072616143337.pdf
Will be changed to:
14592-Carte-didentite-072616143337.pdf
In order to be able to display in the browser? What function do I use?
Also, What function should I use to change the names of the filenames in the original upload box so it does not upload files with names with special characters anymore?
You can use preg_replace() function:
preg_replace('/[^\da-z]/i', '-', $string);
This will replace all non-alphanumeric characters to "-"
And you could use this function to rename uploaded files directly after upload
I have the following www.mywebsite.com/upload/server/php/files/foto/test/Aston_Martin_DBS_V12_coupé_(rear)_b-w.jpg
This file is uploaded trough a script. The file exists on the server.
However, because the special character in the url (é), I am experiencing some problems.
The filename on the server is Aston_Martin_DBS_V12_coup%C3%A9_(rear)_b-w.jpg, which is correct. However somehow my browser (Chrome) requests this page as ISO-8859-1 instead of UTF-8.
Therefore, I get a 404.
I am using jQuery file upload plugin.
I deleted my answer from here and i wrote new:
Usually websites does not contain files with non-standard characters. Files usually have removed non standard characters, sometimes that characters are replaced by similar standard chars (Polish ą to a, ś to s). For example - im renaming files manually, or when i have a lot of files - i just use bash or php script that removes/replaces that characters in filenames on server.
Anyway, if you HAVE TO use original filenames - you have to decode them from ISO and encode them to UTF8.
Take look at that php code fragment here:
how to serve HTTP files with special characters
Some special Charater make problem in url for filename
like
+ ,#,%,&
For those file which are accessing through url make file which not contain above letters
forex
str_replace(array(" ","&","'","+","#","%"),"-","filename")
it will works fine
If the filename contains the % character codes, you will need to encode those in your URL. Try accessing Aston_Martin_DBS_V12_coup%25C3%25E9_(rear)_b-w.jpg
I have a folder on my website just for random files. I used php opendir to list all the files so i can style the page a bit. But the files that I uploaded with special characters in them don't work. when i click on them it says the files are not found. but when i check the directory, the files are there. seems like the links are wrong. any idea how i can get a correct link to these file names with special characters in them?
This is tricky. It depends what encoding your filesystem uses for filenames and how (if) your webserver or PHP functions convert the encoding.
First of all, make sure your links never use unencoded non-ASCII characters. URLs should be in UTF-8, i.e. é should be encoded as %C3%A9. If that doesn't work, try %E9 (é in ISO-8859-1).
You might find iconv() function useful to convert encodings. rawurlencode() is obligatory.
do you see them if you run this?
foreach (new DirectoryIterator('/path/to/folder') as $fileInfo) {
if($fileInfo->isDot() || $fileInfo->isDir()) continue;
echo $fileInfo->getFilename() . "<br>\n";
}
EDIT: just realised i misread the question. Its likely some kind of encoding issue like porneL says