Results 1 to 2 of 2

Thread: upload script page refresh

  1. #1
    Join Date
    Oct 2011
    Location
    London
    Posts
    41
    Thanks
    19
    Thanked 1 Time in 1 Post

    Red face upload script page refresh

    Hi guys sorry if this should be in the javascript section i wasn't shore so here goes....
    i have this upload script with i have modded a bit for my own need but i need to refresh the (HTML) page once the upload has happend here are the codes

    ok the html
    PHP Code:
    <head>
    <
    script src="../js/fileuploader.js" type="text/javascript"></script>
        <script>        
            function createUploader(){            
                var uploader = new qq.FileUploader({
                    element: document.getElementById('file-uploader'),
                    action: 'php.php',
                  });           
            }
            // in your app create uploader as soon as the DOM is ready
            // don't wait for the window to load  
            window.onload = createUploader;     
        </script>  
    </head>
    <body>
    <div id="file-uploader">    
             
            <noscript>            
                <p>Please enable JavaScript to use file uploader.</p>
                <!-- or put a simple form for upload here -->
            </noscript>         
    </div> 
    now the php
    PHP Code:
    <?php session_start();
    include(
    "../config.php");
    /**
     * Handle file uploads via XMLHttpRequest
     */
    class qqUploadedFileXhr {
        
    /**
         * Save the file to the specified path
         * @return boolean TRUE on success
         */
        
    function save($path) {    
            
    $input fopen("php://input""r");
            
    $temp tmpfile();
            
    $realSize stream_copy_to_stream($input$temp);
            
    fclose($input);
            
            if (
    $realSize != $this->getSize()){            
                return 
    false;
            }
            
            
    $target fopen($path"w");        
            
    fseek($temp0SEEK_SET);
            
    stream_copy_to_stream($temp$target);
            
    fclose($target);
            
            return 
    true;
        }
        function 
    getName() {
            return 
    $_GET['qqfile'];
        }
        function 
    getSize() {
            if (isset(
    $_SERVER["CONTENT_LENGTH"])){
                return (int)
    $_SERVER["CONTENT_LENGTH"];            
            } else {
                throw new 
    Exception('Getting content length is not supported.');
            }      
        }   
    }

    /**
     * Handle file uploads via regular form post (uses the $_FILES array)
     */
    class qqUploadedFileForm {  
        
    /**
         * Save the file to the specified path
         * @return boolean TRUE on success
         */
        
    function save($path) {
            if(!
    move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
                return 
    false;
            }
            return 
    true;
        }
        function 
    getName() {
            return 
    $_FILES['qqfile']['name'];
        }
        function 
    getSize() {
            return 
    $_FILES['qqfile']['size'];
        }
    }

    class 
    qqFileUploader {
        private 
    $allowedExtensions = array();
        private 
    $sizeLimit 10485760;
        private 
    $file;

        function 
    __construct(array $allowedExtensions = array(), $sizeLimit 10485760){        
            
    $allowedExtensions array_map("strtolower"$allowedExtensions);
                
            
    $this->allowedExtensions $allowedExtensions;        
            
    $this->sizeLimit $sizeLimit;
            
            
    $this->checkServerSettings();       

            if (isset(
    $_GET['qqfile'])) {
                
    $this->file = new qqUploadedFileXhr();
            } elseif (isset(
    $_FILES['qqfile'])) {
                
    $this->file = new qqUploadedFileForm();
            } else {
                
    $this->file false
            }
        }
        
        private function 
    checkServerSettings(){        
            
    $postSize $this->toBytes(ini_get('post_max_size'));
            
    $uploadSize $this->toBytes(ini_get('upload_max_filesize'));        
            
            if (
    $postSize $this->sizeLimit || $uploadSize $this->sizeLimit){
                
    $size max(1$this->sizeLimit 1024 1024) . 'M';             
                die(
    "{'error':'increase post_max_size and upload_max_filesize to $size'}");    
            }        
        }
        
        private function 
    toBytes($str){
            
    $val trim($str);
            
    $last strtolower($str[strlen($str)-1]);
            switch(
    $last) {
                case 
    'g'$val *= 1024;
                case 
    'm'$val *= 1024;
                case 
    'k'$val *= 1024;        
            }
            return 
    $val;
        }
        
        
    /**
         * Returns array('success'=>true) or array('error'=>'error message')
         */
        
    function handleUpload($uploadDirectory$replaceOldFile FALSE){
            if (!
    is_writable($uploadDirectory)){
                return array(
    'error' => "Server error. Upload directory isn't writable.");
            }
            
            if (!
    $this->file){
                return array(
    'error' => 'No files were uploaded.');
            }
            
            
    $size $this->file->getSize();
            
            if (
    $size == 0) {
                return array(
    'error' => 'File is empty');
            }
            
            if (
    $size $this->sizeLimit) {
                return array(
    'error' => 'File is too large');
            }
            
            
    $pathinfo pathinfo($this->file->getName());
            
    $filename $pathinfo['filename'];
            
    //$filename = md5(uniqid());
            
    $ext $pathinfo['extension'];

            if(
    $this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
                
    $these implode(', '$this->allowedExtensions);
                return array(
    'error' => 'File has an invalid extension, it should be one of '$these '.');
            }
            
            if(!
    $replaceOldFile){
                
    /// don't overwrite previous files that were uploaded
                
    while (file_exists($uploadDirectory $filename '.' $ext)) {
                    
    $filename .= rand(1099);
                }
            }
            
            if (
    $this->file->save($uploadDirectory $filename '.' $ext)){
            
    // INPUT DATA INTO MYSQL
            
    $filenom $filename '.' $ext;
            
    $usernom $_SESSION['SESS_USER_NAME'];
            
    $album =  $_SESSION['ALBUM'];
            
    $t time();
            
    mysql_query("INSERT INTO photos (uploader, album, file, time) VALUES ('$usernom','$album','$filenom','$t')");
            echo 
    mysql_error();
              
            
                return array(
    'success'=>true);
            
            } else {
                return array(
    'error'=> 'Could not save uploaded file.' .
                    
    'The upload was cancelled, or server error encountered');
            }
            
        }    
    }

    // list of valid extensions, ex. array("jpeg", "xml", "bmp")
    $allowedExtensions = array();
    // max file size in bytes
    $sizeLimit 10 1024 1024;

    $uploader = new qqFileUploader($allowedExtensions$sizeLimit);
    $result $uploader->handleUpload('uploads/');
    as you can see i have added mysql insert line to log the uploaded file to the database along with other data i wont paste the javascript cos its miles long but can be found here:-

    http://github.com/valums/file-uploader

    ALSO-as you can see i'm passing data through session is this correct or should it be done another way also sorry i know its long now is there a way to add a <input type="text" name="description">
    to the html and have the file upload script put it in the mysql as it does the file name, time and album

    thanks for your time
    Last edited by TwitterRooms; 02-21-2012 at 08:22 PM.

  2. #2
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default

    What your searching for is an ajax function... not PHP.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •