Special characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Special characters

Post by Mirux »

Well, first of all I am working on a project and this site is in spanish so I need to use these characters: áéíóúñ.

So my problem is: I have an 'uploading file' section where I upload files with this format: Author - Title of the file.doc.

I use explode(" - ", $string) to split the file name into pieces and save it in a db. That's working fine so far but when I upload files and its name contains any special characters, the file doesn't get saved with the original name+special characters in the server. It does in the db. (Eew, I hope you guys understand this part).

I'll paste my file.php class here:

Code: Select all

<?php
/* file.php */
Class File
{
	/* MEMBER DATA */
	private $db;
	/* CONSTRUCTOR */
	public function File($db)
	{
		$this->db= $db;
	}
	/* BEHAVIORS */
	public function UploadFile()
	{
		if (isset($_POST['upload']))
		{
			$extension= explode(".", $_FILES['file']['name']);
			if ($extension[1] != "doc")
			{
				die ("La extension del archivo no es valida.");
			}
			else 
			{
				$path= "files/";
				$path= $path. basename($_FILES['file']['name']);
				if (move_uploaded_file($_FILES['file']['tmp_name'],$path))
				{
					$author= explode(" - ",$_FILES['file']['name']);
					$title= explode(".",$author[1]);
					$title2= explode(".",$title);
					$date= time();
					$sql= "INSERT INTO archivos (author, title, type, date) VALUES (%s, %s, %s, %d)";
					$vars= array($author[0],$title[0],$_POST['type'],$date);
					$result= $this->db->query($sql, $vars) or die ("ERROR");
					die ("El archivo ha sido guardado exitosamente.");
				}
				else 
				{
					die ("Ha habido un error al subir el archivo. Intente mas tarde.");
				}
			}
		}
	}
}
?>
So what I want is, save the files in the server with the original name+special characters. And save it in db with special characters too. And when I want to display this information, display them with special charactes too lol :)

I know most of you won't understand this because you're main language is english. But any help is welcomed. :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Filesystem support for UTF-8 filenames is spotty, so I am not surprised. Instead of directly copying over the file to the filename they specified, I would recommend urlencode() 'ing it before-hand. So, assuming that you're using UTF-8, instead of having a file that was: áéíóúñ.doc, it would be %C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1.doc, which looks ugly but will work. To retrieve the file, take the name and urlencode, to determine the real name of the file, urldecode() it.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

$sql= "INSERT INTO archivos (author, title, type, date) VALUES (%s, %s, %s, %d)";
You need quotes around those %s on that query you also need to SQL escape them.

The browser will usually send in the encoding data is sent to it so make sure you have

Code: Select all

<meta http-equiv="Content-Type" value="text/html; charset=utf-8" />
in your <head> and

Code: Select all

accept-encoding="utf-8"
in your form and you are setting the correct encoding header which you can do using either one of these:

Code: Select all

header('Content-Type: text/html; charset=utf-8');

Code: Select all

ini_set('default_charset', 'utf-8');
these are essential reading for understanding UTF-8 in PHP.
Mirux
Forum Commoner
Posts: 29
Joined: Sun May 13, 2007 7:11 pm

Post by Mirux »

Thanks ole.

The Query line. It's ok without the ' ' and I already escaped them. ;)

I'll try that but where should I put the:

Code: Select all

header('Content-Type: text/html; charset=utf-8');
ini_set('default_charset', 'utf-8');
After <head></head> :S Before any echo/output.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

The <meta> tags go inside <head>. The header() call comes before any HTML output.
Post Reply