> php教程 > php手册 > 通过PHP的File函数库来完成上传图像文件并让其显示

通过PHP的File函数库来完成上传图像文件并让其显示

WBOY
풀어 주다: 2016-06-13 10:02:58
원래의
1267명이 탐색했습니다.


// full directory path
$filepath = "/home/httpd/html/tut/upload";

// 200K is the maximum (picture) file size to be accepted
define("MAX_FILE_SIZE", 200*1024);

function print_error ($err) {
echo "

$err


";
}

do {
// check if picture name variable has a value; if not, skip to the
// "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 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");
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 the
// 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");
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿