WebSite-PHP Framework PHP
Multi language
Simple Ajax mode
No HTML, no JavaScript
URL rewriting
Mail sending
Sitemap - RSS - Web service
Download WebSite-PHP FrameWork now
 


Loading
 


Tutorial : Upload a file

Tutorials

>

Tutorial Uploadfile

>

Tutorial : Upload a file


Uploadfile-01: Tutorial of the composant Uploadfile of the PHP FrameWork WebSite-PHP.

Tutorial : Upload a file:




Go back to tutorial of the componant Uploadfile



Source code

Source code


File: /pages/tutorials/uploadfile/uploadfile-01.php

<?php
class Uploadfile01 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload a file";
        
        
$form = new Form($this);
        
        
$form_obj = new WSPObject();
        
$form_obj->setAlign(WSPObject::ALIGN_CENTER);
        
        
// it's not possible in a Tabs or DialogBox to use
        // button to upload a file.
        // To check if we are in the case of Tab we init
        // variable used bellow in the code
        
$isTabs $this->isAjaxLoadPage() || 
                    
$this->isAjaxPage();
        
        
// create upload file
        
$this->upload = new UploadFile(
                            (
$isTabs?$this:$form));
        
        
// define mime type authorized 
        
$this->upload->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->upload->setFileSizeLimit("300kb");
        
        
$form_obj->add($this->upload);
        
        if (!
$isTabs) {
            
// We will display the button only in a classic page
            
$btn = new Button($form);
            
$btn->setValue("Upload")->onClick("onClick");
            
$form_obj->add("<br/>"$btn);
            
            
$form->setContent($form_obj);
            
$this->render $form;
        } else {
            
// We are in the Tabs and we need to set ajax
            // event to upload a file
            
$this->upload->setAjaxEvent();
            
$this->upload->onChange("onClick");
            
            
// Display message to explain
            
$link = new Link("tutorials/uploadfile/uploadfile-01.html"
                            
Link::TARGET_NONE
                            
"tutorials/uploadfile/uploadfile-01.html");
            
$form_obj->add("<br/><br/>To use UploadFile in a Tab, it's ".
                
"necessary to use ajax event.<br/>Please go on the page "
                
$link" to test without ajax event.");
            
            
$this->render $form_obj;
        }
    }
    
    public function 
onClick($sender) {
        
// check the file is correctly uploaded
        
if ($this->upload->checkMimeType() && 
            
$this->upload->checkFileSize()) {
                
alert("File ".$this->upload->getFileName()." (".
                    
$this->upload->getFileSize().") uploaded !");
        } else if (
$this->upload->isEmptyFile()) {
            
alert("Your file is empty !");
        } else if (!
$this->upload->checkFileSize()) {
            
alert("Your file is too big !");
        } else {
            
alert("Mime type ".$this->upload->getFileMimeType().
                    
" not supported !");
        }
    }
}
?>


Go back to tutorial of the componant Uploadfile


Share

 


Copyright © 2009-2024 WebSite-PHP Framework PHP
Home Documentation Download Quick start Tutorials Wiki Issue Tracker