Use the File function library in PHP to upload an image File and display it.

Source: Internet
Author: User

<? 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 ");

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.