我正在使用symfony框架并尝试将上传的文件移动到另一个目录,但move_uploaded_file
不是正常工作这是我的代码。我希望有人可以帮助我这个
public function addApplicantAction()
{
$em = $this->getDoctrine()->getEntityManager();
$em->getConnection()->beginTransaction();
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$contact_number = $_POST['contact_number'];
$email = $_POST['email'];
$position = $_POST['position'];
$job_id = $_POST['job_id'];
$status = "NO";
$timezone = "Asia/Manila";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$date_default = date('m/d/y',mktime(0,0,0,4,5,2010));
date("m/d/y");
$time = time();
$dats = mktime(0,0,0,date("m"),date("d"),date("Y"));
$date_applied=date("F j, Y ", $dats);
$upload_dir = $this->getRequest()->server->get('DOCUMENT_ROOT') . '/admin1/web/front/files';
$valid_extensions = array('doc', 'docx', 'pdf'); // valid extensions
$path = $upload_dir; // upload directory
if (isset($_FILES['file'])) {
$file = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
// check's valid format
if (in_array($ext, $valid_extensions)) {
$path = $path.strtolower($file);
if (move_uploaded_file($tmp,$path)) {
$add = new Applicants();
$add->setFirstName($first_name);
$add->setLastName($last_name);
$add->setContactNumber($contact_number);
$add->setEmail($email);
$add->setResume($file);
$add->setPosition($position);
$add->setStatus($status);
$add->setDateApplied($date_applied);
$add->setJobId($job_id);
$em->persist($add);
$em->flush();
$em->commit();
}
}
}
return new Response('success');
}
答案 0 :(得分:1)
您已经编写了一组PHP代码来处理您的请求数据。
如果您使用Symfony Framework
,则不应直接从$_POST
使用帖子数据。而是尝试使用Symfony Form Type并利用其优势,例如
回到文件上传问题,
enctype= multipart/form-data
代码中包含form
属性。如果您改变主意使用Symfony FormType
,请检查this docuemnt以了解如何在Symfony中上传文件。