PHP:move_uploaded_file() 无法打开流:没有这样的文件或目录

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

PHP: move_uploaded_file() failed to open stream: No such file or directory

phpapachefile-upload

提问by Chuck Le Butt

I'm trying to get PHP to move an uploaded file from the tmp directory to somewhere permanent on my webserver. It seems simple enough, but I'm getting this error:

我试图让 PHP 将上传的文件从 tmp 目录移动到我的网络服务器上的某个永久位置。看起来很简单,但我收到了这个错误:

Unable to move 'C:\UniServer\tmp\php3F62.tmp' to 'static/images/slides/1/1.jpg'

Unable to move 'C:\UniServer\tmp\php3F62.tmp' to 'static/images/slides/1/1.jpg'

Pretty straight-forward, right? It can't find the destination folder.

很直接,对吧?它找不到目标文件夹。

My question is: How do I reference the desired target directory?

我的问题是:如何引用所需的目标目录?

Is the reference relative to the script's position on the server? Or is it relative to the URL? Or the PHP DOCUMENT_ROOT? Or the OS's filesystem? Or something else?

引用是否相对于脚本在服务器上的位置?还是相对于 URL?还是PHP DOCUMENT_ROOT?还是操作系统的文件系统?或者是其他东西?

I can't find the answer in the PHP documentation or indeed in any of the similar questions here on SO..

我在 PHP 文档中或确实在 SO 上的任何类似问题中都找不到答案。

Can anyone help? Thanks.

任何人都可以帮忙吗?谢谢。

回答by Lawrence Cherone

A simple way to keep track of the path is just to define the absolute path in your index.php

跟踪路径的一种简单方法就是在 index.php 中定义绝对路径

define ('SITE_ROOT', realpath(dirname(__FILE__)));

Then just use it like:

然后像这样使用它:

move_uploaded_file($_FILES['file']['tmp_name'], SITE_ROOT.'/static/images/slides/1/1.jpg');

回答by Elina

I had the same problem with my uploading. See my example and maybe it can help you.

我在上传时遇到了同样的问题。看看我的例子,也许它可以帮助你。

The file that I created is named "uploads".

我创建的文件名为“上传”。

$uploads_dir = 'uploads/';
$name = $_FILES['userfile']['name'];
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{       
    //in case you want to move  the file in uploads directory
     move_uploaded_file($_FILES['userfile']['tmp_name'], $uploads_dir.$name);
     echo 'moved file to destination directory';
     exit;
}

回答by Wayne Lee

$destination = dirname(dirname(dirname(dirname(__FILE__))))."/runtime/tmp/";
chown($destination, 0755);
move_uploaded_file($info['tmp_name'], $destination.$info['name']);

This is my solution, I just use mkdir to create a directory to put my picture which I want to move. Wish it helps.

这是我的解决方案,我只是使用 mkdir 创建一个目录来放置我要移动的图片。希望它有所帮助。

回答by Phrankexist Bailey

I think this will help you without giving you any error:

我认为这会帮助你而不会给你任何错误:

$image = $_FILES['image']['name'];
$image_temp =$_FILES['image']['tmp_name'];
$target='C:/xampp/htdocs/jer/images/'.basename($_FILES['image']['name']);
move_uploaded_file($image_temp, "$target");
insert into images (images) values ('$target');

回答by Praveen Kumar Purushothaman

It is from the script's position on the server! And moreover, you need to have write permissions in this folder:

它来自脚本在服务器上的位置!此外,您需要在此文件夹中具有写入权限:

'static/images/slides/1/1.jpg'

Instead it is better to use an absolute path this way:

相反,最好以这种方式使用绝对路径:

'C:\UniServer\***\static\images\slides.jpg

Use an absolute path.

使用绝对路径。