Felayman -- PHP Tutorial on uploading images to server _ PHP

Source: Internet
Author: User
Felayman -- uploads images from PHP to the server. 1. upload_file.php this file is responsible for obtaining the extension of the uploaded image and the randomly generated file name header (content-type: texthtml; charsetutf-8); *** get file extension * Enterdesc 1. upload_file.php

// Obtain the extension of the uploaded image and the randomly generated file name.

Header ("content-type: text/html; charset = utf-8 ");

/**

* Get the file extension

* Enter description here...

*@ ParamUnknown_type $ filename

*/

Function getFileName ($ filename ){

// Strrchr-find the last occurrence of a specified character in the string

Return substr (strrchr ($ filename, '.'), 1 );

}

/**

* Random generation of n-bit strings

* Enterdescription here...

*@ ParamUnknown_type $ num

*/

Function rand_str ($ num ){

$ Str = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKOLP1234567890 ";

$ Str_len = strlen ($ str)-1;

// Echo $ str_len;

$ S = '';

For ($ I = 0; $ I <$ num; $ I ++ ){

$ S. = $ str [rand (0, $ str_len)];

}

Echo $ s;

}

?>

2.index.html


3. deal_upload.php

Header ("content-type: text/html; charset = utf-8 ");

// Introduce the function lib for obtaining the name of the uploaded file that has generated the file name

Require 'upload _ file. php ';

// Set the directory to save the file

$ Upload_dir = "files /";

If (! File_exists ($ upload_dir )){

Mkdir ($ upload_dir );

}

// Set the file type for running Upload

$ Type = array ('jpg ', 'PNG', 'GIF', 'jpeg ');

// In_array-check whether a value exists in the array

If (! In_array (strtolower (getFileName ($ _ FILES ['file'] ['name']), $ type )){

// Implode, which combines the array into a string

$ Text = implode (',', $ type );

Echo "scriptpolicalert('file names are only allowed when text='{}}location='index.html '; script";

} Else {

// Obtain the file name

$ Filename = explode ('.', $ _ FILES ['file'] ['name']);

$ Filename [0] = rand_str (10 );

$ Name = implode ('.', $ filename );

Bytes;

// Is_uploaded_file-determines whether the file is uploaded through http post.

If (is_uploaded_file ($ _ FILES ['file'] ['tmp _ name']) {

// Move_uploaded_file-move the uploaded file to the new location

If (move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], http://blog.csdn.net/u012332735/article/details/javasuploadfile )){

$ File_path = getcwd (). '\'. http://blog.csdn.net/u012332735/article/details/$uploadfile; echo"

After uploading your file, upload the image to preview it: ";

Echo"
";

Echo "continue Upload ";

} Else {

Echo "Upload failed ";

}

}

}

?>

Http://www.bkjia.com/PHPjc/815137.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/815137.htmlTechArticle1.upload_file.php // The file is responsible for obtaining the extension of the uploaded image and randomly generating the file name header ("content-type: text/html; charset = utf-8 "); /*** get the file extension * Enter desc...

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.