I'm trying to make my first gallery with PHP and I'm having some problems i can't solve. My code looks like this:

<?php

include_once('config.php');


//Script der uploader og omdøber billeder
if(isset($_FILES['billede'])){

    $errors = array();
    $allowed_ext = array('jpg','jpeg','gif','png', 'bmp');

    $file_navn = $_FILES['billede']['name'];
    $file_ext = strtolower(end(explode('.', $file_navn)));
    $file_tmp = $_FILES['billede']['tmp_name'];
    $file_size = $_FILES['billede']['size'];
    $kategori = $_POST['kategori'];
    $beskrivelse = $_POST['beskrivelse'];

    if(!empty($_POST['navn'])){
        $fil_nyt_navn = $_POST['navn'].'.'.$file_ext;
        $fil_nyt_navn = str_replace(' ', '_', $fil_nyt_navn);       
    }else{
        $errors[] = 'Du har ikke udfyldt navnet på billedet.';
    }

    if(in_array($file_ext, $allowed_ext) === false){
        $errors[] = 'Den fil type er ikke understøttet';
    }

    if($file_size > 10485760){
        $errors[] = 'Filen er større end 10mb og kan ikke uploades.';
    }

    if(empty($errors)){
        if(move_uploaded_file($file_tmp, 'billeder/'.$fil_nyt_navn)){

            include_once ("img_resize.php");
            $original_fil = 'billeder/'.$fil_nyt_navn;
            $thumbnail = 'billeder/'.$_POST['navn'].'_thumb.'.$file_ext;
            $maks_width = 150;
            $maks_height = 99;
            ak_img_resize($original_fil, $thumbnail, $maks_width, $maks_height, $file_ext);

            echo 'Filen er uploadet.';
        }
    }else{
        foreach($errors as $error){
            echo $error. '<br />';
        }
    }

    $url_org = 'billeder/'.$fil_nyt_navn;
    $url_thumb = 'billeder/'.$_POST['navn'].'_thumb.'.$file_ext;

    mysql_query("INSERT INTO galleri_billeder VALUES ('','$fil_nyt_navn','$beskrivelse','$kategori','$url_org','$url_thumb')") or die(mysql_error());   
}

//valg af liste
$title = mysql_query("SELECT * FROM galleri_kategorier") or die (mysql_error());

while($row = mysql_fetch_assoc($title))
{
 $arr[] = $row['navn'];
}

$prove = count($arr);

?>

<form action="" method="POST" enctype="multipart/form-data">

    <p><input type="file" name="billede" /></p>    
    <p><select name='kategori'>
    <?php
    for ($i=0; $i<$prove; $i++)
        {
            echo "<option value='{$arr[$i]}'>{$arr[$i]}</option>";
        }
    ?>
    </select></p> 
    <p>Navn<br /><input type="text" name ="navn" /></p>
    <p>Beskrivelse<br /><textarea name="beskrivelse" cols="30" rows="10"></textarea></p>
    <p><input type="submit" value="Upload" /></p>
</form>

The problem is when the user uses special characters like æ ø å in the name. the name can't have special characters because it's used to make the url. I tried using str_replace() but it won't work and i dont know why.
The second problem is that if the user uses special characters in the textarea it's not saved correctly in the database.

Try urlencode.

That's not what I'm looking for. And if it is i can't see how i can use it.

Eg.
If i name my image æøå the url would be /folder/æøå.jpg and then the browser can't read the url.

Sounds more like you have a characterset problem. If everything along the line is using Unicode, including the database.

I don't have a lot of experience with this sort of issue, but I would think that current databases and browsers should have no problem with this.

I'd try echoing things to the screen throughout the flow or your application to see where it goes off the rails.

I would strongly suspect that your second problem is a database configuration issue. I would expect characters like that to be stored without problem.

Hope that helps get you pointed in the right direction.

I just checked my charset and everything is utf8. It's wierd but thanks for the pointers!

Member Avatar for diafol

urls souldn't contain special chars - urlencode is the way to go, if you really have to do this.
Your textbox should work if everything is encoded to utf-8/unicode.

Place this at the head of your file:

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

I assume you already have this:

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

Your DB is utf-8/unicode charset/collated

You may need to set various options manually - but this is surely overkill:

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_unicode_ci');

For some reason it works now. I didn't change any code or charset but thanks for the code now I know what to check if something like this goes wrong again.

Member Avatar for diafol

OK, is it solved?

Yeah, thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.