Home > Backend Development > PHP Tutorial > felayman——Upload pictures to the server in PHP_PHP tutorial

felayman——Upload pictures to the server in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:27:41
Original
935 people have browsed it

1.upload_file.php

//This 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 file extension

*Enter description here ...

* @param unknown_type $filename

*/

function getFileName($filename){

//strrchr—Find the last occurrence of the specified character in the string

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

}

/**

* Randomly generate n-digit string

* 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

$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

to get the name of the uploaded file and generate the file name

require 'upload_file.php';

//Set the directory where the file is to be saved

$upload_dir = "files/";

if(!file_exists($upload_dir)){

mkdir($upload_dir);

}

//Set the file type to be uploaded

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

//in_array — Check if a value exists in the array

if(!in_array(strtolower(getFileName($_FILES['file']['name'])), $type)){

//implode, combine arrays into a string

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

echo "<script>alert('The file type is only allowed to be {$text}');window.location='index.html';</script>";

}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 — Determine whether the file was uploaded via HTTP POST

if(is_uploaded_file($_FILES['file']['tmp_name'])){

//move_uploaded_file — Move the uploaded file 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 "

Your file has been uploaded, upload image preview:
";

echo "felayman——Upload pictures to the server in PHP_PHP tutorial
";

echo "Continue uploading";

}else{

echo "Upload failed";

}

}

}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815137.htmlTechArticle1.upload_file.php //This 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 file extension *Enter desc...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template