<?php
// 在 4.1.0 以前的 PHP 中,需要用 $HTTP_POST_FILES 代替 $_FILES。
// 在 4.0.3 以前的 PHP 中,需要用 copy() 和 is_uploaded_file() 来代替 move_uploaded_file()。$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];
print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
    print "File is valid, and was successfully uploaded.  Here's some more debugging info:\n";
    print_r($_FILES);
} else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
}
print "</pre>";
?>

解决方案 »

  1.   

    你得PHP是什么版本!操作系统是什么?
    多提供点信息!
      

  2.   

    我使用的版本和系统是:
    windows2003 + apach2 + php5 + mysql5
    现在除了上传,其他运行非常正常。
    不知道有谁能够解释这个问题,大家碰到过没有?
    PHP4.0 的应用还算清楚, PHP5学习应用
      

  3.   

    1、你的程序本身有误!这可能是你节选贴出造成的,但这样一来就可能丢失真正的问题所在
    echo "上传失败");???
    2、move_uploaded_file函数的目标文件名并没有包含路径,与你“但是提示上传成功后相关目录下却没有当前时间做文件名的上传文件。”的描述不符。上传的文件在当前目录下
    3、排除已发现的错误后,测试结果正常。测试环境:windows2003 + iis6 + php5.0.3
      

  4.   

    update.html:
    <form action="uploadfile.php" method="post" enctype="multipart/form-data" name="upfrm" id="upfrm">
    <label>
    <input name="upload" type="file" id="upload" />
    </label>
    <label>
    <input type="submit" name="Submit" value="提交" />
    </label>
    </form>uploadfile.php:
    /*
    * 曾经做过以下几个定义上传均告失败
    $r = move_uploaded_file($_FILES['upload']['tmp_name'],"20050918133049");
    $r = move_uploaded_file($_FILES['upload']['tmp_name'],sprintf("%d",time()));
    */
    $r = move_uploaded_file($_FILES['upload']['tmp_name'],date("YmdHis"));
    if($r){
    echo $_FILES['upload']['tmp_name']."<br/>";
    echo "上传成功";
    }
    else{
    echo "上传失败");
    }
    我就简单的使用上述2个文件,并没有节选任何东西。
    没有设置上传路径,会保存到当前目录下,不是吗?,但是上传还是有问题,我的系统是win2003+apache2 + php5 这难到有什么问题吗?
    还是无法将 tmp_name 移动到 当前目录下,好像根本不能使用 长日期做文件名。包括U时间
      

  5.   

    也许是我的的表述不太清楚,这样吧,我重新写了一个上传的类使用还是出现上述问题(即:提示上传成功但是依然无法看到上传后的文件),大家帮我挑挑错误,小弟我刚学习PHP时间不久,还请各位前辈多多指教。
    /**
     * uploadfile.php
     *
     */
    <?php
    //上传文件类
    class  UpLoadFile{
    private $src;//上传源文件名称路径
    private $savepath;//保存上传文件的路径
    private $saveName;//上传文件后保存的名字
    private $fileType;//上传的文件类型
    private $allowSize;//允许上传的大小
    private $allowType;//允许上传的文件类型
    private $files; //上传文件构造方法
    public function __construct(){
    $this->savepath = ".";
    $this->saveName = $this->getFiles('name');
    $this->fileType = $this->getFiles('type');
    $this->allowSize = 8388608;
    } /**
     * 设置上传保存文件的路径
     *
     * @param String $savepath
     */
    public function setSavePath($savepath){
    $this->savepath = $savepath;
    } /**
     * 获取上传文文件路径
     *
     * @return unknown
     */
    public function getSavePath(){
    return $this->savepath;
    }
    /**
     * 设置上传文件保存后的名称
     *
     * @param String $filename
     */
    public function setSaveName($filename){
    $this->saveName = $filename;
    } /**
     * 获取上传文件的名称
     *
     * @return String
     */
    public function getSaveName(){
    //echo $this->saveName."<br/>";
    return $this->saveName;
    } /**
     * 设置和获取上传文件信息
     *
     * @param array $fils|$_FILES[]
     */
    public function setFiles($fils){
    $this->files = $fils;
    }
    /**
     * 获取上传文件信息$_FILES[]设定
     *
     * @param String $parm
     * @return String
     */
    public function getFiles($parm){
    return $this->files[$parm];
    } /**
     * 设置上传文件的类型是否合法
     *
     * @param String $allowType|多个用逗号隔开
     * @return boolean
     */
    public function setAllowType($allowType){
    $type = explode(",",$allowType);
    $ext = end(explode('.',$this->getFiles('name')));
    //$disallow = false;
    foreach ($type as $t){
    if ($t == $ext) {
    return  true;
    }
    }
    return false;
    } //----上传的尺寸----
    /**
     * 设置允许上传的文件尺寸
     *
     * @param int $size
     */
    public function setAllowSize($size){
    $this->allowSize = $size;
    } /**
     * 获取允许上传文件的大小尺寸
     *
     * @return int
     */
    public function getAllowSize(){
    return $this->allowSize;
    }
    /**
     * 是否限定上传文件的大小
     *
     * @return boolean
     */
    public function allowSize(){
    if ( $this->getFiles('size') > $this->getAllowSize()) {
    return false;
    }
    else {
    return true;
    }
    } //检测上传路径是否存在,如果不存在设定是否建立路径
    /**
     * 测试上传保存路径是否存在,如果不存在是否创建:param=true创建,否则不创建
     *
     * @param boolean $mkdir
     */
    public function testDir($mkdir = true){
    if($mkdir){
    $path = explode("/",$this->getSavePath());
    $dest = null;
    foreach ($path as $p){
    $dest .= $p;
    if (!is_dir($dest)) {
    mkdir($dest,777);
    }
    else {
    return false;
    }
    }
    return true;
    }
    else {
    return "没有发现这个文件夹或者路径!";
    }
    } /**
     * 上传文件
     *
     * @return boolean
     */
    public function upload(){
    $dest = $this->getSavePath().'/'.$this->getSaveName(); if (!is_uploaded_file($this->getFiles('tmp_name'))) {
    return false;
    }
    $r = @move_uploaded_file($this->getFiles('tmp_name'),$dest);
    return $r;
    }}
    ?>
    <?php
    define("NO_WRITE", 0);
    define("IS_STRING_WRITE", 1);
    define("IS_IMAGE_WRITE", 2);class UpLoadImage extends UpLoadFile { private  $strWrite;
    private  $imageWrite;
    private  $smallImageWidth;
    private  $smallImageHeight;
    private  $smallImageName;
    private $wrtieable; public function __construct(){
    parent::upload();
    } //
    /**
     * 创建缩略图$dest是创建缩略图的源文件
     *
     * @param  String
     */
    public function createSmallImage($dest){
    try {
    $image = imagecreatefromjpeg($dest);
    list($width, $height, $type, $attr) = getimagesize($dest);
    $s = $width/$height;
    if ($s > 1) {
    $im = imagecreatetruecolor($this->getSmallImageWidth(), $this->getSmallImageWidth()/$s);
    imagecopyresized($im, $image, 0, 0, 0, 0, $this->getSmallImageWidth(), $this->getSmallImageWidth()/$s, $width, $height);
    }
    else {
    $im = imagecreatetruecolor($this->getSmallImageHeight()*$s, $this->getSmallImageHeight());
    imagecopyresized($im, $image, 0, 0, 0, 0, $this->getSmallImageHeight()*$s, $this->getSmallImageHeight(), $width, $height);
    }
    imagejpeg($im, $this->getSaveName());
    imagedestroy($im);
    imagedestroy($image);
    }
    catch (Exception $e){
    throw $e;
    } } //
    /**
     * 创建文字水印 $dest 添加水印的图片名及路径
     *
     * @param String $dest
     */
    private function createStringWrite($dest){
    try {
    $dest = $this->getSavePath()."/".$this->getSaveName();
    $forecolor = imagecolorallocate($image,242,242,242);
    $image = imagecreatefromjpeg($dest);
    list($width, $height, $type, $attr) = getimagesize($dest);
    imagestring($image,5,$width/2-150, $height-100, $this->getStrWrite());
    imagejpeg($image,$dest);
    imagedestroy($image);
    }
    catch (Exception $e){
    throw $e;
    }
    } /**
     * 创建图形水印$dest需要添加水印的文件
     *
     * @param String $dest
     */
    private function createImageWrite($dest){
    try {
    $image = imagecreatefromjpeg($dest);
    $write = imagecreatefromjpeg($this->getImageWrite());
    list($width, $height, $type, $attr) = getimagesize($dest);
    list($write_width, $write_height, $write_type, $write_attr) = getimagesize($write); imagecopy($image, $write, $width/2-$write_width/2, $width-$write_height-10,0,0, $write_width, $write_height);
    imagedestroy($image);
    imagedestroy($write);
    }
    catch (Exception $e){
    throw $e;
    }
    } /**
     * 是否添加水印
     *
     * @param unknown_type $write

    public function setWriteable($write){
    $this->wrtieable = $write;
    } */
    /**
     * 获取是否添加水印
     *
     * @return unknown
     
    public function getWriteable(){
    return $this->wrtieable;
    }*/ /**
     * 设置缩略图的大小
     *
     * @param int $imagewidth
     * @param int $imageheight
     */
    public function setSmallImage($imagewidth, $imageheight){
    $this->smallImageWidth = $imagewidth;
    $this->smallImageHeight = $imageheight;
    }
    /**
     * 获取缩略图的宽
     *
     * @return int
     */
    public function getSmallImageWidth(){
    return $this->smallImageWidth;
    }
    /**
     * 获取缩略图的高
     *
     * @return int
     */
    public function getSmallImageHeight(){
    return $this->smallImageHeight;
    } //设置图片水印内容
    public function setImageWrite($imagewrite){
    $this->imageWrite = $imagewrite;
    }
    /**
     * 获取图片水印的名称
     *
     * @return unknown
     */
    public function getImageWrite(){
    return $this->imageWrite;
    } //
    /**
     * 设置字符串水印的内容
     *
     * @param String $String
     */
    public function setStrWrite($String){
    $this->strWrite = $String;
    }
    /**
     * 获取字符串水印的文字串
     *
     * @return String
     */
    public function getStrWrite(){
    return $this->strWrite;
    }
    }
    ?>
    <?php
    $up = new UpLoadFile();
    $up->setFiles($_FILES['upload']);
    $ext = end(explode('.',$up->getFiles('name')));
    $upname = date('YzHis');
    echo $upname;
    $up->setSaveName("$upname.$ext");
    $up->upload();
    ?>/**
     * upload.html
     *
     */
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <form action="uploadfile.php" method="post" enctype="multipart/form-data" name="upfrm" id="upfrm">
      <label>
      <input name="upload" type="file" id="upload" />
      </label>
      <label>
      <input type="submit" name="Submit" value="提交" />
      </label>
    </form>
    </body>
    </html>
      

  6.   

    与文件名没有关系你仔细检查一下,是不是被上传到 php 目录里了 或是系统目录里
      

  7.   

    胡说八道!
    既然是“没有设定目录的读写权限”那怎么会是“但是文件名改为 "abc"或者任意字母组合上传成功,找到abc文件。”
      

  8.   

    楼主是来忽悠我们的吧...如果没有上传权限,直接就出错了...php函数都会提示没有权限.
      

  9.   

    其实这个问题我也没有搞清楚,不过现在问题解决了,心里还是很高兴的
    现在发现 上传文件 调用日期做文件名,上传就会失败,但是重新 chmod 777一下 上传就没有问题了
    可是 不运行 chmod 777 居然可以上传 字母组合做文件名,而不能保存为 日期 做文件的名。
    这个问题,还是要请高手来解答一下。如果大家有兴趣可以试试,系统 2003 apache php5 ,保存上传文件为当前执行php脚本路径  。文件名为 date("YmdHis") 或者 time();  
          ---这是我最近发现的问题,更换了几台机器,系统配置相同。都出现这样的问题。
           
         声明一下,我绝对没有忽悠大家。是的的确确的问题