php move_uploaded_file 无法打开流和权限被拒绝错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20582507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:18:48  来源:igfitidea点击:

move_uploaded_file failed to open stream and Permission denied error

php.htaccessfile-upload

提问by user2988966

i'm trying to upload a file but i'm getting this following errors in my browser:

我正在尝试上传文件,但在浏览器中出现以下错误:

Warning  move_uploaded_file(public/upload/udesignwptheme138.zip) [function.move-uploaded-file]: failed to open stream: Permission denied in <b>/home/monivbr/public_html/classes/system/Util.php on line 1803

Warning:  move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpJtBlbi' to 'public/upload/udesignwptheme138.zip' in /home/monivbr/public_html/classes/system/Util.php on line 1803

this is my php class that make uploads to my server:

这是我的 php 类,用于上传到我的服务器:

            foreach ($files as $file)
            if($file['tmp_name'] != "" && $file['error'] != 4){

                if($file['size'] < 10000000000){
                    $nome_antigo = $file["name"];
                    $novo_nome = strtolower(preg_replace("/[^a-zA-Z0-9_.]/", "", strtr($nome_antigo, "áà?aéêíó??úü?áà??éêíó??úü? ", "aaaaeeiooouucAAAAEEIOOOUUC_")));


                    $query = $conexao->prepare('SELECT MAX(id) AS maxId FROM tbArquivo');
                    $query->execute();
                    $arquivo = $query->fetchObject();   

                    $caminhoArquivo = 'public/upload/';
                    $nomeArquivo = substr($novo_nome, 0, strripos($novo_nome, '.')).$arquivo->maxId;
                    if($idTipoArquivo == '6'){ //6 = arquivos xml de tradu??o
                        $caminhoArquivo  = 'public/traducao/';
                        $nomeArquivo = substr($novo_nome, 0, strripos($novo_nome, '.'));
                    }

                    $extensao = substr($novo_nome, strripos($novo_nome, '.'));

This line below is where is located the error:

下面这一行是错误所在的位置:

move_uploaded_file($file["tmp_name"],$caminhoArquivo.$nomeArquivo.$extensao);




                    $query = $conexao->prepare("INSERT INTO tbArquivo
                                                (idTipoArquivo, idComplementar, idComplementar2, nomeArquivo, caminhoArquivo, tamanhoArquivo, extencaoArquivo, excluido)
                                                VALUES
                                                (:idTipoArquivo, :idComplementar, :idComplementar2, :nomeArquivo, :caminhoArquivo, :tamanhoArquivo, :extencaoArquivo, 0)");
                    $query->bindParam(":idTipoArquivo", $idTipoArquivo);
                    $query->bindParam(":idComplementar", $idComplementar);
                    $query->bindParam(":idComplementar2", $idComplementar2);
                    $query->bindParam(":nomeArquivo", $nomeArquivo);                
                    $query->bindParam(":caminhoArquivo", $caminhoArquivo);
                    $query->bindParam(":tamanhoArquivo", $file['size']);
                    $query->bindParam(":extencaoArquivo", $extensao);

                    $query->execute();

                }
            }   

this is my .htaccess file:

这是我的 .htaccess 文件:

php_value upload_max_filesize 30M
php_value post_max_size 30M
php_value max_input_time 6000000
php_value max_execution_time 6000000
php_value memory_limit 35M

this is the var_dump of the variables:

这是变量的 var_dump:

var_dump($extensao);
var_dump($file["tmp_name"]);
var_dump($caminhoArquivo);
var_dump($nomeArquivo);

string(4) ".zip" string(14) "/tmp/phpKUpN24" string(14) "public/upload/" string(17) "udesignwptheme139" 

someone knows what culd be wrong ?

有人知道可能有什么问题吗?

回答by Artas

Maybe you can consider changing the upload folder chmod 755 or 777

也许你可以考虑更改上传文件夹 chmod 755 或 777

chmod 777 folder_path 

By this we are setting read. write and execute privilege for owner, group as well as others.

通过这个我们设置读取。所有者、组以及其他人的写入和执行权限。

回答by Ravindra Miyani

Its because of the you dont have the sufficient permission on the Folder. Change this permission to read and write. That's all !

这是因为您对文件夹没有足够的权限。将此权限更改为读写。就这样 !

回答by Shabeer Sha

Do not change the folder path

不要更改文件夹路径

chmod -R 777 /home/monivbr/public_html/classes/system/Util.php