我的文件没有在我的电脑上传,并且数据库中没有更新profileFilePath ...我不知道有什么问题。我希望你能帮助我!
我的User.php。
<?php
namespace L3O1\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\Util\SecureRandom;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="l3o1_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Assert\File(maxSize="2048k")
* @Assert\Image(mimeTypesMessage="Please upload a valid image.")
*/
protected $profilePictureFile;
// for temporary storage
private $tempProfilePicturePath;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $profilePicturePath;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Sets the file used for profile picture uploads
*
* @param UploadedFile $file
* @return object
*/
public function setProfilePictureFile(UploadedFile $file = null) {
// set the value of the holder
$this->profilePictureFile = $file;
// check if we have an old image path
if (isset($this->profilePicturePath)) {
// store the old name to delete after the update
$this->tempProfilePicturePath = $this->profilePicturePath;
$this->profilePicturePath = null;
} else {
$this->profilePicturePath = 'initial';
}
return $this;
}
/**
* Get the file used for profile picture uploads
*
* @return UploadedFile
*/
public function getProfilePictureFile() {
return $this->profilePictureFile;
}
/**
* Set profilePicturePath
*
* @param string $profilePicturePath
* @return User
*/
public function setProfilePicturePath($profilePicturePath)
{
$this->profilePicturePath = $profilePicturePath;
return $this;
}
/**
* Get profilePicturePath
*
* @return string
*/
public function getProfilePicturePath()
{
return $this->profilePicturePath;
}
/**
* Get the absolute path of the profilePicturePath
*/
public function getProfilePictureAbsolutePath() {
return null === $this->profilePicturePath
? null
: $this->getUploadRootDir().'/'.$this->profilePicturePath;
}
/**
* Get root directory for file uploads
*
* @return string
*/
protected function getUploadRootDir($type='profilePicture') {
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir($type);
}
/**
* Specifies where in the /web directory profile pic uploads are stored
*
* @return string
*/
protected function getUploadDir($type='profilePicture') {
// the type param is to change these methods at a later date for more file uploads
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/user/profilepics';
}
/**
* Get the web path for the user
*
* @return string
*/
public function getWebProfilePicturePath() {
return '/'.$this->getUploadDir().'/'.$this->getProfilePicturePath();
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUploadProfilePicture() {
if (null !== $this->getProfilePictureFile()) {
// a file was uploaded
// generate a unique filename
$filename = $this->generateRandomProfilePictureFilename();
$this->setProfilePicturePath($filename.'.'.$this->getProfilePictureFile()->guessExtension());
}
}
/**
* Generates a 32 char long random filename
*
* @return string
*/
public function generateRandomProfilePictureFilename() {
$count = 0;
do {
$generator = new SecureRandom();
$random = $generator->nextBytes(16);
$randomString = bin2hex($random);
$count++;
}
while(file_exists($this->getUploadRootDir().'/'.$randomString.'.'.$this->getProfilePictureFile()->guessExtension()) && $count < 50);
return $randomString;
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*
* Upload the profile picture
*
* @return mixed
*/
public function uploadProfilePicture() {
// check there is a profile pic to upload
if ($this->getProfilePictureFile() === null) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getProfilePictureFile()->move($this->getUploadRootDir(), $this->getProfilePicturePath());
// check if we have an old image
if (isset($this->tempProfilePicturePath) && file_exists($this->getUploadRootDir().'/'.$this->tempProfilePicturePath)) {
// delete the old image
unlink($this->getUploadRootDir().'/'.$this->tempProfilePicturePath);
// clear the temp image path
$this->tempProfilePicturePath = null;
}
$this->profilePictureFile = null;
}
/**
* @ORM\PostRemove()
*/
public function removeProfilePictureFile()
{
if ($file = $this->getProfilePictureAbsolutePath() && file_exists($this->getProfilePictureAbsolutePath())) {
unlink($file);
}
}
}
我的表单类型
<?php
namespace L3O1\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ProfileFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// add your custom field
$builder->add('profilePictureFile');
}
public function getParent()
{
return 'fos_user_profile';
}
public function getName()
{
return 'l3o1_user_profile';
}
}
我的编辑在edit_content.html.twig
中 <form action="{{ path('fos_user_profile_edit') }}" method="POST" {{ form_enctype(form) }} class="fos_user_profile_edit">
<!-- left column -->
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="text-center">
<h6>Upload a different photo...</h6>
<img src="/ProjetSVN/web{{app.user.getWebProfilePicturePath}}" class="avatar img-circle img-thumbnail" alt="avatar">
{{ form_row(form.profilePictureFile, {'label': 'Profile picture'}) }}
{{ form_widget(form.profilePictureFile, {'attr': {'class': 'form-control' }})}}
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input type="submit" class="btn btn-primary" value="Save change" type="button">
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
感谢您的帮助!
答案 0 :(得分:0)
我解决了这个问题。这是因为在我的树枝上,我用{{ form_widget(form.file) }}
而不是{{ form_widget(form) }}
!但我不确定为什么会这样理解。看来,通过FOSUB,当您更新个人资料时,您必须输入密码进行验证。这就是为什么,我认为它不会上传或更新数据库。