V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Wao
V2EX  ›  问与答

PHP 问题:如何把上传文件这个功能改为选择性的?目前是必须的,谢谢

  •  
  •   Wao · 2014-03-03 12:59:31 +08:00 · 2886 次点击
    这是一个创建于 3900 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我自己加了个很简单的判断,但是不对,文件上传不了了,也没错误提示
    在线等,谢谢!

    code:
    ======================================
    <?php
    include ('../header.php');
    include ('../secure_page.php');
    include ('../php/phpClass/data_class.php');

    $userID=$_SESSION['user_id'];
    $user=$_SESSION['username'];

    $dest_dir="../media/$user/".date(Ymd);

    if( !is_dir($dest_dir) || !is_writeable($dest_dir) )
    {
    if(!mkdir($dest_dir, 0777, true))
    die("The expected directory ".$dest_dir." encountered errors");
    }

    $type=array("png","jpg","jpeg");

    $upfile=$_FILES['file'];

    if($upfile != NULL)
    {

    function fileext($filename)
    {
    return substr(strrchr($filename, '.'), 1);
    }

    if( !in_array( strtolower( fileext($upfile['name'] ) ),$type) )
    {
    $text=implode(",",$type);
    echo "Sorry, you cannot upload this file type: ",$text;
    }
    else
    {
    $dest=$dest_dir.'/'.date("His")."_".$upfile['name'];

    $state=move_uploaded_file($upfile['tmp_name'],$dest);

    if ($state)
    {
    print("File Uploaded. Thanks for your contribution!");
    }
    else
    {
    switch($upfile['error'])
    {
    case 1 : die("File size is larger than php.ini: upload_max_filesize");
    case 2 : die("File is too large");
    case 3 : die("Only part of file has been uploaded");
    case 4 : die("No file has been uploaded");
    case 5 : die("Cannot find the expected directory");
    case 6 : die("File cannot be written");
    }
    }
    }
    }
    else
    {
    print("Thanks for your contribution!");
    }

    $aname = $_POST['aname'];
    $atime = $_POST['atime'];
    $adate = $_POST['adate'];
    $latitude = $_POST['latitude'];
    $longitude = $_POST['longitude'];
    $agchoice = $_POST['agchoice'];
    $age = $_POST['age'];
    $aschoice = $_POST['aschoice'];
    $adescription = $_POST['adescription'];

    $dataClass = new Data();
    $data = array($aname,$atime,$adate,$latitude,$longitude,$agchoice,$aschoice,$adescription,$age,$dest);

    $h = $dataClass->add_sighting($data, $userID);


    ?>
    ==========================
    2 条回复    1970-01-01 08:00:00 +08:00
    vcex
        1
    vcex  
       2014-03-07 10:30:32 +08:00   ❤️ 1
    目测,$dest变量保存了上传文件的保存路径。当没有文件被上传时候, $dest并没有被定义,所以在函数$dataClass->add_sighting ()中 $data的长度不够,
    解决方法:
    else
    {
    print("Thanks for your contribution!");
    }
    里面加上一句对 $dest的赋值
    Wao
        2
    Wao  
    OP
       2014-03-07 12:05:43 +08:00
    @vcex 谢谢~ 不过应该不是您说的原因

    我把最开始对文件是否为空的判断改为 if( $_FILES['file']['name'] != "" ) 后,问题解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1207 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 23:02 · PVG 07:02 · LAX 15:02 · JFK 18:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.