php - fine-uploader implementation -
i have html
file:
<html> <head> <meta charset="utf-8"> <title>fine uploader demo</title> <link href="fineuploader-3.4.1.css" rel="stylesheet"> </head> <body> <div id="fine-uploader"></div> <script src="jquery-1.7.2.min.js"></script> <script src="jquery.fineuploader-3.4.1.js"></script> <script> function createuploader() { var uploader = new qq.fineuploader({ // pass html element here element: document.getelementbyid('fine-uploader'), // or, if using jquery // element: $('#fine-uploader')[0], // use relevant server script url here // if it's different default “/server/upload” request: { endpoint: 'qqfileuploader' } }); } window.onload = createuploader; </script> </body> </html>
my php code qqfileuploader.php
<?php class qqfileuploader { public $allowedextensions = array(); public $sizelimit = null; public $inputname = 'qqfile'; public $chunksfolder = 'chunks'; public $chunkscleanupprobability = 0.001; // once in 1000 requests on avg public $chunksexpirein = 604800; // 1 week protected $uploadname; function __construct(){ $this->sizelimit = $this->tobytes(ini_get('upload_max_filesize')); } /** * original filename */ public function getname(){ if (isset($_request['qqfilename'])) return $_request['qqfilename']; if (isset($_files[$this->inputname])) return $_files[$this->inputname]['name']; } /** * name of uploaded file */ public function getuploadname(){ return $this->uploadname; } /** * process upload. * @param string $uploaddirectory target directory. * @param string $name overwrites name of file. */ public function handleupload($uploaddirectory, $name = null){ if (is_writable($this->chunksfolder) && 1 == mt_rand(1, 1/$this->chunkscleanupprobability)){ // run garbage collection $this->cleanupchunks(); } // check max upload size specified in class configuration not // exceed size allowed server config if ($this->tobytes(ini_get('post_max_size')) < $this->sizelimit || $this->tobytes(ini_get('upload_max_filesize')) < $this->sizelimit){ $size = max(1, $this->sizelimit / 1024 / 1024) . 'm'; return array('error'=>"server error. increase post_max_size , upload_max_filesize ".$size); } // is_writable() not reliable on windows (http://www.php.net/manual/en/function.is-executable.php#111146) // following tests if current os windows , if so, merely checks if folder writable; // otherwise, checks additionally executable status (like before). $iswin = (strtoupper(substr(php_os, 0, 3)) === 'win'); $folderinaccessible = ($iswin) ? !is_writable($uploaddirectory) : ( !is_writable($uploaddirectory) && !is_executable($uploaddirectory) ); if ($folderinaccessible){ return array('error' => "server error. uploads directory isn't writable" . (!$iswin) ? " or executable." : "."); } if(!isset($_server['content_type'])) { return array('error' => "no files uploaded."); } else if (strpos(strtolower($_server['content_type']), 'multipart/') !== 0){ return array('error' => "server error. not multipart request. please set forcemultipart default value (true)."); } // size , name $file = $_files[$this->inputname]; $size = $file['size']; if ($name === null){ $name = $this->getname(); } // validate name if ($name === null || $name === ''){ return array('error' => 'file name empty.'); } // validate file size if ($size == 0){ return array('error' => 'file empty.'); } if ($size > $this->sizelimit){ return array('error' => 'file large.'); } // validate file extension $pathinfo = pathinfo($name); $ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : ''; if($this->allowedextensions && !in_array(strtolower($ext), array_map("strtolower", $this->allowedextensions))){ $these = implode(', ', $this->allowedextensions); return array('error' => 'file has invalid extension, should 1 of '. $these . '.'); } // save chunk $totalparts = isset($_request['qqtotalparts']) ? (int)$_request['qqtotalparts'] : 1; if ($totalparts > 1){ $chunksfolder = $this->chunksfolder; $partindex = (int)$_request['qqpartindex']; $uuid = $_request['qquuid']; if (!is_writable($chunksfolder) && !is_executable($uploaddirectory)){ return array('error' => "server error. chunks directory isn't writable or executable."); } $targetfolder = $this->chunksfolder.directory_separator.$uuid; if (!file_exists($targetfolder)){ mkdir($targetfolder); } $target = $targetfolder.'/'.$partindex; $success = move_uploaded_file($_files[$this->inputname]['tmp_name'], $target); // last chunk saved if ($success , ($totalparts-1 == $partindex)){ $target = $this->getuniquetargetpath($uploaddirectory, $name); $this->uploadname = basename($target); $target = fopen($target, 'wb'); ($i=0; $i<$totalparts; $i++){ $chunk = fopen($targetfolder.directory_separator.$i, "rb"); stream_copy_to_stream($chunk, $target); fclose($chunk); } // success fclose($target); ($i=0; $i<$totalparts; $i++){ unlink($targetfolder.directory_separator.$i); } rmdir($targetfolder); return array("success" => true); } return array("success" => true); } else { $target = $this->getuniquetargetpath($uploaddirectory, $name); if ($target){ $this->uploadname = basename($target); if (move_uploaded_file($file['tmp_name'], $target)){ return array('success'=> true); } } return array('error'=> 'could not save uploaded file.' . 'the upload cancelled, or server error encountered'); } } /** * returns path use upload. check name not exist, * , appends suffix otherwise. * @param string $uploaddirectory target directory * @param string $filename name of file use. */ protected function getuniquetargetpath($uploaddirectory, $filename) { // allow 1 process @ time unique file name, otherwise // if multiple people upload file same name @ same time // latest saved. if (function_exists('sem_acquire')){ $lock = sem_get(ftok(__file__, 'u')); sem_acquire($lock); } $pathinfo = pathinfo($filename); $base = $pathinfo['filename']; $ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : ''; $ext = $ext == '' ? $ext : '.' . $ext; $unique = $base; $suffix = 0; // unique file name file, appending random suffix. while (file_exists($uploaddirectory . directory_separator . $unique . $ext)){ $suffix += rand(1, 999); $unique = $base.'-'.$suffix; } $result = $uploaddirectory . directory_separator . $unique . $ext; // create empty target file if (!touch($result)){ // failed $result = false; } if (function_exists('sem_acquire')){ sem_release($lock); } return $result; } /** * deletes file parts in chunks folder files uploaded * more chunksexpirein seconds ago */ protected function cleanupchunks(){ foreach (scandir($this->chunksfolder) $item){ if ($item == "." || $item == "..") continue; $path = $this->chunksfolder.directory_separator.$item; if (!is_dir($path)) continue; if (time() - filemtime($path) > $this->chunksexpirein){ $this->removedir($path); } } } /** * removes directory , files contained inside * @param string $dir */ protected function removedir($dir){ foreach (scandir($dir) $item){ if ($item == "." || $item == "..") continue; unlink($dir.directory_separator.$item); } rmdir($dir); } /** * converts given size units bytes. * @param string $str */ protected 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; } }
when upload file, follwing 2 errors , file upload fails:
error 1
options file:///c:/users/upload/qqfileuploader jquery.fineuploader-3.4.1.js:3903 handlestandardfileupload jquery.fineuploader-3.4.1.js:3903 api.upload jquery.fineuploader-3.4.1.js:3989 upload jquery.fineuploader-3.4.1.js:3041 qq.fineuploaderbasic._upload jquery.fineuploader-3.4.1.js:1437 qq.fineuploaderbasic._uploadfileorblobdatalist jquery.fineuploader-3.4.1.js:1415 qq.fineuploaderbasic.addfiles jquery.fineuploader-3.4.1.js:1049 qq.fineuploaderbasic._oninputchange jquery.fineuploader-3.4.1.js:1340 qq.uploadbutton.onchange jquery.fineuploader-3.4.1.js:1117 (anonymous function) jquery.fineuploader-3.4.1.js:680
error 2
[fineuploader] error when attempting parse xhr response text (syntaxerror: unexpected end of input) jquery.fineuploader-3.4.1.js:155 qq.log jquery.fineuploader-3.4.1.js:155 qq.fineuploaderbasic.log jquery.fineuploader-3.4.1.js:939 qq.uploadhandler.log jquery.fineuploader-3.4.1.js:1146 parseresponse jquery.fineuploader-3.4.1.js:3683 oncomplete jquery.fineuploader-3.4.1.js:3732 (anonymous function) jquery.fineuploader-3.4.1.js:3766
when debug set true, this:
[fineuploader] xhr - server response received 0 jquery.fineuploader-3.4.1.js:150 [fineuploader] responsetext = <!doctype html public "-//ietf//dtd html 2.0//en"> <html><head> <title>404 not found</title> </head><body> <h1>not found</h1> <p>the requested url php/qqfileuploader.php not found on server.</p> <hr> <address>apache/2.2.15 (centos) server @ port 80</address> </body></html>
you haven't set endpoint correctly, appears. endpoint must relative or absolute url pointing @ location on server setup handle fine uploader's requests. looks may not running app in webserver. seems opening web browser , pointing @ html file on filesystem. never going work.
please see demos on fineuploader.com request option documentation in readme.
Comments
Post a Comment