php如何获得调用网页的网址

 我来答
匿名用户
推荐于2016-09-03
展开全部
用 file()函数
下面的代码是
通过PHP的File函数库来完成上传图像文件并让其显示

// FILE 1: DISPLAY AND PROCESS ENTRY FORM AND UPLOAD PICTURE FILE TO SERVER 
<?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"); 
        if(!$out) { 
            print_error("Cannot open file: $filename"); 
            break; 
        }
        // write the server picture name to the server name file, and close the server 
        // name file 
        fputs($out,$name); 
        fclose($out);
        // display message that client picture file was successfully copied to the 
        // server, display a prompt to look at updated server photo gallery, and supply 
        // the HTML link
?>
        Picture added. Thanks.<br> 
        <a href="upload_display.php">Continue to the gallery</a>
<?php
        // exit to the server photo gallery 
        exit(); 
    } 
} while(false);
// you get to here only when "if(isset($picture))" is false, which means that 
// no picture name has been submitted, therefore go display the input form where 
// the necessary information can be entered
?>
<!-- start upload form --> 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> 
<html> 
<head> 
    <title>Photo gallery - add</title> 
</head>
<body bgcolor="white"> 
<h1>Photo gallery add</h1>
<?php
// start of segment of code for displaying input form
// using $PHP_SELF for value of "form action" causes form to refer to itself 
// when "submit" button is clicked
?>
<form action="<? echo $PHP_SELF ?>" method=POST ENCTYPE="multipart/form-data">
<?php
// pass the PHP constant MAX_FILE_SIZE to the HTML maximum file size 
// constant MAX_FILE_SIZE
?>
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="<? echo MAX_FILE_SIZE ?>">
<?php
// display the text boxes for entering user name and picture name, and store 
// the entered values in PHP variables; browsing is enabled
?>
Your name is: <INPUT NAME="name"><br> 
Your picture: <INPUT NAME="picture" TYPE="file"><br>
<?php
// display the "submit" button
?>
<INPUT TYPE="submit" VALUE="Add picture" name="send">
</form> 
</body> 
</html>

// ------------------------------------------------------ 
// FILE 2: DISPLAY THE SERVER PHOTO GALLERY
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> 
<html> 
<head> 
    <title>Photo gallery</title> 
</head>
<body> 
<h1>Photo gallery</h1>
<?php
// full directory path 
$filepath = "/home/httpd/html/tut/upload";
// user's path in browser -- same as full directory path 
$url_path = "/tut/upload";
// get unique server directory used for this user session 
$dir = dir($filepath);
// loop through all server subdirectories for this user session 
while($entry=$dir->read()) { 
    // if entry is system file (doesn't have picture files), go to next entry in 
    // "while" loop 
    if($entry == "." || $entry == "..") { 
        continue; 
    }
    // open server name 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, go to next entry in "while" loop 
    $fp = @fopen("$filepath/$entry/name","r"); 
    if(!$fp) { 
        print "Bad entry: $entry<br>"; 
        continue; 
    }
    // get name of the server picture file and close the server name file 
    $name = fgets($fp,4096); 
    fclose($fp);
    // display each picture and its file name; in addition, "alt=" causes the file 
    // name to be displayed as ToolTip text when mouse points to picture
?>

    <img src="<? echo "$url_path/$entry/picture" ?>" 
     alt="<? echo $name ?>"> <b><? echo $name ?></b><br>
<? 

?>
</body> 
</html>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式