程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 通過PHP的File函數庫來完成上傳圖像文件並讓其顯示

通過PHP的File函數庫來完成上傳圖像文件並讓其顯示

編輯:關於PHP編程

<?php

// 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 "<h1>$err</h1><hr>";
}

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved