我创建了一个php页面,其中我获取了files array和count数组元素,我想应用循环和循环工作,直到计数如果count = 3,然后代码运行3次并上传3个文件。
这是我的代码:
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[]" multiple /></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"> </div>
<div class="inputButton"><input id="SubmitButton" type="submit" value="[[Send]]" onclick="return applySubmit();"/></div>
</fieldset>
</form>
{/if}
</div>
这是我的apply_now.php文件代码:
<?php
class SJB_Classifieds_ApplyNow extends SJB_Function
{
public function isAccessible()
{
$this->setPermissionLabel('apply_for_a_job');
return parent::isAccessible();
}
public function execute()
{
$errors = array();
$field_errors = array();
$tp = SJB_System::getTemplateProcessor();
$loggedIn = SJB_UserManager::isUserLoggedIn();
$current_user_sid = SJB_UserManager::getCurrentUserSID();
$controller = new SJB_SendListingInfoController($_REQUEST);
$isDataSubmitted = false;
$isCaptcha = 0;
if (SJB_PluginManager::isPluginActive('CaptchaPlugin') && SJB_System::getSettingByName('contactUserCaptcha') == 1) {
$isCaptcha = 1;
}
$captcha = new SJB_Captcha($_REQUEST, 'modal');
$captcha_form = SJB_ObjectMother::createForm($captcha);
$captcha_form->registerTags($tp);
$tp->assign('captcha', array_pop($captcha_form->form_fields));
$tp->assign('captchaObject', $captcha);
$jobInfo = SJB_ListingManager::getListingInfoBySID($controller->getListingID());
if ($controller->isListingSpecified()) {
if ($controller->isDataSubmitted()) {
$captcha_errors = array();
if ($isCaptcha && !$captcha_form->isDataValid($captcha_errors)) {
foreach ($captcha_errors as $error)
$errors[$error] = true;
}
else {
// получим уникальный id для файла в uploaded_files
$file_id_current = 'application_' . md5(microtime());
$upload_manager = new SJB_UploadFileManager();
$upload_manager->setFileGroup('files');
$upload_manager->setUploadedFileID($file_id_current);
for($i = 0; $i < count($_FILES['file_tmp']['name']); $i++){
$file_name = $upload_manager->uploadFile($_FILES['file_tmp']['name'][$i]);
}
$id_file = mysql_insert_id();
$post = $controller->getData();
$listingId = 0;
$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'));
foreach ( $_FILES['file_tmp']['name'] as $key => $file ) {
$fileInfo = pathinfo($_FILES['file_tmp']['name'][$key]);
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';
}
$res = false;
$listing_info = '';
$notRegisterUserData = $_POST;
$score = 0;
// для зарегестрированного пользователя получим поля email и name
// для незарегестрированных - поля name и email приходят с формы
if(!empty($file_name))
$file_name = 'files/files/'. $file_name;
if (!SJB_Notifications::sendApplyNow($post, $file_name, $listing_info, $current_user_sid, $notRegisterUserData, $score))
$errors['SEND_ERROR'] = true;
$tp->assign('request', $_REQUEST);
$tp->assign('isCaptcha', $isCaptcha);
$tp->assign('errors', $errors);
$tp->assign('listing_id', $controller->getListingID());
$tp->assign('is_data_submitted', $isDataSubmitted);
$tp->display('apply_now.tpl');
}
}
先谢谢
答案 0 :(得分:1)
如果您在html中的multiple
标记中添加<input>
属性,那么您的文件将在PHP $_FILES["file_tmp"]
下以数组形式提供。
<input type="file" accept="image/*" name="file_tmp[]" multiple>
for($i = 0; $i < count($_FILES["file_tmp"]["name"]); $i++){
//your upload code
//You can access your file as following:
$filename = $_FILES["file_tmp"]['tmp_name'][$i];
}
将来,请不要发布您的整个代码,而只发布相关的代码段。没有人想要阅读所有代码只是为了找到你的问题。
答案 1 :(得分:1)
example.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="my_parser.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<input type="submit" value="Upload all files">
</form>
</body>
</html>
my_parser.php
<?php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])){
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
?>
答案 2 :(得分:1)
上述代码略有变化..
for($i = 0; $i < count($_FILES["file_tmp"]["name"]); $i++){
$filename[] = $_FILES["file_tmp"]['tmp_name'][$i];
}