ainbila Posted July 31, 2021 Share Posted July 31, 2021 how to put code for check file size before upload in the if issertfile .the file is in format pdf or docs $targetDirg= "folder/pda-semakan/gambar/"; if(isset($_FILES['gambar'])){ $fileNameg = $_FILES['gambar']['name']; $targetFilePathg = $targetDirg . $fileNameg; //$main_tmp1 = $_FILES['surat']['tmp_name']; $moveg =move_uploaded_file($_FILES["gambar"]["tmp_name"], $targetFilePathg); } Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/ Share on other sites More sharing options...
gw1500se Posted July 31, 2021 Share Posted July 31, 2021 (edited) You can't know that from PHP (server side) directly. You can use Javascript/Ajax (client side) to send the file size back to PHP. However, the question is why do you care about the file size before upload? If you want to prevent files over a certain size from being uploaded you can set that with 'upload_max_filesize' in php.ini. The default is 2M. Edited July 31, 2021 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588737 Share on other sites More sharing options...
ainbila Posted July 31, 2021 Author Share Posted July 31, 2021 because we need to limit the file size as our server is not too big . The user need to upload multiple file .so , we currenly have problem when user upload file too big and it slower our server Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588740 Share on other sites More sharing options...
Strider64 Posted July 31, 2021 Share Posted July 31, 2021 if ($_FILES['image']['size'] >= 44040192) { $errors[] = 'File size must be less than or equal to 42 MB'; } Simply check the file size and throw and throw an error if it is too big. I would also on the HTML page state the maximum file size allowed to upload. You could also try to compress the file, but that gets more involved. Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588741 Share on other sites More sharing options...
requinix Posted July 31, 2021 Share Posted July 31, 2021 Check the php.net documentation for a method that will allow PHP to ignore the file upload if it's too large. Hint: it's a hidden input in your <form>. 2 minutes ago, Strider64 said: Simply check the file size and throw and throw an error if it is too big. I would also on the HTML page state the maximum file size allowed to upload. You could also try to compress the file, but that gets more involved. That will only take effect after the file has been uploaded. Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588742 Share on other sites More sharing options...
Strider64 Posted July 31, 2021 Share Posted July 31, 2021 2 minutes ago, requinix said: Check the php.net documentation for a method that will allow PHP to ignore the file upload if it's too large. Hint: it's a hidden input in your <form>. That will only take effect after the file has been uploaded. Yeah I know, but wouldn't the file name have to be known if it is a hidden attribute? Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588744 Share on other sites More sharing options...
requinix Posted July 31, 2021 Share Posted July 31, 2021 38 minutes ago, Strider64 said: Yeah I know, but wouldn't the file name have to be known if it is a hidden attribute? No. Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588745 Share on other sites More sharing options...
gw1500se Posted July 31, 2021 Share Posted July 31, 2021 2 hours ago, ainbila said: because we need to limit the file size as our server is not too big . The user need to upload multiple file .so , we currenly have problem when user upload file too big and it slower our server Like I said, you can limit the upload size with the php.ini parameter or use requinix suggestion. However, a malicious user could change that parameter in the HTML page. Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588746 Share on other sites More sharing options...
ainbila Posted August 5, 2021 Author Share Posted August 5, 2021 On 7/31/2021 at 11:27 PM, Strider64 said: if ($_FILES['image']['size'] >= 44040192) { $errors[] = 'File size must be less than or equal to 42 MB'; } Simply check the file size and throw and throw an error if it is too big. I would also on the HTML page state the maximum file size allowed to upload. You could also try to compress the file, but that gets more involved. Thank you . Do you know if there have code for reupload file in php ? Quote Link to comment https://forums.phpfreaks.com/topic/313459-check-file-size-before-upload/#findComment-1588857 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.