Symfony - Form File Upload [temp to another server using ftp]
Friday, May 25, 2012
class PhotoForm extends BasePhotoForm
{
public function configure()
{
$this->setWidgets(array(
'photo' => new sfWidgetFormInputFile(array('label' => 'Зураг')),
));
$this->setValidators(array(
'photo' => new sfValidatorFile(
array(
'required' => false,
'max_size' => 5242880,
'path' => sfConfig::get('sf_upload_dir'),
'mime_types' => array(
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
),
'validated_file_class' => 'sfValidatedFileLocations'
),
array(
'required' => 'Та зураг оруулна уу',
'max_size' => 'Таны оруулсан зурагны хэмжээ иx байна. Хамгийн иxдээ 5MB.',
'mime_types' => 'Та зөвxөн зурган файл оруулаx боломжтой')),
));
$this->widgetSchema['photo'] = new sfWidgetFormInputFileEditable(array(
'label' => 'Зураг',
'file_src' => FtpHandler::FTPFOLDER . 'files/' . $this->getObject()->getPhoto(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
), array('width' => 500));
$this->widgetSchema->setNameFormat('photo[%s]');
}
}
...
class sfMyValidatedFile extends sfValidatedFile
{
public function save()
{
$newName = $this->generateFilename();
$ftp = new FtpHandler();
$ftp->transfer('files/folderimg', $this->getTempName(), $newName);
return $newName;
}
}
...
class FtpHandler
{
const FTPFOLDER = 'http://content/';
private $_IP = 'IP';
private $_PORT = 21;
private $_USERNAME = 'USERNAME';
private $_PASSWORD = 'PASSWORD';
private $_FTPPATH = '/FOLDERNAME/';
private $_ERROR = '';
private $_ERROR_CONNECT = 'Content сервер лүү холбогдох боломжгүй байна!';
private $_ERROR_LOGIN = 'Content сервер лүү нэвтрэх үед алдаа гарлаа!';
private $_ERROR_PUT = 'Content cервер лүү файл хуулах үед алдаа гарлаа!';
public function __construct($path = 'empty')
{ if ($path != 'empty') {
$this->_FTPPATH = $path;
}
}
public function transfer($folder, $localFile, $fileName)
{ $this->setPath($folder, $fileName);
$ftp = $this->ftpConnect();
if ($ftp) {
$login = $this->ftpLogin($ftp);
if ($login) {
$upload = $this->ftpPut($ftp, $localFile);
if (!$upload) {
$this->_ERROR = $this->_ERROR_PUT;
}
} else {
$this->_ERROR = $this->_ERROR_LOGIN;
}
ftp_close($ftp);
} else {
$this->_ERROR = $this->_ERROR_CONNECT;
}
return $this->_ERROR;
}
public function ftpConnect()
{ return ftp_connect($this->_IP, $this->_PORT);
}
public function ftpLogin($ftp)
{ return ftp_login($ftp, $this->_USERNAME, $this->_PASSWORD);
}
public function ftpPut($login, $localFile)
{ return ftp_put($login, $this->_FTPPATH, $localFile, FTP_BINARY);
}
public function delete($folder, $file)
{ $file = $this->_FTPPATH . $folder . '/' . $file;
$ftp = $this->ftpConnect();
$login = $this->ftpLogin($ftp);
$b = false;
if ($ftp) {
if ($login) {
$b = ftp_delete($ftp, $file);
}
ftp_close($ftp);
}
return $b;
}
public function fileSize($folder, $file)
{ $file = $this->_FTPPATH . $folder . '/' . $file;
$ftp = $this->ftpConnect();
$login = $this->ftpLogin($ftp);
$b = -1;
if ($ftp) {
if ($login) {
$b = ftp_size($ftp, $file);
}
ftp_close($ftp);
}
return $b;
}
public function setPath($folder, $fileName)
{ $this->_FTPPATH = $this->_FTPPATH . $folder . '/' . $fileName;
}
}
0 comments:
Post a Comment