PHP - Move_uploaded_file移动文件,符号无法理解

时间:2014-10-25 18:02:59

标签: php

我的move_uploaded_file功能存在问题。

问题是,如果我上传一个阿拉伯语文件,其中的符号无法理解。

看图片

image

3 个答案:

答案 0 :(得分:0)

对此不是100%肯定,但您是否尝试过从FILES数组接收字符串时对文件名进行编码?

http://php.net/manual/en/ref.url.php

答案 1 :(得分:0)

幸运的是,有些库可以解决此问题,例如ar-php

<?php
set_time_limit(0);
require("connect.php");
include 'Arabic.php';
$arabic = new I18N_Arabic('Transliteration');
if (isset($_FILES['upload_file'])) {
    foreach ($_FILES['upload_file']['tmp_name'] as $count => $upload_file) {
        if (!empty($_FILES['image']['error'][$count])) {
            // Some error occured with the file in index $count
            // Put some error handling in here...
            return false; // or you could continue;
        }

        if (!empty($upload_file) && is_uploaded_file($upload_file)) {
            $charset = "AHfgG15Ds10MZD58N";
            $code = '';
            $length = 15;
            for ($i = 0; $i <= $length; $i++) {
                $rand = rand() % strlen($charset);
                $tmp = substr($charset, $rand, 1);
                $code .= $tmp;
            }
            mysql_query("SET NAMES utf8");
            $query = mysql_query("SELECT `code` FROM `files`  WHERE `code`='$code'") or die(mysql_error());
            $numrows = mysql_num_rows($query);

            while ($numrows != 0) {
                $code = '';
                for ($i = 0; $i <= $length; $i++) {
                    $rand = rand() % strlen($charset);
                    $tmp = substr($charset, $rand, 1);
                    $code .= $tmp;
                }
                $query = mysql_query("SELECT `code` FROM `files`  WHERE `code`='$code'");
                $numrows = mysql_num_rows($query);
            }
            $types = array('jpg', 'jpeg', 'gif', 'png', 'zip', 'rar', 'mp3', 'm4a', 'wmv', 'divx', 'mp4',
                           'flv', 'FLV', 'dvd', 'mkv', 'bmp', 'xml', 'doc', 'pdf', 'xls', 'psd', 'svg',
                           '3gp', 'ppt', 'txt', 'exe', 'mpeg', 'dvdrip');

            $names = pathinfo($_FILES['upload_file']['name'][$count]);
            if (in_array($names['extension'], $types)) {
                mkdir("upload/$code/");
                $name = trim($arabic->ar2en($_FILES["upload_file"]["name"][$count]));
                $type = $_FILES["upload_file"]["type"][$count];
                $size = $_FILES["upload_file"]["size"][$count];
                $tmp_name = $_FILES["upload_file"]["tmp_name"][$count];
                if ($size < 600000000) {
                    $site_url = ('http://localhost');
                    $url = ("$site_url/download.php?file=$code");
                    mysql_query("SET NAMES utf8");
                    $query = mysql_query("INSERT INTO files VALUES ('Null','$name','$code','$type','$size','$url')");
                    $url = urlencode($name);
                    if ($upload = move_uploaded_file($upload_file, "upload/$code/" . $name)) {

                        if ($upload) {
                            ?>
                            <table>
                                <tr>
                                    <td><h3>File name</h3></td>
                                    <td><h3>File Link</h3></td>
                                    <td><h3>File Type</h3></td>
                                </tr>
                                <tr>
                                    <td><?php echo "$name"; ?></td>
                                    <td>
                                        <?php echo "<a href='http://localhost/downoald.php?file=$code' target='_blanck'>File Link</a>"; ?>
                                    </td>
                                    <td><?php print_r($names['extension']); ?></td>
                                </tr>
                            </table>
                        <?php } else { ?>
                            No file selected
                        <?php
                        }

                    } else {
                        echo "A problem occurred while uploading your file(s), please try again.";
                    }
                } else {
                    echo "This file(s) is up to 1GB<br>";
                }
            } else {
                echo "Type of the file not allowed";
            }
        } else {
            echo "this error is fan";
        }
    }
}
?>

答案 2 :(得分:0)

谢谢@meda 这是我的代码

发送给我的加法骑行

    <?php
set_time_limit(0);
require("connect.php");
if (isset($_FILES['upload_file'])) {
    foreach ($_FILES['upload_file']['tmp_name'] as $count=>$upload_file) {
            if (!empty($_FILES['image']['error'][$count])) {
            // Some error occured with the file in index $count
            // Put some error handling in here...
            return false; // or you could continue;
        }

        if (!empty($upload_file)&&is_uploaded_file($upload_file)) {
    $charset="AHfgG15Ds10MZD58N";
            $code='';
            $length=15;
            for ($i=0; $i<=$length; $i++) {
                $rand=rand()%strlen($charset);
                $tmp=substr($charset,$rand,1);
                $code.=$tmp;
            }
             mysql_query("SET NAMES utf8");
            $query=mysql_query("SELECT `code` FROM `files`  WHERE `code`='$code'") or die(mysql_error());
            $numrows=mysql_num_rows($query);

            while ($numrows!=0) {
                $code='';
                for ($i=0; $i<=$length; $i++) {
                    $rand=rand()%strlen($charset);
                    $tmp=substr($charset,$rand,1);
                    $code.=$tmp;
                }
                $query=mysql_query("SELECT `code` FROM `files`  WHERE `code`='$code'");
                $numrows=mysql_num_rows($query);
            }
            $types = array('jpg','jpeg','gif','png','zip','rar','mp3','m4a','wmv','divx','mp4','flv','FLV','dvd','mkv','bmp','xml','doc','pdf','xls','psd','svg','3gp','ppt','txt','exe','mpeg','dvdrip');
            $names = pathinfo($_FILES['upload_file']['name'][$count]);
            if (in_array($names['extension'],$types)) {
            mkdir("upload/$code/");
            $name=$_FILES["upload_file"]["name"][$count];
            $type=$_FILES["upload_file"]["type"][$count];
            $size=$_FILES["upload_file"]["size"][$count];
            $tmp_name=$_FILES["upload_file"]["tmp_name"][$count];
            if ($size < 600000000)
            {
            $site_url=('http://localhost');
            $url= ("$site_url/download.php?file=$code");
            mysql_query("SET NAMES utf8");
            $query=mysql_query("INSERT INTO files VALUES ('Null','$name','$code','$type','$size','$url')");
            $url=urlencode($name);
            if($upload = move_uploaded_file($upload_file ,"upload/$code/".$name))
            {

            if ($upload) { ?>
                <table>
                <tr>
                <td><h3>File name</h3></td><td><h3>File Link</h3></td><td><h3>File Type</h3></td>
                </tr>
                <tr>
                <td><?php echo"$name"; ?></td><td> <?php echo"<a href='http://localhost/downoald.php?file=$code' target='_blanck'>File Link</a>"; ?></td><td><?php print_r ($names['extension']); ?></td>
                </tr>
                </table>
            <?php } else { ?>
                No file selected
            <?php }

                        }else{
            echo "A problem occurred while uploading your file(s), please try again."; 
            }
            }else{
            echo "This file(s) is up to 1GB<br>";
            }
}else{
echo "Type of the file not allowed"; 
}
}else{
echo "this error is fan";
}
}
} 
?>