limiting downloading files Type

Discuss MySQL Ajax Table Editor Pro Version

limiting downloading files Type

Postby olivier » Wed Dec 16, 2015 8:40 pm

Hi,

Is it possible to limit the file upload type.
For example allow only .PDF .JPG .GIF. and deny everything else.

Thank a lot for your help.
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm

Re: limiting downloading files Type

Postby admin » Fri Dec 18, 2015 3:21 pm

Yes this is possible. Take a look at the upload to directory example. The handle upload callback function returns an array of validation errors, so in that function you could verify the file type before uploading.
admin
Site Admin
 
Posts: 1502
Joined: Fri Jul 11, 2008 1:34 am

Re: limiting downloading files Type

Postby olivier » Fri Dec 18, 2015 9:31 pm

Thank you for your answer.
Alas, I am not a development expert, would you have a piece of code to guide me.

Thank a lot for your help.
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm

Re: limiting downloading files Type

Postby olivier » Tue Dec 22, 2015 9:44 pm

not even a small piece of code under the tree for Christmas ;)

Thank a lot to all for your Help
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm

Re: limiting downloading files Type

Postby olivier » Tue Dec 29, 2015 9:01 pm

Hi,

Here is my code, when the extension is the content of the array $extensions_valides, the table is working correctly.

I have a problem when the file extension does not match.
For exemple : i'm upload the file "Acheter.pdf", in print in the tableeditor : "%PDF-1.4%" and the navigator return this message after the clic link "%PDF-1.4%" in tableeditor : Your browser sent a request that this server could not understand.


Code: Select all
// Copy file to data directory and update database with the file name.
         if(move_uploaded_file($filesArr['tmp_name'],$this->dataDir.$filesArr['name']))
         {
            $extensions_valides = array( 'jpg' , 'jpeg' , 'gif' , 'png', 'txt' );
            $extension_upload = strtolower(  substr(  strrchr($filesArr['name'], '.')  ,1)  );

            if ( in_array($extension_upload,$extensions_valides) )
               
            {
               
               $query = "update essai_entretien_nice set file_name = :file_name where code_essai = :id";
               $queryParams = array('file_name' => $filesArr['name'], 'id' => $id);
               $stmt = DBC::get()->prepare($query);
               $result = $stmt->execute($queryParams);
            }
            
            
            if(!$result)
            {
               $valErrors[] = 'There was an error updating the database.';
               unlink($this->dataDir.$filesArr['name']);
            }
         }
         else
         {
            $valErrros[] = 'The file could not be moved';
         }


Thank a lot for your help.
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm

Re: limiting downloading files Type

Postby olivier » Wed Dec 30, 2015 11:09 pm

Hi,
Here is my code, I checked though the file format and if this is the right, the transfer is OK.
If the format I want to upload is not accepted, the upload is not done (that's good) but I can not seem to display an error message.

Thank you kindly help me, I do not know what to do.



Code: Select all
// Copy file to data directory and update database with the file name.
         if(move_uploaded_file($filesArr['tmp_name'],$this->dataDir.$filesArr['name']))
         {
            $type_file = $filesArr['name'];
            if( !strstr($type_file, 'txt') && !strstr($type_file, 'pdf') && !strstr($type_file, 'xsl') && !strstr($type_file, 'doc') )
            {
               $valErrors[] = 'ERREUR : Le fichier sélectionné n\'est pas autorisé ! (pdf, txt, xls, doc).';
               exit;
            }
            ELSE
            {
               $query = "update essai_entretien_nice set file_name = :file_name where code_essai = :id";
               $queryParams = array('file_name' => $filesArr['name'], 'id' => $id);
               $stmt = DBC::get()->prepare($query);
               $result = $stmt->execute($queryParams);
            
               if(!$result)
               {
                  $valErrors[] = 'There was an error updating the database.';
                  unlink($this->dataDir.$filesArr['name']);
               }
            }
         }
         else
         {
            $valErrros[] = 'The file could not be moved';
         }
      }
      return $valErrors;
   }


thanks for your help
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm

Re: limiting downloading files Type

Postby olivier » Thu Jan 07, 2016 10:00 pm

Hi,

Help me please, my project is finished after the resolution of this problem.

Thanks you !
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm

Re: limiting downloading files Type

Postby admin » Tue Jan 19, 2016 3:53 pm

You are exiting after setting the validation error. Have you tried removing that exit to see if it works?
admin
Site Admin
 
Posts: 1502
Joined: Fri Jul 11, 2008 1:34 am

Re: limiting downloading files Type

Postby olivier » Mon Jan 25, 2016 9:32 pm

Hi,

If I remove "EXIT", for an unauthorized file extention is upload, a message appears "UPLOAD OK".
The file is not uploaded but a corrupt file link is sent to the database. Obviously when you click this link, a page is displayed 404.
If the extension is approved, the program works.

An idea ?

Thanks
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm

Re: limiting downloading files Type

Postby olivier » Sat Jan 30, 2016 6:09 pm

Hi
You have a solution for this problem ?

Thank
olivier
 
Posts: 17
Joined: Tue Dec 15, 2015 12:09 pm


Return to Pro Version

Who is online

Users browsing this forum: No registered users and 2 guests

cron