felayman--php picture upload to server _php tutorial

Source: Internet
Author: User
1.upload_file.php

This file is responsible for getting the uploaded image extension and randomly generated file name

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

/**

* Get file name extension

*enter description here ...

* @param unknown_type $filename

*/

function GetFileName ($filename) {

Strrchr-finds the last occurrence of a specified character in a string

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

}

/**

* Random generation of n-bit strings

* Enterdescription here ...

* @param unknown_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");

Introduces the function lib that gets the name of the uploaded file that has generated the filename

Require ' upload_file.php ';

Set the directory to save the file

$upload _dir = "files/";

if (!file_exists ($upload _dir)) {

mkdir ($upload _dir);

}

Set up file types to run uploads

$type =array (' jpg ', ' png ', ' gif ', ' jpeg ');

in_array-checks if a value exists in the array

if (!in_array (Strtolower (GetFileName ($_files[' file ' [' name '])), $type)) {

Implode, union the array into a string

$text = Implode (', ', $type);

echo "";

}else{

Get file name

$filename = Explode ('. ', $_files[' file ' [' name ']);

$filename [0] =rand_str (10);

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

http://blog.csdn.net/u012332735/article/details/$uploadfile = $upload _dir. $name;

is_uploaded_file-to determine if the file was uploaded via HTTP post

if (is_uploaded_file ($_files[' file ' [' Tmp_name '])) {

Move_uploaded_file-moving uploaded files to a new location

if (move_uploaded_file ($_files[' file '] [' tmp_name '],http://blog.csdn.net/u012332735/article/details/$uploadfile) ){

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

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

echo "
";

echo "Continue uploading";

}else{

echo "Upload failed";

}

}

}

?>

http://www.bkjia.com/PHPjc/815137.html www.bkjia.com true http://www.bkjia.com/PHPjc/815137.html techarticle 1.upload_file.php//The file is responsible for obtaining the uploaded image extension and randomly generated filename header ("Content-type:text/html;charset=utf-8");/** * Get 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.