Simple PHP Image Upload Program

Source: Internet
Author: User

First:
PHP Section

CopyCode The Code is as follows: <? PHP
If ($ _ FILES ['file'] ['error']> 0 ){
Echo '! Problem :';
Switch ($ _ FILES ['file'] ['error'])
{
Case 1: Echo 'file size exceeds server limited ';
Break;
Case 2: Echo 'the file is too large! ';
Break;
Case 3: ECHO: only part of the file is loaded! ';
Break;
Case 4: Echo 'file loading failed! ';
Break;
}

Exit;
}
If ($ _ FILES ['file'] ['SIZE']> 1000000 ){
Echo 'The file is too large! ';
Exit;
}
If ($ _ FILES ['file'] ['type']! = 'Image/JPEG '& $ _ FILES ['file'] ['type']! = 'Image/gif '){
Echo: The file is not jpg or GIF! ';
Exit;
}
$ Today = date ("ymdhis ");
$ Filetype = $ _ FILES ['file'] ['type'];
If ($ filetype = 'image/JPEG '){
$ Type = '.jpg ';
}
If ($ filetype = 'image/gif '){
$ Type = '.gif ';
}
$ Upfile = 'upfile/'. $ today. $ type;
If (is_uploaded_file ($ _ FILES ['file'] ['tmp _ name'])
{
If (! Move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], $ upfile ))
{
Echo 'failed to move the file! ';
Exit;
}
}
Else
{
Echo 'problem! ';
Exit;
}
Echo 'Echo 'file size: '. $ _ FILES ['file'] ['SIZE']. 'byte'. '<br> ';
Echo 'file path: '. $ upfile;
Echo '<HR with = "100%"/>'. '<p> ';
$ Dirr = 'upfile /';
$ Dir = opendir ($ dirr );
Echo $ dirr. '-- listing: <ul> ';
While ($ file = readdir ($ DIR )){
Echo "<li> $ file </LI> ";
}
Echo '</ul> ';
Closedir ($ DIR );
?>

Second:Copy codeThe Code is as follows: <? PHP

If (empty ($ _ Get [Submit])

{

?>
<Form enctype = "multipart/form-Data" Action = "<? PHP $ _ server ['php _ Self ']?>? Submit = 1 "method =" Post ">
Send this file: <input name = "FILENAME" type = "file">
<Input type = "Submit" value = "Confirm upload">
</Form>
<? PHP
} Else {
$ Path = "uploadfiles/"; // upload path

// Echo $ _ FILES ["FILENAME"] ["type"];

If (! File_exists ($ PATH ))
{
// Check whether the folder exists. If not, create the folder and grant the highest permission.
Mkdir ("$ path", 0700 );
} // End if
// File format that can be uploaded
$ TP = array ("image/GIF", "image/pjpeg", "image/PNG ");
// Check whether the uploaded file is of the allowed upload type
If (! In_array ($ _ FILES ["FILENAME"] ["type"], $ TP ))
{
Echo "invalid format ";
Exit;
} // End if
If ($ _ FILES ["FILENAME"] ["name"])
{
$ File1 = $ _ FILES ["FILENAME"] ["name"];
$ File2 = $ path. Time (). $ file1;
$ Flag = 1;
} // End if
If ($ flag) $ result = move_uploaded_file ($ _ FILES ["FILENAME"] ["tmp_name"], $ file2 );
// Note that the first parameter passed to move_uploaded_file is the temporary file uploaded to the server.
If ($ result)
{
// Echo "Upload successful! ". $ File2;
Echo "<script language = 'javascript '> ";
Echo "alert (\" uploaded successfully! \");";
Echo "location = 'add _ AAA. php? Pname = $ file2 '";
Echo "</SCRIPT> ";
} // End if

}

?>

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.