Where is the problem with uploading code?

Source: Internet
Author: User
What is the problem with uploading code ??? This upload code does not limit the size of the file to be uploaded. However, if the size of the uploaded file exceeds KB, the file cannot be uploaded. what is the problem? I want to limit the number of files that can only be uploaded within KB. how can I change it? & Lt ;? Php & nbsp; $ allow_type = array (& quot; m4r & quot; where is the problem with this Upload code ???
This upload code does not limit the size of the file to be uploaded. However, if the size of the uploaded file exceeds KB, the file cannot be uploaded. what is the problem?
I want to limit the number of files that can only be uploaded within KB. how can I change it?



$ Allow_type = array ("m4r", "jpg"); // specifies the image type that can be uploaded.

$ Name = $ _ FILES ['upimg '] ['name']; // original name of the client file

$ Upload_dir = "upload/"; // image storage directory

// Strtolower converts the string to lowercase

$ File_suffix = strtolower (getFileSuffix ($ name ));

If (! In_array ($ file_suffix, $ allow_type ))

{

// Implode combines array elements into strings

$ All_type = implode (",", $ allow_type );

Exit ("script" alert ('You can only upload images in $ all_type format! '); History. back (); script ");

}

// Explode splits the string into arrays

$ Name_array = explode (".", $ name );


Do
{

$ Name_array [0] = randomFileName ();

$ Name = implode (".", $ name_array );

$ Upload_file = $ upload_dir. $ name;

} While (file_exists ($ upload_file ));


$ Tmp_name = $ _ FILES ['uptime'] ['tmp _ name'];

// Is_uploaded_file: checks whether the file is uploaded through http post.

If (is_uploaded_file ($ tmp_name ))

{




// Move_uploaded_file: move the uploaded file to the specified directory.

If (move_uploaded_file ($ tmp_name, $ upload_file ))

{

Echo "$ upload_file song uploaded successfully! ";
Exit ("your scriptshorttop.doc ument. getElementById ('URL'). value = '$ upload_file'; script");


}

}
// Get the file suffix

Function getFileSuffix ($ fileName)

{

// Strrchr returns all the characters from the last position to the end in $ fileName. Remove '.' from substr '.'

Return substr (strrchr ($ fileName, '.'), 1 );

}

// Generate random file names

Function randomFileName ()

{

// Change the time zone to Beijing time, and php5 defaults to Greenwich Mean Time.

Date_default_timezone_set ("PRC ");

Return date ('ymdhis '). rand (100,999 );

}



?>

------ Solution --------------------
1. FILES larger than KB cannot be uploaded. please take a look at the value of $ _ FILES ['upimg '] ['error']. There may be the following situations:
The value is 1, indicating that the file size exceeds the upload_max_filesize defined in php. ini;
The value is 2, indicating that the file size exceeds the limit in HTML. The following code restricts the file size in HTML:
HTML code
  
------ Solution --------------------
It can also be determined in PHP!
If ($ _ FILES ['upimg '] ['size']> 1024*500)
{
Echo "the file exceeds 500 kB and is not uploaded! ";
Exit;
}
As for the inability to upload, my personal estimate is:
Do
{

$ Name_array [0] = randomFileName ();

$ Name = implode (".", $ name_array );

$ Upload_file = $ upload_dir. $ name;

} While (file_exists ($ upload_file ));
$ Upload_file = $ upload_dir. $ name; the fact that this file does not exist for the first time is that the code in this do is not executed at all, that is, $ upload_file cannot actually reflect its file path, in this case, the move_uploaded_file cannot be executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.