Php 上载图像文件错误
当我尝试上载文件时,此php脚本正在重新调整错误。我在WinXP机器上使用XAMPP。我相信我已正确设置了共享权限 剧本:Php 上载图像文件错误,php,html,file-upload,xampp,Php,Html,File Upload,Xampp,当我尝试上载文件时,此php脚本正在重新调整错误。我在WinXP机器上使用XAMPP。我相信我已正确设置了共享权限 剧本: //For images if (isset($_FILES['image300x100']) && !empty($_FILES['image300x100']['tmp_name'])) { $name = $_FILES['image300x100']['name']; // getting the name of the file
//For images
if (isset($_FILES['image300x100']) && !empty($_FILES['image300x100']['tmp_name'])) {
$name = $_FILES['image300x100']['name']; // getting the name of the file
$tempName = $_FILES['image300x100']['tmp_name']; // getting the temporary file name.
$allowedExt = array('jpg', 'jpeg', 'png', 'gif' );// specifying the allowed extentions
$a = explode('.', $name);
$fileExt = strtolower(end($a)); unset($a);//
$fileSize = $_FILES['image300x100']['size'];
$filePath = "";
switch($category){
case 1 : $filePath = "c:/www/Perspect/categories/politics/articleImages"; break;
//etc etc etc
}
$path = $filePath;
} else{
$errors[] = 'no file selected';
}
$moveResult = move_uploaded_file($tempName, $filePath);
如图所示,错误为:
Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in C:\www\Dev\admin\addNewArticle.php on line 62
Warning: move_uploaded_file(): Unable to move 'C:\Server\XAMPP\tmp\php62C.tmp' to 'c:/www/Dev/categories/bla/articleImages' in C:\www\Dev\admin\addNewArticle.php on line 62
我从路径中删除了c://
,我认为这是正确的路径,但结果是:
temp nameC:\Server\XAMPP\tmp\php62E.tmp filepath
www/Dev/categories/bla/articleImages
Warning: move_uploaded_file(www/Dev/categories/bla/articleImages): failed to open stream: No such file or directory in C:\www\Dev\admin\addNewArticle.php on line 62
Warning: move_uploaded_file(): Unable to move 'C:\Server\XAMPP\tmp\php62E.tmp' to 'www/Dev/categories/bla/articleImages' in C:\www\Dev\admin\addNewArticle.php on line 62
非常感谢任何帮助您需要指定目标文件名,例如:
$filePath = "c:/www/Perspect/categories/politics/articleImages/file.png";
或者,您可以生成一个随机名称。正如错误所述,第二个参数是目录,而不是文件。您需要将文件名添加到目标路径。我将为其指定一个特定于图像大小的名称,但我是否可以将文件名添加到移动函数而不是路径中?我想我意识到我做错了什么,我在重复一些早期代码,该代码使用了附加文件名的功能。derp[@SteveGreen是的,您可以将其附加到
move
函数中。它现在工作了吗?我刚刚更改:$moveResult=move\u上传的文件($tempName,$filePath.“testFile.png”);它正在工作,但文件显示为0字节。感谢您的帮助和评论,工作正常。只需添加名称等,但这应该相当简单。