Index.htm
<Form enctype = "multipart/form-data" action = "upload. php" method = "post">
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "2000000">
<Input name = "userfile" type = "file">
<Input type = "submit" value = "Upload File">
</Form>
Upload. php
<?
$ F = $ _ FILES [jeff_img_upload];
// Check whether a file is uploaded
If ($ f [error] = 4 ){
Echo ("<script> alert (select the file you want to upload. Thank you !); Location.href1_index.htm; </script> ");
}
Else {
// Error Determination
If ($ f [error]) {
Echo "failed. The file is too large! The error code is ". $ f [error];
Exit;
}
// Type determination
If (substr ($ f [type], 0, 5) = image ){
Switch ($ f [type]) {
Case image/jpeg:
Case image/jpg:
Case image/pjpeg:
$ Ext =. jpg;
Break;
Case image/gif:
$ Ext =. gif;
Break;
Case image/png:
Case image/x-png:
$ Ext =. png;
Break;
Default:
Header (Content-type: text/html; charset = UTF-8 );
Echo ("<script> alert (isn't it an image ?); Location.href1_index.htm; </script> ");
Die (REDIRECTING ...);
Break;
}
} Else {
Header (Content-type: text/html; charset = UTF-8 );
Echo ("<script> alert (isn't it an image ?); Location.href1_index.htm; </script> ");
Die (REDIRECTING ...);
}
// Generate a random number
$ Randomnum = NULL;
For ($ j = 0; $ j <= 5; $ j ++) // the length of a random number. In this example, the length of a random number is 6.
{
Srand (double) microtime () * 1000000 );
$ Randomnumber = rand (! $ J? 1:); // generates a random number, which is not the first number with 0. In some special cases, 0 is omitted by the system.
$ Randomnum. = $ randomnumber;
}
$ Dest_dir =/var/www/htdocs/upload; // you can specify the upload directory.
// Generate the date directory. First, determine whether the date directory exists. If it does not exist, create it.
If (! File_exists (date ("Ymd "))){
Mkdir (date ("Ymd"), 0777 );}
$ Dest = $ dest_dir./. date ("Ymd")./. time (). _. $ randomnum. $ ext; // set the file name to the timestamp and add a random number to avoid duplication.
$ R = move_uploaded_file ($ f [tmp_name], $ dest );
Chmod ($ dest, 0777); // sets the attributes of the uploaded file
}
Echo "succeeded. The file address is:". $ dest;
?>