Allowed File Types in 4.2.8

Talk about Image/File fields (uploading, problems etc.)

Moderators: ne_moj, zewa, vadimako, ne_moj, zewa, vadimako

sukhdeepsinghkohli
Newbie
Newbie
Posts: 1
Joined: Aug 28th, '09, 14:34

Allowed File Types in 4.2.8

Postby sukhdeepsinghkohli » Aug 28th, '09, 14:41

Hi

First of all thanks for such a great component to integrate into my websites. I was wondering is it possible to allow uploads of only particular File Types in PHP Data Grid 4.2.8.

For Example, I only want .pdf Files to be uploaded ?

Regards

administrator
Site Admin
Site Admin
Posts: 5976
Joined: Jan 7th, '09, 23:18
Contact:

Re: Allowed File Types in 4.2.8

Postby administrator » Aug 29th, '09, 08:44

No, it's not possible.
But I can tell you how you can implement it...
You need some javascript script code, that will check a file extension in "browse"
filed and give you alert if something wrong or clean the field.

You can write such code, using

<script type='text/javascript'>
function unique_prefix_onSubmitMyCheck(){
return true;
}
</script>

Read about this function here:
viewtopic.php?f=33&t=98

lambro
Master
Master
Posts: 102
Joined: Sep 13th, '10, 14:31
Location: Metz, France

Re: Allowed File Types in 4.2.8

Postby lambro » Oct 7th, '10, 09:56

It's an old topic but since I just coded that function for a website I develop (and since I've been really happy to get hints from numerous contributors in this forum ;) ), I may as well give my own personal example here :

At the end of the <body> part I put :

Code: Select all

##  *** bind the DataGrid and draw it on the screen
         $dgrid->Bind();       
         ob_end_flush();
?>
   <script type="text/javascript">
//function used to check the file name of the file put in the uploaded field (and entitled "styfile_name")
function recup_extension(){
var numbcar;
var extension;
var namfile = document.getElementById("styfile_name").value;
numbcar = namfile.length;
extension = namfile.substring(numbcar-4,numbcar);
extension=extension.toLowerCase();
return extension;
}
         
function verif_extension(){
var ext = recup_extension();
//in this example, we only want .pdf files
if(ext != ".pdf" ){
// Here you put your warning message
    alert("The file name you are trying to upload is :"+ext+". \n This file name is not allowed !\n .pdf file type only !>");
// Here you refresh the page (=clear the field) if selected file does not match what you want :
//use the address you get in your URL for add or edit mode
   window.location.replace("http://www.YOURWEBSITE.YOURDOMAIN/index.php?act=&id=&namdoc=&ldoc=&verdoc=&docmode=add&docrid=-1&docsort_field=ta_document.nom_doc&docsort_type=ASC&docpage_size=10&docp=1");
   }
}
</script>


Now, in the

Code: Select all

+---------------------------------------------------------------------------+
    ## | 7. Add/Edit/Details Mode Settings:                                        |
    ## +---------------------------------------------------------------------------+

section, I put :

Code: Select all

"file_name" =>array("header"=>"file", "type"=>"file", "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true",
"on_js_event"=>"onchange=verif_extension()",
"target_path"=>"uploads/", "max_file_size"=>"10M", "file_name"=>"", "host"=>"local|remote", "allow_downloading"=>"true"),


That's it now, only .pdf files can be put on the upload field.
Thanks ApPHP for your wonderful softwares :D

administrator
Site Admin
Site Admin
Posts: 5976
Joined: Jan 7th, '09, 23:18
Contact:

Re: Allowed File Types in 4.2.8

Postby administrator » Oct 10th, '10, 07:42

Thanks for your help.
BTW, from v6.1.1. there is a new attribute for file/image fields: "allowed_extensions"=>""
The syntax is "allowed_extensions"=>"pdf, txt" etc.

lambro
Master
Master
Posts: 102
Joined: Sep 13th, '10, 14:31
Location: Metz, France

Re: Allowed File Types in 4.2.8

Postby lambro » Oct 11th, '10, 13:45

Again, I like your grid. Since I could put version 6.1.1 into effect, I removed my javascript functions. Here is a little help regarding french language : change the sentence in English to French !

In ->languages ->fr.php replace

Code: Select all

$lang['file_extension_error'] = "File upload error: file extension not allowed for upload. Please select another file.";
by

Code: Select all

$lang['file_extension_error'] = "Erreur d'upload de fichier: Cette extension de fichier n'est pas autoris&eacute;e. Merci de s&eacute;lectionner un autre fichier.";

administrator
Site Admin
Site Admin
Posts: 5976
Joined: Jan 7th, '09, 23:18
Contact:

Re: Allowed File Types in 4.2.8

Postby administrator » Oct 12th, '10, 09:01

Fixed for the next version. Thanks!


Return to “Field type: "image/file"”