Page 1 of 1

Special characters

Posted: Sat Jun 09, 2007 1:16 pm
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. :)

Posted: Sat Jun 09, 2007 4:08 pm
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.

Posted: Sat Jun 09, 2007 6:51 pm
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.

Posted: Sat Jun 09, 2007 8:26 pm
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.

Posted: Sat Jun 09, 2007 8:32 pm
by Ambush Commander
The <meta> tags go inside <head>. The header() call comes before any HTML output.