We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP str_replace and MySQL problem

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.

4
Contributors
8
Replies
6 Hours
Discussion Span
7 Months Ago
Last Updated
9
Views
Question
Answered
vestervang
Newbie Poster
5 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try urlencode.

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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.

vestervang
Newbie Poster
5 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

scaiferw
Junior Poster
115 posts since May 2010
Reputation Points: 25
Solved Threads: 7
Skill Endorsements: 0

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

vestervang
Newbie Poster
5 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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');
diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,512
Skill Endorsements: 57

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.

vestervang
Newbie Poster
5 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

OK, is it solved?

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,512
Skill Endorsements: 57
Question Answered as of 7 Months Ago by diafol, pritaeas and scaiferw

Yeah, thanks for the help

vestervang
Newbie Poster
5 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0855 seconds using 2.78MB