用PHP中的mkdir()创建中文文件夹?

时间:2015-06-12 10:43:19

标签: javascript php jquery html

我有一个PHP,我用它来创建使用PHP的文件夹和子文件夹。我想从我的中文数据库中获取数据并用中文创建文件夹。以下是我的代码:

<?php 
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "dtable";

  //session_start();
  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  mysqli_set_charset($conn,"utf8");
  // Check connection
  if (!$conn) {
      die("Connection failed: " . mysql_connect_error());
  }

  $upload = 'E://demo';

  $str = '';
  $str1 = '';
  if(isset($_POST['userid'],$_POST['pid']))
{
    $userid = trim($_POST["userid"]);
    $pid = trim($_POST["pid"]);

    $sql = "SELECT * FROM demo WHERE username = '$userid' and password = '$pid'";
    $result = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($result);

$target = $upload.'/'.$row['week'].'/'.$row['day1'].'/'.$row['client'].'/'.$row['brand'].'/'.$row['sn'].'/';
if(!file_exists($target))
     {
       mkdir($target,0777,true);
     }
?>   

我的数据库中的数据是中文的。当数据是英文时,这完全正常,但一旦我将其更改为中文,它就开始给我错误。

1 个答案:

答案 0 :(得分:0)

使用iconvmb_convert_encoding更改字符串编码。

enter image description here

$dir_name = "測試/測試2";
$dir_name = iconv("UTF-8", "big5", $dir_name);
// $dir_name = mb_convert_encoding($dir_name, "big5", "UTF-8");
$path = "clients/$dir_name";    
mkdir($path, 0777, true);