[英]PHP: move_uploaded_file() failed to open stream: No such file or directory
I'm trying to get PHP to move an uploaded file from the tmp directory to somewhere permanent on my webserver.我正在尝试让 PHP 将上传的文件从 tmp 目录移动到我的网络服务器上的某个永久位置。 It seems simple enough, but I'm getting this error:看起来很简单,但我收到了这个错误:
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?引用是否与服务器上脚本的 position 相关? Or is it relative to the URL?还是与 URL 相关? Or the PHP DOCUMENT_ROOT
?还是PHP DOCUMENT_ROOT
? Or the OS's filesystem?还是操作系统的文件系统? Or something else?或者是其他东西?
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.谢谢。
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');
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;
}
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\1\1.jpg
Use an absolute path.使用绝对路径。
$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.这是我的解决方案,我只是使用 mkdir 创建一个目录来放置我要移动的图片。 Wish it helps.希望它有所帮助。
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');
Add $_SERVER['DOCUMENT_ROOT']
before the name of the destination of your file.在文件目标名称之前添加$_SERVER['DOCUMENT_ROOT']
。 It will add an absolute location of your project on the server so the file can be upload in the exact location that you want.它将在服务器上添加项目的绝对位置,以便文件可以上传到您想要的确切位置。
Example of code代码示例
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$file_name = time(). "-". rand(100000, 1000000). ".". $ext;
$destination = $_SERVER['DOCUMENT_ROOT'].'/storage/uploads/'. $file_name;
$res = move_uploaded_file($file['tmp_name'], $destination);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.