PHP - 警告:复制(..):无法打开流:中没有这样的文件或目录

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

PHP - Warning: copy(..): failed to open stream: No such file or directory in

phpfile-uploaduploadstreamcopy

提问by Brian

I'm fairly new to PHP and was trying to create a simple PHP file upload system.

我对 PHP 还很陌生,正在尝试创建一个简单的 PHP 文件上传系统。

I followed a tutorial from (http://www.phpeasystep.com/phptu/2.html). I only altered the $HTTP_POST_FILES, as it was giving me errors, and from what I read it's old in PHP.

我遵循了 (http://www.phpeasystep.com/phptu/2.html) 中的教程。我只更改了$HTTP_POST_FILES,因为它给了我错误,并且从我读到的内容来看,它在 PHP 中很旧。

I got less error messages but I am getting an error in the copy()function, with these given error messages:

我收到的错误消息较少,但在 copy()函数中出现错误,并显示以下错误消息:

Warning: copy(Task2/uploads/anonymous.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\Task2\upload.php on line 13

Warning: copy(Task2/uploads/DSCF4639.JPG): failed to open stream: No such file or directory in C:\xampp\htdocs\Task2\upload.php on line 14

Warning: copy(Task2/uploads/jien maroon.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\Task2\upload.php on line 15

I thought it was a problem with permission (read/write permissions in Windows 7), but from a quick google search it seems that XAMPP is set by default to deal with permission on Win 7.

我认为这是权限问题(Windows 7 中的读/写权限),但从谷歌快速搜索来看,XAMPP 似乎默认设置为处理 Win 7 上的权限。

This is the code:

这是代码:

<?php

//set where you want to store files
//in this example we keep file in folder upload
//$_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif

$path1= "Task2/uploads/".$_FILES['ufile']['name'][0];
$path2= "Task2/uploads/".$_FILES['ufile']['name'][1];
$path3= "Task2/uploads/".$_FILES['ufile']['name'][2];

//copy file to where you want to store file
copy($_FILES['ufile']['tmp_name'][0], $path1);
copy($_FILES['ufile']['tmp_name'][1], $path2);
copy($_FILES['ufile']['tmp_name'][2], $path3);

//$_FILES['ufile']['name'] = file name
//$_FILES['ufile']['size'] = file size
//$_FILES['ufile']['type'] = type of file
echo "File Name :".$_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$_FILES['ufile']['size'][0];
$filesize2=$_FILES['ufile']['size'][1];
$filesize3=$_FILES['ufile']['size'][2];

if($filesize1 && $filesize2 && $filesize3 != 0)
{
echo "We have recieved your files";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

if($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}
?>

Any help would be appreciated !

任何帮助,将不胜感激 !

Thanks !

谢谢 !

回答by Naftali aka Neal

Do notuse copy, use move_uploaded_file(...)

千万不能使用copy,使用move_uploaded_file(...)

回答by Sebastian Viereck

make sure that the destination directory exists, copy will not create directories for you. The 3rd parameter will also create directories recursive.

确保目标目录存在,复制不会为您创建目录。第三个参数也将创建递归目录。

if (!is_dir($directory)) {
    mkdir($directory, 0777, true);
}

回答by Brian

Solved the problem !

解决了问题!

The problem was with the Path.. instead of Task2/uploads/I had to put ../Task2/uploads/.

问题出在 Path .. 而不是Task2/uploads/我必须把../Task2/uploads/.

Thanks!

谢谢!