表单有没有上传文件(php)

时间:2016-01-26 06:14:55

标签: php

需要你的帮助。 情况如下。我有简单的形式与一些输入。 用户填写表格,可以上传或不上传他的标志。但是在这段代码中,当我按下没有徽标的提交按钮时出现错误。所以,问题是我如何解决这个问题,并允许用户提交和不上传文件?很多人感谢!!

<?
    if(isset($_POST['add'])){    
    $title = sf($_POST['title']);
    $company = sf($_POST['company']);
    $city = sf($_POST['city']);

    $path = '../img/logo';
    // Get extension
    $extension = strtolower(substr(strrchr($_FILES['logo']['name'], '.'), 1));

    $blacklist = array(".php", ".phtml", ".php3", ".php4", ".html", ".htm");
    foreach ($blacklist as $item)
    if(preg_match("/$item\$/i", $_FILES['logo']['name'])) exit;
    $type = $_FILES['logo']['type'];
    $size = $_FILES['logo']['size'];
    $filename = DFileHelper::getRandomFileName($path, $extension);
    $target = $path . '/' . $filename . '.' . $extension;
    if (($type != "image/jpg") && ($type != "image/jpeg")) exit; 
    if ($size > 10485760) exit;

    move_uploaded_file($_FILES['logo']['tmp_name'], $target);

    $jb = $mysql->prepare("INSERT INTO tb_job (j_title, j_company, j_city, j_logo) VALUES (?, ?, ?, ?)");
    $jb->execute(array($title, $compnay, $city, $filename,));

    echo 'OK';
    Header("Refresh: 2, /jobs/cv");
    }

    ?>

 <form enctype="multipart/form-data" method="post" action="">              
    <input type="text" name="title" />
    <input type="text" name="compnay" />
    <input type="text" name="city" />
    <input type="file" name="logo" />
    <input type="submit" value="OK" />
</form>

2 个答案:

答案 0 :(得分:0)

如果表单有徽标

,您应该设置一个条件
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }

    @Override
    protected WebApplicationContext createServletApplicationContext() {
        XmlWebApplicationContext cxt = new XmlWebApplicationContext();
        cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
        return cxt;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

答案 1 :(得分:0)

if (isset($_FILES["example"])){
    echo true; 
}else{
    echo false;
}