php - Error with file upload as base64 encoded string -


i want receive image android device, sending image base64 encoded string. controller action code:

public function  upload() {     if ($this->request->is('post')) {         $dir= app.'outsidefiles'; //chane directory cloud         $fill = $this->request->data['file'] ;         $data = base64_decode($fill);         $im = imagecreatefromstring($data);         if ($im !== false)         {             $nam ='mypic.png';             move_uploaded_file($im['tmp_name'],$dir.ds.time().$nam);         }          //$dir= app . 'outsidefiles';          //  $this->request->data['grade']['fila']= $file;         $this->grade->create();         if ($this->grade->save($this->request->data))         {              $return = array(                 'response' =>'1',                 'body' => 'data saved');             return new cakeresponse(array('body' => json_encode($return, json_numeric_check)));          }         else         {             $return = array(                 'response' =>'0',                 'body' => 'data not saved');             return new cakeresponse(array('body' => json_encode($return, json_numeric_check)));         }     } } 

however image not created in destination folder - error?

move uploaded file moving uploaded files

this not work:

move_uploaded_file("i not file upload" , "/put/file/here/path.png"); 

it won't work if first argument points @ file because, as stated in documentation:

if filename not valid upload file, no action occur, , move_uploaded_file() return false.

this prevents kind of naïve attack working:

// user input not safe, e.g.: $_files['example']['tmp_name'] = '/etc/passwd';  ... move_uploaded_file(     $_files['example']['tmp_name'],      "/web/accessible/location/now-public.txt" ); 

image create string not return array

this won't work:

$im = imagecreatefromstring($data); $im['tmp_name']; <- 

as, return value function is image resource, not array. tmp_name related file uploads - there no actual file upload if being submitted base64 encoded string; it's string.

to upload base64 encoded string means creating file

the logical steps required not related file uploads @ all, writing binary string file i.e.:

$data = base64_decode($fill); file_put_contents('/tmp/pic.png', $data); 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -