Page 1 of 1

Handle upload from file in disk

PostPosted: Wed Jan 19, 2011 1:43 am
by jvinolas
Hi,

I tried successfully to upload a .zip file (not a tar.gz... don't know why) to a blob field and retrieved it again correctly using the provided documentation. My problem is that I'm generating some text files that I zip into a file in disk on the server and I want to upload it automatically to a blob field.

What I've tried since now is:

Code: Select all
$data = file_get_contents("/tmp/tmp.zip");
$data = mysql_real_escape_string($data);
        $record->setValues(
            array(,
      'FITXERS'=>$data,
      'FITXERS_mimetype'=>"application/x-gzip",
      'FITXERS_filename'=>"tmp.zip"
      ));


This code uploads the file, but there is a problem when downloading from xataface application that makes the downloaded file unusable. I think the problem is with the method that I use to read the file or with the format I apply to $data before uploading the file.

Does anybody knows how to handle file upload to a blob from server disk?

Thanks in advance.

Re: Handle upload from file in disk

PostPosted: Wed Jan 19, 2011 1:46 pm
by shannah
Can you show the actual code where you insert this into the database? You've shown your generating of the $data array but now how you're inserting it.

-Steve

Re: Handle upload from file in disk

PostPosted: Thu Jan 20, 2011 12:53 am
by jvinolas
Here is complete function

Code: Select all
function beforeInsert(&$record){
        //Previous checks
   $defany=date("Y");$defany=$defany-1;
   $sql = "select count(*) as row_count from interesos where IDANY='".$defany."'";
   $res = mysql_query($sql);
   $num_rows = mysql_result($res,0);

   if (strcmp($record->val('IDANY'),$defany)!=0){
      //Year error
      return PEAR::raiseError("Any incorrecte per al càlcul.",DATAFACE_E_NOTICE);
   }else{
   if ($num_rows!=0){
      //Already exists
            return PEAR::raiseError('Ja existeix el còmput interessos per a aquest any. Cal eliminar-lo abans, si es el cas.',DATAFACE_E_NOTICE);
   }else{
      //OK, let's do it!   
   //Random string
   $codiOperacio=$defany.md5(uniqid());
       //Call to function that returns 3 numbers
   list($cr,$ct,$cd)=insertar_Dades($defany,$codiOperacio,$record->val('INTERESANUAL'));// 0:reinv,1:transf,2:don

   //Read file and format
      $data = file_get_contents("/tmp/tmp.zip");
                $data = mysql_real_escape_string($data);

        //Another method.... not working also
    //$fh = fopen("/tmp/tmp.zip", "r");
    //$data = addslashes(fread($fh, filesize("/tmp/tmp.zip")));
    //$data = fread($fh, filesize("/tmp/tmp.zip"));
    //fclose($fh);

        $record->setValues(
            array(
                'IDANY'=>$defany,
                'REINVERSIONS'=>$cr,
                'TRANSFERENCIES'=>$ct,
                'DONATIUS'=>$cd,
                'CODIOPERACIO'=>$codiOperacio,
      'FITXERS'=>$data,
      'FITXERS_mimetype'=>"application/x-gzip",
      'FITXERS_filename'=>"tmp.zip"
      ));

   }}

}


As you can see, I use the beforeInsert handle to modify the records (setValues). It uploads file, but I get incorrect file on downloading through app.

Re: Handle upload from file in disk

PostPosted: Thu Jan 20, 2011 9:35 am
by shannah
You don't need to add slashes (escape strings) for values that are inserted via $record->setValue(). All values are escaped when Xataface serializes the record in the database. The the fact that you are escaping the string would definitely account for the file being corrupted (because it is being escaped twice).

Re: Handle upload from file in disk

PostPosted: Fri Jan 21, 2011 3:20 am
by jvinolas
Removing the extra escape sequence I had added worked perfectly!

If anyone interested, is the same code as before, but removing the line $data = mysql_real_escape_string($data);

Thanks a lot!