文件不通过PHP上传

时间:2014-09-20 02:30:09

标签: php

我有一个脚本,它去年提交给自己。我不得不重做它,以便提交在自己的页面上,但文件根本没有上传!该脚本没有产生任何错误,所以我似乎无法弄清楚问题是什么。这是脚本的相关部分(删除了额外的代码):

<?php

if (isset($_POST['title']) && !empty($_POST['title'])) {

    // Set POST as variables
    $title = $_POST['title'];
    $name = $_POST['name'];
    $email = $_POST['email'];

    $time = time();
    $resumefilename = "";

    $rfilename = strtolower($_FILES['resume']['name']);
    $rfiletype = strtolower($_FILES['resume']['type']);
    $ruploadname = str_replace(" ", "-", trim($name)) . "-" . str_replace( " ", "-", trim($title)) . "-resume";
    $ruploadname = preg_replace('/[^a-zA-Z0-9_ -]/s', '', $ruploadname);
    $ruploadname = strtolower($ruploadname); 

    // Folder where images will be stored
    $uploaddir = 'uploads/';

    ///////////////////////////////////////////////////////////////////////////
    // SECURITY - Checks if file contains PHP and kills if positive
        $rpos = strpos($rfilename, 'php');
        if(!($rpos === false)) { die('Error, Bad File Type'); }
    ///////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////
    // Resume File Check
        $file_ext = strrchr($rfilename, '.');
        $allowedExts = array(
            "pdf", 
            "doc", 
            "docx"
        ); 
        $allowedMimeTypes = array( 
            'text/pdf',
            'application/pdf',
            'application/msword',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        );
        if (!(in_array($file_ext, $allowedExts))) {
            $error .= '<p>Invalid file type detected for Resume: please upload pdf, doc, or docx files only. Thank you.</p>';
        }

        if(in_array($_FILES["resume"]["type"], $allowedMimeTypes)){  
            $ruploadfile = $uploaddir.$time.'-'.$ruploadname.$file_ext;
            $resumefilename = $time.'-'.$ruploadname.$file_ext;
            $remailname = $ruploadname.$file_ext;

            if (move_uploaded_file($_FILES['resume']['tmp_name'], $ruploadfile)) {
                $resumesuccess = 1;
            } else {
                $resumesuccess = 0;
            }
        }
    ///////////////////////////////////////////////////////////////////////////
}

&GT;

2 个答案:

答案 0 :(得分:1)

试试这个

<form method="POST" enctype="multipart/form-data" action="your file">

所有其他代码

</form>

答案 1 :(得分:0)

你可以试试这个

if (isset($_POST['title']) && !empty($_POST['title'])) {
// Do something
} else {
   echo '<form method="POST" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">';
   <!-- Field sets -->
   echo '</form>';
}