[英]PHP: move_uploaded_file() failed to open stream: No such file or directory
[英]php file upload move_uploaded_file() Failed to open stream
我有一个文件上传脚本:
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["picture"]["name"]));
if ((($_FILES["picture"]["type"] == "image/gif") || ($_FILES["picture"]["type"] == "image/jpeg") || ($_FILES["picture"]["type"] == "image/jpg") || ($_FILES["picture"]["type"] == "image/png")) && in_array($extension, $allowedExts)):
if($_FILES["picture"]["error"] > 0):
echo "Error: " . $_Files["picture"]["error"];
else:
move_uploaded_file($_FILES["picture"]["tmp_name"], "/TnA/ProfilePics/" . $_SESSION['ID'] . "." . $extension);
endif;
endif;
但是我得到了错误:
Warning: move_uploaded_file(TnA/ProfilePics/1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/91/9848191/html/TnA/webservice.php on line 1067
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php5nOdhI' to 'TnA/ProfilePics/1.jpg' in /home/content/91/9848191/html/TnA/webservice.php on line 1067
这是我的文件结构:
Web Host Root (I have another site here)
-TnA (Root of this site)
-index.html
-webservice.php
-ProfilePics
-(My target location)
我应该使用哪个相对目录URL? 我尝试了ProfilePics/1.jpg
和/ProfilePics/1.jpg
都导致相同的错误。
编辑:
使用方法:
move_uploaded_file($_FILES["picture"]["tmp_name"], dirname(__FILE__) . "ProfilePics/" . $_SESSION['ID'] . "." . $extension);
我得到:
Warning: move_uploaded_file(/home/content/91/9848191/html/TnA/ProfilePics/1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/91/9848191/html/TnA/webservice.php on line 1067
您很可能在使用相关路径时遇到问题。 如果您的上传脚本是webservice.php,那么您实际上是在尝试将文件放入/%webHostRoot%/ TnA / TnA / ProfilePics /目录。 在move_uploaded_file()
的目标中使用完整路径。 尝试使用类似:
move_uploaded_file($_FILES["picture"]["tmp_name"], dirname(__FILE__)."ProfilePics/" . $_SESSION['ID'] . "." . $extension);
UPD:这是递归创建目录的功能:
function MkDirTree($path,$permissions = 0755, $compat_mode=true) {
if (!$compat_mode) {
$dirs = split("/",$path);
$path = "";
foreach ($dirs as $key=>$dir) {
$path .= $dir."/";
if ($dir!="" && !is_dir($path)) exec("mkdir -m ".$permissions." ".$path);
}
} else {
$dirs = split("/",$path);
$path = "";
foreach ($dirs as $key=>$dir) {
$path .= $dir."/";
if ($dir!="" && !file_exists($path)) mkdir($path, $permissions);
}
}
return file_exists($path);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.