我正在实现一个简单的PHP代码来接受上传的文件并将它们存储到“uploads文件夹”
我在stackexchange上看了几个答案,包括这个(这正是我面临的错误): WAMP: failed to open stream: No such file or directory
我得到的错误是:
警告:copy(speed.png):无法打开流:第6行的C:\ Program Files \ wamp \ www \ clientservertest \ imageupload.php中没有此类文件或目录
我的代码如下:
<?php
if( $_FILES['UploadedPic']['name'] != "" )
{
$dirPath=dirname(__FILE__).'\uploads\temp.png';
copy( $_FILES["UploadedPic"]["name"], $dirPath ) or
die( "Could not copy file!$dirPath");
}
else
{
die("No file specified!");
}
?>
它还在$ dirPath中打印绝对路径:
C:\ Program Files \ wamp \ www \ clientservertest \ uploads \ temp.png
绝对正确。
真的希望得到答案! :)
编辑:带有move_uploaded_file的代码:
<?php
if( $_FILES['UploadedPic']['name'] != "" )
{
$dirPath=dirname(__FILE__).'\uploads\temp.png';
move_uploaded_file( $_FILES["UploadedPic"]["name"], $dirPath ) or
die( "Could not copy file!$dirPath");
}
else
{
die("No file specified!");
}
?>
错误:无法复制文件!C:\ Program Files \ wamp \ www \ clientservertest \ uploads \ temp.png
答案 0 :(得分:1)
使用此代码
如果($ _ FILES [ 'UploadedPic'] [ '名称'])
{
$target='uploads/'.$_FILES['UploadedPic']['name'];
$source=$_FILES['UploadedPic']['tmp_name'];
copy($source,$target);
}
答案 1 :(得分:1)
您需要使用$_FILES['UploadedPic']['tmp_name']
这是tmp文件夹中的实际文件位置而不是$_FILES["UploadedPic"]["name"]
,这只是文件的名称。
此外,您需要检查上传是否正常,因为上传可能会失败的原因可能是:
$uploaddir = 'uploads/';
// Check for upload attempt
if(isset($_FILES['UploadedPic'])){
//$newfile = $uploaddir.basename($_FILES['UploadedPic']['name']);
$newfile = $uploaddir.'temp.png';
// If no error
if($_FILES['UploadedPic']['error'] == 0){
//Attempt to move
if (move_uploaded_file($_FILES['UploadedPic']['tmp_name'], $newfile)) {
echo "File was successfully uploaded.";
}else{
echo 'Error moving file.';
}
} else {
// Has error
$errors = array(
0=>"There is no error, the file uploaded with success",
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder"
);
echo "Error: ".$errors[$_FILES['UploadedPic']['error']];
}
}
另外,您可能需要查看检查上传实际上是getimagesize()
的图片希望它有助于好运。