Jump to content

check file size before upload


ainbila

Recommended Posts

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);
 }  
         
 

 

Link to comment
Share on other sites

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 by gw1500se
Link to comment
Share on other sites

    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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.