php 警告:move_uploaded_file() 无法打开流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16079220/
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 10:29:02  来源:igfitidea点击:

Warning: move_uploaded_file() failed to open stream

phpfile-upload

提问by Nirali Joshi

I am trying to upload file on ftp.

我正在尝试在 ftp 上上传文件。

here is my code

这是我的代码

    $jname= "Accounts of Biotechnology Research";
    if (!is_dir('/Trade/upload/ '.$jname)) {

        mkdir('/Trade/upload/ '.$jname);   // line 63
    }
    move_uploaded_file($_FILES["submission_file"]["tmp_name"],    "/Trade/upload/$jname/" . $dup_name );    // line 67

Trade is a folder inside public_html folder.

Trade 是 public_html 文件夹中的一个文件夹。

When i am uploading a file it gives me a warning like,

当我上传文件时,它会给我一个警告,例如,

Warning: mkdir() [function.mkdir]: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 63

Warning: move_uploaded_file(/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 67

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phphZXp0O' to '/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf' in /home/my_username/public_html/Trade/upload.php on line 67 

回答by Viktor S.

First: You have a space here mkdir('/Trade/upload/ '.$jname);. Suppose you should have mkdir('/Trade/upload/'.$jname);(same for is_dir)

第一:你在这里有一个空间mkdir('/Trade/upload/ '.$jname);。假设你应该有mkdir('/Trade/upload/'.$jname);(is_dir 相同)

Second: Ensure that you can write into Trade/uploaddirectory.

第二:确保您可以写入Trade/upload目录。

Third (and I suppose that is the real problem):

第三(我想这是真正的问题):

It looks like you are trying to upload into a directory with full path: /home/my_username/public_html/Trade/upload/, but your code will try to create a directory with full path: /Trade/upload/. You need to change

看起来您正在尝试上传到具有完整路径的目录: /home/my_username/public_html/Trade/upload/,但您的代码将尝试创建具有完整路径的目录: /Trade/upload/。你需要改变

 if (!is_dir('/Trade/upload/ '.$jname)) {    
     mkdir('/Trade/upload/ '.$jname);   // line 63
 }

to

 if (!is_dir('upload/'.$jname)) {    
     mkdir('upload/'.$jname);   // line 63 (or maybe there should be Trade/upload, but suppose current working dir will be /home/my_username/public_html/Trade, so only upload/)
 }

Another option is to force mkdirto create directories recursively:

另一种选择是强制mkdir递归创建目录:

 mkdir('/Trade/upload/'.$jname, 0755, true);

But in that case, files will be uploaded into /Trade/upload/...instead of /home/my_username/public_html/Trade/upload/...

但在这种情况下,文件将被上传到/Trade/upload/...而不是/home/my_username/public_html/Trade/upload/...

回答by Emmanuel Okeke

There are 2 things you should be aware of based on the error messages you received.
I'm guessing /Tradeisn't the root path on your machine since it's clear in the error that your actual path is /home/my_username/public_html/Trade/, so the first adjustment should be

根据您收到的错误消息,您应该注意两件事。
我猜/Trade这不是你机器上的根路径,因为在错误中很明显你的实际路径是/home/my_username/public_html/Trade/,所以第一次调整应该是

$root_path = "/home/my_username/public_html/Trade/upload/";    

The second adjustment I'd suggest is that you avoid pathnames with space in them:

我建议的第二个调整是避免路径名中包含空格:

$jname= "Accounts of Biotechnology Research"; //could be changed to
$jname= "Accounts_of_Biotechnology_Research"; //$jname = str_replace(" ","-",$jname)    OR
$jname= "Accounts-of-Biotechnology-Research"; //$jname = str_replace(" ","-",$jname)    

Finally take note of the space character on the following lines, they affect you final result:

最后注意以下几行的空格字符,它们会影响您的最终结果:

if (!is_dir('/Trade/upload/ '.$jname)) {   //AND
    mkdir('/Trade/upload/ '.$jname);
}

Note the [space] between upload/ '.$jnamein both strings.
Hope this helps.

注意upload/ '.$jname两个字符串之间的 [空格] 。
希望这可以帮助。