Jump to content
deejagothic

Validation File Size

Recommended Posts

Salam kawan2,

Aku buat satu form, utk upload file.
Form aku sume ok, bleh upload..tak jadi masalah.

Ape yg aku tengok, php akan upload file sebelom read file metadata. Tak kisah file tu brape besar pun, php akan upload dlu baru bleh baca $files->size, file->type dll.

Persoalannya, camna aku nk buat validation file size menggunakan [b]server-side[/b] scripting utk elakkan dari user upload file yg besar2.

Situasi : user upload file lebih besar dari upload_max_filesize yg aku set kat php.ini. Error dia aku takleh nk display.

Sila beri tunjuk ajar kepada saya.
Ini bukan soalan assignment tau! =)

Share this post


Link to post
Share on other sites
[quote name='BasicCX' date='31 March 2010 - 05:58 PM' timestamp='1270029525' post='1023937']
Salam,

http://www.kavoir.com/2009/01/check-for-file-size-with-javascript-before-uploading.html
[/quote]

Terima kasih bagi link.
Tapi tu validate pakai javascript(Client-Side). Aku nk tau camne cara validation utk server-side.

Terima kasih!

Share this post


Link to post
Share on other sites
Nak pkai server side script apa sbb byk gak server side pnya script. kl php, untuk limit size tu bleh pkai cmni:
$_FILES["maklumat_file"]["size"] < 30000

size tu dlm byte

untuk file format plak, bleh simpan format file yg dibenarkan dlm satu array, kemudian pkai explode atau pkai strrchr untuk dptkan extension file tu cntoh:
$file_ext = strrchr($_FILES["maklumat_file"]['name'],".");

dh dpt extension, bleh la compare dgn array extension yg dibenarkan td. Edited by johnburn

Share this post


Link to post
Share on other sites
[quote name='johnburn' date='31 March 2010 - 10:38 PM' timestamp='1270046287' post='1023972']
Nak pkai server side script apa sbb byk gak server side pnya script. kl php, untuk limit size tu bleh pkai cmni:
$_FILES["maklumat_file"]["size"] < 30000

size tu dlm byte

untuk file format plak, bleh simpan format file yg dibenarkan dlm satu array, kemudian pkai explode atau pkai strrchr untuk dptkan extension file tu cntoh:
$file_ext = strrchr($_FILES["maklumat_file"]['name'],".");

dh dpt extension, bleh la compare dgn array extension yg dibenarkan td.
[/quote]

Good one..and satu soklan cepu emas.
Kalau user upload, PHP akan go through code '$_FILES["maklumat_file"]["size"] < 30000' dlu ke? atau dia akan upload file dlu then baru go through validation?
So klu user upload file yg sangat besar, mcm mane php nk catch error tu?

Sila beri tunjuk ajar!

Share this post


Link to post
Share on other sites
kita buat la setel sume validation dlu br upload file. check size file, check file type, kl nk rename file tu pn bleh rename dlu, if sume ok br pkai function move_uploaded_file() untuk upload file. Edited by johnburn

Share this post


Link to post
Share on other sites
[quote name='johnburn' date='01 April 2010 - 08:11 AM' timestamp='1270080667' post='1024019']
kita buat la setel sume validation dlu br upload file. check size file, check file type, kl nk rename file tu pn bleh rename dlu, if sume ok br pkai function move_uploaded_file() untuk upload file.
[/quote]

Ooo...Tapikan bang, move_uploaded_file() ni klu tak silap aku utk pindahkan file dari tmp folder ke destination yg kite dah set. ape cara kite nk check size file sblom upload bang?

Share this post


Link to post
Share on other sites
[quote name='deejagothic' date='31 March 2010 - 05:06 PM' timestamp='1270026412' post='1023927']

Persoalannya, camna aku nk buat validation file size menggunakan [b]server-side[/b] [b]scripting utk elakkan dari user upload file yg besar2.[/b]

[/quote]


Kalau nak elak user upload file besar kena guna client-side. Kalau nak buat server-side kena upload dulu file baru boleh buat validation, bila tak valid, kena delete, (kan dah membazir bandwidth tu)

Share this post


Link to post
Share on other sites
[quote name='BasicCX' date='01 April 2010 - 11:42 AM' timestamp='1270093339' post='1024049']
Kalau nak elak user upload file besar kena guna client-side. Kalau nak buat server-side kena upload dulu file baru boleh buat validation, bila tak valid, kena delete, (kan dah membazir bandwidth tu)
[/quote]

Oo..jadi maknanya, server side mmg takleh nk validate file size sblom upload la eh?

Share this post


Link to post
Share on other sites
AFAIK, it almost impossible untuk php check seblom file dipindahkn ke temporary folder sbb php adalah server side script.
browser mungkin akan hantar header Content-length bersama file yg diupload tu (time kita tekan butang upload), tp php xdpt bg response sehingga la HTTP request tu selesai atau dlm kata lainnya sehingga file tu dh diupload ke temporary folder. mungkin aku silap on this, tp ikut aku pham gtu la. So mungkin bleh gabungkn dgn client side pnya validation. It usually a good practice to provide extra layer of protection/verification ^_^

Share this post


Link to post
Share on other sites
[quote name='johnburn' date='01 April 2010 - 11:46 AM' timestamp='1270093592' post='1024051']
AFAIK, it almost impossible untuk php check seblom file dipindahkn ke temporary folder sbb php adalah server side script.
browser mungkin akan hantar header Content-length bersama file yg diupload tu (time kita tekan butang upload), tp php xdpt bg response sehingga la HTTP request tu selesai atau dlm kata lainnya sehingga file tu dh diupload ke temporary folder. mungkin aku silap on this, tp ikut aku pham gtu la. So mungkin bleh gabungkn dgn client side pnya validation. It usually a good practice to provide extra layer of protection/verification ^_^
[/quote]

Client side tu mmg dah ada...cume extra protection. Maklumlah, never trust user input punye hal.
Jadi bila kadang2 ada yg menggatal off kan javascript kat browser, lepas tu upload file smpi berpuluh2 GB. pastu tak kuar error, tak kuar ape2 response.

And kalau boleh, aku nk buat camnih :

[code]

$Uploader = new Uploader();
try {
$Uploader->process($_FILES['file']));
}
catch (Exception $e) {
var_dump($e->getMessage());
}
[/code]

kat situ aku bleh trap error...Tapi still tak jumpe lagi cara yg sesuai.
Terima kasih kawan2..memberi tunjuk ajar pada saya yg masih belajar :D Edited by deejagothic

Share this post


Link to post
Share on other sites
bleh ubah kandungan php.ini dan limitkan [b]upload_max_filesize[/b] dan [b]post_max_size[/b]
nt kl klau file lg besar dr yg diset ni, dia akan kuar error la

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...