如何在php中使用smarty上传多个文件?

时间:2014-03-11 13:26:16

标签: php smarty

我制作了一个选择框,我在其中选择不同的简历以应用于同一作业,但是当我应用作业时,它给出了我这样的错误“你已经应用”

这是我的apply_now.tpl:

{literal}
<script>
    function applySubmit() {
        $("#ApplicationForm").hide();
        $("#ProgressBar").show();
        $("#applyForm").ajaxSubmit({
            url: $("#applyForm").attr("action"),
            type: "POST",
            success: function (data) {
                $("#messageBox").html(data);
            }
    });
    return false;
}
$(document).ready(function() {
    $("#applyForm").jqTransform();
});
</script>
 {/literal}

div id="ProgressBar" style="display:none"><img src="{$GLOBALS.site_url}/system/ext/jquery/progbar.gif" alt="[[Please wait ...]]" />[[Please wait ...]]</div>

<div id="ApplicationForm">
{if $is_data_submitted && !$errors}
    <p class="message">[[You applied successfully]]</p>
{else}
    {foreach from=$errors key=error_code item=error_message}
            <p class="error">
                {if $error_code  eq 'EMPTY_VALUE'} [[Enter Security code]]
                {elseif $error_code eq 'NOT_VALID'} [[Security code is not valid]]
                {elseif $error_code eq 'SEND_ERROR'} [[There was an error while sending your application.]]
                {else}[[{$error_message}]]
                {/if}
            </p>
    {/foreach}
    {include file='field_errors.tpl'}
    <form method="POST" enctype="multipart/form-data" action="{$GLOBALS.site_url}/apply-now/" id="applyForm">
        <input type="hidden" name="is_data_submitted" value="1">
        <input type="hidden" name="listing_id" value="{$listing_id}">
        {if NOT $GLOBALS.current_user.logged_in}
            <fieldset>
                <div class="inputName">[[Your name]]:</div>
                <div class="inputField"><input type="text" name="name" value="{$request.name}" /></div>
            </fieldset>
            <fieldset>
                <div class="inputName">[[Your e-mail]]:</div>
                <div class="inputField"><input type="text" name="email" value="{$request.email}" /></div>
            </fieldset>
        {/if}
        <fieldset>
            <div class="inputName">[[Cover letter (optional)]]:</div>
            <div class="inputField"><textarea name="comments" rows="5">{$request.comments}</textarea></div>
        </fieldset>
        {if $GLOBALS.current_user.logged_in && $resume}
            <fieldset>
                <div class="inputName">[[Select your resume]]:</div>
                <div class="inputField">
                    <select name="id_resume" multiple="multiple">
                        <option value="0" selected="selected">[[Select your resume]]</option>
                        {html_options options=$resume selected=$request.id_resume}
                    </select>
                    <br />or
                </div>
            </fieldset>
        {/if}
        <fieldset>
            <div class="inputName">[[Attach your resume]]:</div>
            <div class="inputField"><input type="file" name="file_tmp" /></div>
        </fieldset>
        {if $isCaptcha == 1}
            <fieldset>
                <div class="inputName">[[$captcha.caption]]:</div>
                <div class="inputField">{input property=$captcha.id object=$captchaObject}</div>
            </fieldset>
        {/if}
        <input type="hidden" name="anonymous" value="1" />
        {if $form_fields}
            <fieldset>
                {include file="questionnaire.tpl" form_fields=$form_fields}
            </fieldset>
        {/if}
        <fieldset>
            <div class="inputName">&nbsp;</div>
            <div class="inputButton"><input id="SubmitButton" type="submit" value="[[Send]]" onclick="return applySubmit();"/></div>
        </fieldset>
    </form>
{/if}
 </div>

这是我的apply_now.php代码:

 $listingId = 0;
                $post['submitted_data']['questionnaire'] = '';
                if (isset($post['submitted_data']['id_resume']))
                    $listingId = $post['submitted_data']['id_resume'];

                $mimeType = isset($_FILES['file_tmp']['type']) ? $_FILES['file_tmp']['type'] : '';

                if (isset($_FILES['file_tmp']['size']) && $file_name != '' && $_FILES['file_tmp']['size'] == 0) {
                    $errors['FILE_IS_EMPTY'] = 'The uploaded file should not be blank';
                }

                if (!empty($_FILES['file_tmp']['name'])){
                    $fileFormats = explode(',',SJB_System::getSettingByName('file_valid_types'));
                    $fileInfo = pathinfo($_FILES['file_tmp']['name']);
                    if (!in_array(strtolower($fileInfo['extension']), $fileFormats)) {
                        $errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
                    }
                }

                if ($file_name == '' && $listingId == 0) {
                    $canAppplyWithoutResume = false;
                    SJB_Event::dispatch('CanApplyWithoutResume', $canAppplyWithoutResume);
                    if (!$canAppplyWithoutResume) {
                        $errors['APPLY_INPUT_ERROR'] = 'Please select file or resume';
                    }
                }
                else if (SJB_Applications::isApplied($post['submitted_data']['listing_id'], $current_user_sid) && !is_null($current_user_sid)) {
                    $errors['APPLY_APPLIED_ERROR'] = 'You already applied';
                }

 if (count($errors) == 0 && count($field_errors) == 0) {
                    $res = SJB_Applications::create(
                        $post['submitted_data']['listing_id'],
                        $current_user_sid,
                        (isset($post['submitted_data']['id_resume'])) ? $post['submitted_data']['id_resume'] : '',
                        $post['submitted_data']['comments'],
                        $file_name,
                        $mimeType,
                        $id_file,
                        (isset($post['submitted_data']['anonymous'])) ? $post['submitted_data']['anonymous'] : '0',
                        $notRegisterUserData,
                        $post['submitted_data']['questionnaire'],
                        $score
                    );
                    if ($res)
                        SJB_Statistics::addStatistics('apply', $post['submitted_data']['listing_id'], $res);
                    if (isset($post['submitted_data']['id_resume']) && $post['submitted_data']['id_resume'] != 0) {
                        $listing_info = SJB_ListingManager::getListingInfoBySID($post['submitted_data']['id_resume']);
                        $emp_sid = SJB_ListingManager::getUserSIDByListingSID($post['submitted_data']['listing_id']);
                        $accessible = SJB_ListingManager::isListingAccessableByUser($post['submitted_data']['id_resume'], $emp_sid);
                        if (!$accessible)
                            SJB_ListingManager::setListingAccessibleToUser($post['submitted_data']['id_resume'], $emp_sid);
                    }

我如何选择多个简历然后应用?

1 个答案:

答案 0 :(得分:-1)

使用此.. ..

<input type="file" name="file_tmp[]" multiple /> .. 这会给你一个array of files

 for($i=0; $i < count($_FILES['file_tmp']['tmp_name']);$i++)
    {
        // check if there is a file in the array
        if(!is_uploaded_file($_FILES['file_tmp']['tmp_name'][$i]))
        {
            $messages[] = 'No file uploaded';
        }
        else
        {
            // copy the file to the specified dir 
            if(@copy($_FILES['file_tmp']['tmp_name'][$i],$upload_dir.'/'.$_FILES['file_tmp']['name'][$i]))
            {
                $messages[] = $_FILES['file_tmp']['name'][$i].' uploaded';
            }
            else
            {
                /*** an error message ***/
                $messages[] = 'Uploading '.$_FILES['file_tmp']['name'][$i].' Failed';
            }
        }
    }

如果您需要任何进一步的指导,请告诉我......