我正在尝试通过简单的表单上传文件,然后将其发布到我的控制器并将其移动到服务器中的某个目录,但文件未发布,这是我的代码:
<form id="loginForm" method="post" class="form-horizontal" style="display: none;" action="<?php echo Yii::app()->createUrl('jobs/view', array('id'=>$model->idJob)); ?>">
<div class="form-group">
<label class="col-md-3 control-label">Full Name</label>
<div class="col-md-5">
<input type="text" class="form-control" name="fullName" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Email</label>
<div class="col-md-5">
<input type="email" class="form-control" name="email" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Phone Number</label>
<div class="col-md-5">
<input type="text" class="form-control" name="phoneNumber" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Upload your CV</label>
<div class="col-md-5">
<input type="file" class="form-control" name="cv" id="cv" />
</div>
</div>
<div class="form-group">
<div class="col-md-5 col-md-offset-3">
<button type="submit" class="btn btn-default">submit</button>
</div>
</div>
</form>
if(isset($_POST['fullName'])&&isset($_POST['email'])&&isset($_POST['phoneNumber'])){
if ($_FILES["cv"]["error"] > 0) {
echo "Error: " . $_FILES["cv"]["error"] . "<br>";
}
else {
echo "Upload: " . $_FILES["cv"]["name"] . "<br>";
echo "Type: " . $_FILES["cv"]["type"] . "<br>";
echo "Size: " . ($_FILES["cv"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["cv"]["tmp_name"];
}
$uploaddir = getcwd()."/CVs/jobsApply/";
$uploadfile = $uploaddir . $_FILES['cv']['name'];
echo '<pre>';
if (move_uploaded_file($_FILES['cv']['name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
$jobs = Jobs::model()->find('idJob='.$job);
$to = 'jobs@site.com';
$subject = 'Job Applying';
$headers = "From: " . strip_tags($_POST['fullName']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "Phone Number: ". strip_tags($_POST['phoneNumber']) . "\r\n";
$headers .= "cv: ". strip_tags($_POST['cv']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$url2 = $src."/CVs/jobsApply/".$model->fullName."-".$model->email."-".$model->cv;
$message = '<html><body>';
$message .= '<h3>'.$headers.'<br>Job reference: '.$jobs->reference.'</h3><br><h3>Candidate cv: <a href="site.com/CVs/jobsApply/'.$_POST['fullName']."-".$_POST['email']."-".$model->cv.'")Download CV</a></h3>';
$message .= '</body></html>';
$flgSend = @mail($to, $subject, $message);
那么我为什么不在这里收到文件呢?
答案 0 :(得分:6)
您必须将此部分添加到表单标题中:enctype="multipart/form-data"
。在您的表单中发布二进制数据时需要它,例如上传的文件。所以你的代码的第一行看起来像是:
<form id="loginForm" method="post" class="form-horizontal" style="display: none;" action="<?php echo Yii::app()->createUrl('jobs/view', array('id'=>$model->idJob)); ?>" enctype="multipart/form-data">
答案 1 :(得分:1)
您需要逐步调试
首先检查是否设置了帖子值:
发出回声“作品”;或类似if(isset($ _ POST ['fullName']等行
如果这样可以放一个var_dump($ _ FILES);
看看你从那里得到什么
如果这个数组中有错误,请查看这里的含义: http://php.net/manual/en/features.file-upload.errors.php
尝试这些事情,看看它“停止工作”之前,任何人都可以真正帮助你,我会说,因为这里有一些遗漏的信息。
答案 2 :(得分:0)
尝试在表单声明中添加enctype="multipart/form-data"
。
请参阅POST method uploads,了解更多有用信息,了解您要实现的目标。