为啥我用这段php上传代码总是出现这个问题?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥我用这段php上传代码总是出现这个问题?相关的知识,希望对你有一定的参考价值。

用的是下面的php代码:
<html>
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="file.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->

<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</html>

//file.php
<?php
$uploaddir = './var/';
echo $uploaddir ;

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo $uploadfile;

echo '<br>Come in <br>';

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
echo "File is valid, and was successfully uploaded.\n";
else
echo "Possible file upload attack!\n";
echo 'Here is some more debugging info:';
print_r($_FILES);


?>
但是上传后总是出现
/bt//bt/bb_qvod.png
Come in

Warning: move_uploaded_file(/bt/bb_qvod.png) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/ipower/public_html/bt/upload.php on line 12

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpZkTgsp' to '/bt/bb_qvod.png' in /home/ipower/public_html/bt/upload.php on line 12
Possible file upload attack! Here is some more debugging info:Array ( [userfile] => Array ( [name] => bb_qvod.png [type] => image/png [tmp_name] => /tmp/phpZkTgsp [error] => 0 [size] => 1050 ) )
还有如何控制上面的文件上传类型

参考技术A 这个错误提示的是:无法将文件复制到指定位置。。我看了你的代码。估计你上传的文件。和你正在编辑的文件。。不是同一个文件。

$uploaddir = './var/';

这一行代码和你显示出来的不一样。
你用的应该是虎翼网的空间吧。。如果你要上传到 bt 目录。直接这么写就可以了。

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $_FILES['userfile']['name']))
参考技术B 给你个我写的上传类自已去看看吧
class upload
private $uploadFile; //上传路径
private $uploadType; //数组上传文件类型
private $uploadSize; //上传文件大小

public function __construct($uploadFile , $uploadType , $uploadSize)
$this->uploadFile = $uploadFile;
$this->uploadType = $uploadType;
$this->uploadSize = $uploadSize;


public function is_upload($name)
$error = new error();
if(!is_uploaded_file($_FILES[$name][tmp_name]))
return $error->alert_msg("文件上传不成功,请重新上传" , 1);

return true;


public function is_uploadSize($name)
$error = new error();
if($_FILES[$name]['size'] > $this->uploadSize)
echo $this->uploadSize;
return $error->alert_msg("文件太大了,请压缩后重新上传" , 1);

return true;


public function is_uploadType($name)
$fileType = explode( "." , $_FILES[$name]['name'] );
$count = (count($fileType) - 1);
$type = $fileType[$count];
for($i=0;$i<count($this->uploadType);$i++)
$retType .= $this->uploadType[$i].",";

$setType = trim($retType , ",");
$error = new error();
if(!in_array( $type , $this->uploadType ))
return $error->alert_msg("上传文件类型错误,只能上传".$setType."类型的文件,请重新上传" , 1);

return true;


public function uploading ($name)
$error = new error();
if($this->is_upload($name) && $this->is_uploadSize($name) && $this->is_uploadType($name))
if(!file_exists($this->uploadFile))
mkdir($this->uploadFile , 0777) or die("无法创建目录");

$fileName = date("Y-m-j" , time())."-".$_FILES[$name]['name'];
if(!file_exists($this->uploadFile."/".$fileName))
if(!move_uploaded_file ($_FILES[$name]['tmp_name'], $this->uploadFile."/".$fileName))
return $error->alert_msg("文件无法移动,请检查设置" , 1);

else
return $error->alert_msg("上传文件已存在,请重新上传" , 1);


return true;


参考技术C $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
去掉basename即可

最好是加个是否上传成功的判断本回答被提问者采纳
参考技术D if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))

你这句是怎么移动的 没给文件名好么!!!!
第5个回答  2008-06-09 <form action="xxx.php" method="post" enctype="multipart/form-data">

少了这句吧enctype="multipart/form-data"

以上是关于为啥我用这段php上传代码总是出现这个问题?的主要内容,如果未能解决你的问题,请参考以下文章

为啥猫鼬在更新时不验证?

为啥字典比列表快得多?

为啥用户点击后按钮图像会恢复?

WRONGTYPE 对持有错误值 php 的键的操作

错误:需要以下参数:-i/--image

为啥这段代码没有将数据插入数据库?