将空文件上传并存储到数据库中,并将上传大小限制为1mb

时间:2013-03-14 23:16:50

标签: php file

  1. 我希望能够在我的数据库中存储空文件。
  2. 我想将文件大小限制为1mb。
  3. 我正在尝试使用和不使用文件进行上传,但第一个if($_FILES['word']['size'] = (0000000))不会执行。相反,它执行ELSE语句if($_FILES['word']['size'] < (1024000))

    <?php
    error_reporting(E_ALL ^ E_NOTICE);
    mysql_connect("localhost", "root", "") or die (mysql_error());
    mysql_select_db("calendario") or die (mysql_error());
    
    
    #Add Files in Database#
    session_start();
    if(isset($_POST['btnadd'])){
    $title =$_POST['txttitle'];
    $detail =$_POST['txtdetail'];
    
    $month=$_POST['month'];
    $day =$_POST['day'];
    $year =$_POST['year'];
    $eventdate = $month."/".$day."/".$year;
    
    $emonth=$_POST['Emonth'];
    $eday =$_POST['Eday'];
    $eyear =$_POST['Eyear'];
    $expire = $emonth."/".$eday."/".$eyear;
    
    $code = $_SESSION['code'];
    
    $id = $_SESSION["username"];
    
    ##############################################################################################Image
    
        $word =$_FILES['word']['tmp_name'];
        $word_name = addslashes($_FILES['word']['name']);
        $word_size = addslashes($_FILES['word']['size']);
        $word_type = $_FILES['word']['type'];
    
    
    
    if($_FILES['word']['size'] = (0000000)) //if files uploaded empty, still enter database
    {
       echo("<script language=\"JavaScript\" type=\"text/JavaScript\">\n");
        echo("alert('File Error 0.');\n");
        echo("window.location = ('calender.php');\n");
        echo("</script>\n");
    }
    else
    {
        if($_FILES['word']['size'] < (1024000)) //if files is less than 1mb
        {
        echo("<script language=\"JavaScript\" type=\"text/JavaScript\">\n");
        echo("alert('File Error.');\n");
        echo("window.location = ('calender.php');\n");
        echo("</script>\n");
        }
    
        else{
        $word = addslashes(file_get_contents($_FILES['word']['tmp_name']));
        $word_name = addslashes($_FILES['word']['name']);
        $word_size = addslashes($_FILES['word']['size']);
        $fp      = fopen($word, 'r');
        $content = fread($fp, filesize($word));
        $content = addslashes($content);
        fclose($fp);
    
    
      if(!get_magic_quotes_gpc())
    {
        $word_name = addslashes($word_name);
    }
    
        }
    
    
    ############################################################################################Insert
    $sqlinsert = "INSERT into eventcalendar(classCode,Title,Detail,eventDate,dateAdded,Image,WordPpt,WordPptName,WordPptSize,WordPptType,Username,Edate) values ('".$code."','".$title."','".$detail."','".$eventdate."',now(),'".$image."','".$content."','".$word_name."','".$word_size."','".$word_type."','".$id."','".$expire."')";
    $resultinginsert = mysql_query($sqlinsert);
    }
    if($resultinginsert){
    echo("<script language=\"JavaScript\" type=\"text/JavaScript\">\n");
    echo("alert('Done.');\n");
    echo("window.location = ('calender.php');\n");
    echo("</script>\n");
    }else{
    echo "(Event Failed to be Added....)";    
    
    }
    }
    else
    {
        echo "error";
    }
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

你的意思是空文件还是没有文件?

如果有文件但大小为0 :(双等号)

if($_FILES['word']['size'] == 0)

如果没有上传文件,您可以写:

if(!isset($_FILES['word']))

注意:

1MB = 1字节* 1024 * 1024 = 1048576