Category Archives: Prado Framework

Prado Framework : Upload Picture


Form preview with TActiveFileUpload

upload_picture_prado

 

 

 

 

 

 

test.page:

Code:
<com:TActiveFileUpload ID="actUpload" MaxFileSize="102400" OnFileUpload="onImgUpload" />
<com:TActiveImage ID="labPhoto />
<com:TActiveLabel ID="labError />
test.php:

Code:
<?php

public function onImgUpload($sender, $param)
{
	if(!extension_loaded('gd'))
	{
		$this->labError->Text="Error: missing php's gd extension.";
		return;
	}

	if (!$sender->getHasFile())
	{
		//error handling
		switch ($sender->ErrorCode)
		{
			case 1:
				$err="file size too big (php.ini).";
				break;
			case 2:
				$err="file size too big (form).";
				break;
			case 3:
				$err="file upload interrupted.";
				break;
			case 4:
				$err="no file chosen.";
				break;
			case 6:
				$err="internal problem (missing temporary directory).";
				break;
			case 7:
				$err="unable to write file on disk.";
				break;
			case 8:
				$err="file type not accepted.";
				break;
		}

		$this->labError->Text="Error: ".$err;
		return;
	}

	$mime=$sender->getFileType();
	if($mime!="image/png" && $mime!="image/jpg" && $mime!="image/jpeg")
	{
		$this->labError->Text="Error: the file is not an image";
		return;
	}

	$source=false;

	if($mime=="image/png")
	{
		if(!(imagetypes() & IMG_PNG))
		{
			$this->labError->Text="Error: missing png support in gd library.";
			return;
		}
		$source = imagecreatefrompng($sender->LocalName);
	}

	if(($mime=="image/jpg" || $mime=="image/jpeg"))
	{
		if(!(imagetypes() & IMG_JPG))
		{
			$this->labError->Text="Error: missing jpeg support in gd library.";
			return;
		}
		$source = imagecreatefromjpeg($sender->LocalName);
	}

	if(!$source)
	{
		$this->labError->Text="Errore: invalid file.");
		return;
	}

	list($width_orig, $height_orig) = getimagesize($sender->LocalName);
	$width = 80;
	$height = 80;

	$thumb = imagecreatetruecolor($width, $height);

	// Get new dimensions

	$ratio_orig = $width_orig/$height_orig;

	if ($width/$height > $ratio_orig) {
		$width = $height*$ratio_orig;
	} else {
		$height = $width/$ratio_orig;
	}
	imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

	// use output buffering to capture outputted image stream
	ob_start();
	imagejpeg($thumb);
	$thumbjpg = ob_get_clean();

	$this->labPhoto->ImageUrl = "data:image/jpeg;base64,".base64_encode($thumbjpg);

            $new_filename = $sender->FileName;
            $path = "/opt/lampp/htdocs/your_app/protected/Pages/app/img/".$new_filename;
            // to save file into drive/folder
            if($sender->saveAs($path,false)){
                $this->photo_url->Text = $path;
            }
}

[Notice] A session had already been started – ignoring session_start()


How to resolve this prado framwork error message ?

[Notice] A session had already been started – ignoring session_start() (@line 134 in file /opt/lampp/htdocs/your_app/prado/framework/Web/THttpSession.php).

You can fix with. Firstly you can open existing “THttpSession.php” file and check line 134 (in my case).

As you can see, red script causing that error above. So you can remove and replace with green script.

<?

public function open()
{
if(!$this->_started)
{
if($this->_customStorage)
session_set_save_handler(array($this,’_open’),array($this,’_close’),array($this,’_read’),array($this,’_write’),array($this,’_destroy’),array($this,’_gc’));
if($this->_cookie!==null)
session_set_cookie_params($this->_cookie->getExpire(),$this->_cookie->getPath(),$this->_cookie->getDomain(),$this->_cookie->getSecure());
            if(ini_get(‘session.auto_start’)!==’1′)
                session_start();

            if(headers_sent()){ session_start(); }

// if(!isset($SESSION)) { ob_start(); session_start();}
$this->_started=true;
}
}

?>

Hope this helpfully.

Blackphp