FileDownloader.php中的undefine变量

时间:2017-01-21 15:54:48

标签: php

我收到通知说有undefined variable: ShowErrorPage in C:\xampp\htdocs\dwupload\FileDownloader.php on line 18undefined variable: ErrorMsg in C:\xampp\htdocs\dwupload\FileDownloader.php on line 39

在这里,我展示了我的上传,下载和查看文件:

  1. FileUploader.php

                  上传文件     

    <body>
    
    <?php
    
    $Dir = "files";
    if (isset($_FILES['new_file'])) {
        if (move_uploaded_file($_FILES['new_file']['tmp_name'], $Dir . "/" . $_FILES['new_file']['name']) == TRUE) {
            chmod($Dir . "/" . $_FILES['new_file']['name'], 0644);
            echo "File \"" . htmlentities($_FILES['new_file']['name']) . "\"successfully uploaded. <br />\n";
        }
        else 
            echo "There was an error uploading \"" . htmlentities($_FILES['new_file']['name']) . "\" .<br />\n";
    }
    
    ?>
    
    <form action="FileUploader.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="25000"/><br />
        File to upload: <br />
        <input type="file" name="new_file" /><br /> (25,000 byte limit) <br />
        <input type="submit" name="upload" value="Upload The File" />
        <br />
    </form>
    </body>
    
    </html>
    
  2. ViewFile.php

  3. &#13;
    &#13;
    <!DOCTYPE html>
        <html>
        <head>
        <title>ViewFile</title>
        </head>
        
        <body>
        
        <?php 
        
        $Dir = "files";
        $DirEntries = scandir($Dir);
        foreach ($DirEntries as $Entry) {
        	if ((strcmp($Entry, '.') != 0) && (strcmp($Entry, '..') != 0))
        	echo "<a href=\"FileDownloader.php?filename=$Entry\">". htmlentities($Entry). "</a><br />\n";
        	
        }
        
        ?>
        
        </body>
        
        </html>
    &#13;
    &#13;
    &#13;

    1. FileDownloader.php
    2. &#13;
      &#13;
      <?php
      
      $Dir = "files";
      if (isset($_GET['filename'])) {
      	$FileToGet = $Dir . "/" . stripcslashes($_GET['filename']);
      	if (is_readable($FileToGet)) {
      	}
      	else {
      		$ErrorMsg = "Cannot read \"$FileToGet\"";
      		$ShowErrorPage = TRUE;
      	}
      }
      else {
      	$ErrorMsg = "No filename specifified";
      	$ShowErrorPage = TRUE;
      }
      
      if ($ShowErrorPage) {
      	header("Content-Description: File Transfer");
      	header("Content-Type: application/force-download");
      	header("Content-Disposition: attachment; filename=\"" . $_GET['filename'] . "\"");
      	header("Content-Transfer-Encoding: base64");
      	header("Content-Length: " . filesize($FileToGet));
      	readfile($FileToGet);
      	$ShowErrorPage = FALSE;
      }
      
      ?>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title>DownloadFile</title>
      <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
      </head>
      
      <body>
      <p>There was an error downloading "<?php echo htmlentities($_GET['filename']); ?></p>
      <p><?php echo htmlentities($ErrorMsg); ?></p>
      
      
      
      
      </body>
      
      </html>
      &#13;
      &#13;
      &#13;

      有谁知道为什么它会给我通知?并且FileDownloader.php中没有错误,但是当我尝试下载使用FileUploader.php上传的文件时,它表示上传时出现了错误。有人可以帮帮我吗?我在php中还是新手,我需要有人来指导我。谢谢。

1 个答案:

答案 0 :(得分:0)

在条件语句中直接赋值之前,需要使用默认值声明这些变量。 在文件的开头添加这个

$ShowErrorPage = TRUE;
$ErrorMsg = '';