<? Php
// Full directory path
$ Filepath = "/home/httpd/html/tut/upload ";
// 200 K is the maximum (picture) file size to be accepted
Define ("MAX_FILE_SIZE", 200*1024 );
Function print_error ($ err ){
Echo "}
Do {
// Check if picture name variable has a value; if not, skip to
// "While (false)" section of "do" statement
If (isset ($ picture )){
// Here is where the server transparently checks that the client picture file
// Doesn' t exceed maximum allowable size
If (getenv ("CONTENT_LENGTH")> MAX_FILE_SIZE ){
Print_error ("File too large: $ picture_name ");
Break;
}
// Open client picture file for read only; "@" prefix tells fopen not to print
// Message if there is an error, since function print_error does that
// If there is an error, break out of "do" loop and continue at "while (false )"
$ Fp = @ fopen ($ picture, "r ");
If (! $ Fp ){
Print_error ("Cannot open file: $ picture_name ");
Break;
}
// Generate unique name for session, use it to generate unique server
// Directory name, and create the directory
Srand (double) microtime () * 1000000 );
$ Id = md5 (uniqid (rand ()));
$ Dirname = "$ filepath/$ id ";
Mkdir ($ dirname, 0700 );
// Create the server picture file in the newly created server directory
$ Filename = $ dirname. "/picture ";
// Open server picture file for write only; "@" prefix tells fopen not
// Print message if there is an error, since function print_error does that
// If there is an error, break out of "do" loop and continue at "while (false )"
$ Out = @ fopen ($ filename, "w ");
If (! $ Out ){
Print_error ("Cannot open file: $ filename ");
Break;
}
// Copy client picture file to server picture file
While ($ buffer = fread ($ fp, 8192 )){
Fwrite ($ out, $ buffer );
}
// Close client picture file and server picture file
Fclose ($ fp );
Fclose ($ out );
// Create server name file in picture file directory; this file will hold
// Name of the picture file
$ Filename = $ dirname. "/name ";
// Open server name file for write only; "@" prefix tells fopen not to print
// Message if there is an error, since function print_error does that
// If there is an error, break out of "do" loop and continue at "while (false )"
$ Out = @ fopen ($ filename, "w ");