Jump to content

file Upload question


bryanevil

Recommended Posts

Hi everyone,

I have facing this problem for a few days already. In my currently project, I  want to stop the file upload when the file size is larger than my website default before it completely send to the server (like gmail or hotmail). I know php has "MAX_FILE_SIZE" , is there another way to do that?

I know PHP cannot do that because it is server side program language. I also know activeX or Java can achieve that , but only if user allow website to access their filesystem (mostly wouldnt). can anyone give me solution ? I heard about ajax can do something like this,is it true and how to they do that? when i google ajax and file upload, i find nothing about check file size. 

Cheers,
Bryan
Link to comment
https://forums.phpfreaks.com/topic/35346-file-upload-question/
Share on other sites

Client side you can use,

<input type="hidden" name="MAX_FILE_SIZE" value="100" />

on your upload form. However the PHP method is better because people can change client side input by creating a mimic of your original form.

The following is a function I made to check file uploads, you may wish to change it to suit your needs.

[code=php]
//********************************** check_upload function **************************************//
function check_upload($upload, $dest, $max) {

  $filewarning = "";

  // Error checking
  switch ($upload['error']) {
          case 0:
                // No Error, now check and edit name if required and upload file to directory
                $name = strtolower($upload['name']);
                $filenameinfo = convert_filename($name);
                $newname = $filenameinfo[2];

                // If function finds no extension then return error warning and do not upload else accept new name and upload.
                if ($filenameinfo[0]=="!") {
                    $filewarning = "<span class='str red'>No file extension found, file not uploaded.</span>";
                } else {
                    if (file_exists($dest.$newname)) {
                      $filewarning = "<span class='str'>File overwritten sucessfully.</span>";
                    } else {
                      $filewarning = "<span class='str'>File uploaded sucessfully.</span>";
                    }
                    move_uploaded_file( $upload['tmp_name'], $dest.$newname );
                }
                break;
          case 1:
                // Upload exceeds php config maxsize
                $filewarning = "<span class='str red'>File size exceeds ".ini_get("upload_max_filesize")." in PHP config, file not uploaded see administrator</span>";
                break;
          case 2:
                // Upload exceeds preset HTML config size
                $filewarning = "<span class='str red'>File size exceeds {$max} bytes, file not uploaded.</span>";
                break;
          case 3:
                // Upload was only partially uploaded or corrupt
                $filewarning = "<span class='str red'>File upload is corrupt, file not uploaded please try again.</span>";
                break;
          case 4:
                // No file present do nothing
                break;
          case 6:
                // Temporary folder is missing or corrupt on server
                $filewarning = "<span class='str red'>There is a problem with the server temporary folder, file not uploaded contact administrator.</span>";
                break;
          case 7:
                // Failed to write to disk (may not work as it was introduced in PHP 5)
                $filewarning = "<span class='str red'>Disk Write Failure, file was not uploaded, contact administrator.</span>";
                break;
          default:
                // Unknown error
                $filewarning = "<span class='str red'>An unknown error occurred, file was not uploaded, contact administrator.</span>";
                break;
  }

return $filewarning;
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/35346-file-upload-question/#findComment-167056
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.