The first simple PHP image upload program:
Php section
The code is as follows:
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 'Success!
';
Echo 'file size: '. $ _ FILES ['file'] ['size']. 'Byte '.'
';
Echo 'file path: '. $ upfile;
Echo'
'.'
';
$ Dirr = 'upfile /';
$ Dir = opendir ($ dirr );
Echo $ dirr. '-- Listing:
';
While ($ file = readdir ($ dir )){
Echo"
- $ File
";
}
Echo'
';
Closedir ($ dir );
?>
Second:
The code is as follows:
If (empty ($ _ GET [submit])
{
?>
} 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"