我正在按照本教程使用php中的echo打印已提交文件的值。 教程:http://www.learncomputer.com/tutorial-uploading-files-in-php/
<?php //Whole php code
echo "<table border=\"1\">";
echo "<tr><td>Client FileName: </td>
<td>" . $_FILES["fileName"]["name"] . "</td></tr>";
echo "<tr><td>File Type: </td>
<td>" . $_FILES["fileName"]["type"] . "</td></tr>";
echo "<tr><td>File Size: </td>
<td>" . ($_FILES["fileName"]["size"] / 1024) . " Kb</td></tr>";
echo "<tr><td>Name of Temporary File: </td>
<td>" . $_FILES["fileName"]["tmp_name"] . "</td></tr>";
echo "</table>";
?>
尽管如此,当我上传并提交图像时,以下结果会打印在屏幕上
"; echo "Client Filename: " . $_FILES["fileName"]["name"] . ""; echo "File Type: " . $_FILES["fileName"]["type"] . ""; echo "File Size: " . ($_FILES["fileName"]["size"] / 1024) . " Kb"; echo "Name of Temporary File: " . $_FILES["fileName"]["tmp_name"] . ""; echo ""; ?>
这不是预期的表,其名称,类型,大小和临时文件的名称均符合预期。我什至尝试用引号“代替',但未成功。我不明白为什么要打印开头的分号,而代码指令”。$ var。“却插入了变量的实际值
编辑:我的PHP版本7.2.1,这是我用来提交并调用我的php代码的html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="jquery.js"></script>
<!-- GOOGLE -->
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<!--<script src="https://apis.google.com/js/platform.js" async defer>
</script> -->
<!-- GOOGLE -->
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
-->
<link rel="stylesheet" type="text/css" href="Tops.css">
<link rel="stylesheet" type="text/css" href="Ejemplo_botones.css">
<link rel="stylesheet" type="text/css" href="Config.css">
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
</head>
<body>
<style>
</style>
<link rel="stylesheet" href="">
<nav>
<a href="Topsitio.html"><img src="logo.png" class=top alt=""></a>
<ul>
<li><a href="/">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Contacto</a></li>
<li><a href="I.php">Iniciar Sesión</a></li>
</ul>
</nav>
<figure>
<img src="shop.png" class=shop alt="">
</figure>
<div class="container">
<div class="Pasos">
<ul>
<a href=""><li class="paso1 paso">Paso 1 </li></a>
<a href=""><li class="paso"> Paso 2 </li></a>
<a href=""><li class="paso"> Paso 3 </li></a>
<a href=""><li class="paso"> Paso 4 </li></a>
<a href=""><li class="paso5 paso"> Paso 5</li></a>
</ul>
</div>
</div>
<div class="registro">
<p id="regi" class="titulo-registro">Cargar foto de Perfil</p>
<div class="ringborder">
<div class="ring">
<i id="profile" class="fas fa-user-tie"></i>
</div>
</div>
<br><br><form action="upload_table.php " method="post" enctype="multipart/form-data">
<div class="contenedor">
<p>
Elige una foto:</p>
<input class="plus" type="file" name="fileName" id="file-5" class="inputfile inputfile-4" data-multiple-caption="{count} files selected" multiple="" hidden>
<label for="file-5"><figure class="box"><svg id="upload" xmlns="http://www.w3.org/2000/svg" width="180" height="170" version="1.1">
<path id="upload" fill="white" heigthstroke="#f00" transform="scale(3.0)" d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"></path>
</svg></figure>
</label>
</div>
<br><input class="subir" type="submit" value="+ Subir foto">
</form>
<br><br>
<div class="next">Siguiente</div>
</div>
<div class="first blau">
<h1><span class="text">Articulos de gran calidad</span><span class="text2">   -   Relojes y Pulseras</span></h1>
</div>
<script type="text/javascript" src="jquery.js"></script>
<script>
</script>
</body>
答案 0 :(得分:3)
没有理由尝试将所有内容放入echo
。让HTML只是纯HTML,并在需要的地方添加PHP数据。
// assuming you were already in PHP
?>
<table border="1">
<tr><td>Client Filename: </td>
<td><?php echo $_FILES['fileName']['name']; ?></td></tr>
<tr><td>File Type: </td>
<td><?php echo $_FILES['fileName']['type']; ?></td></tr>
<tr><td>File Size: </td>
<td><?php echo ($_FILES['fileName']['size'] / 1024); ?> Kb</td></tr>
<tr><td>Name of Temporary File: </td>
<td><?php echo $_FILES['fileName']['tmp_name']; ?></td></tr>
</table>
<?php // back into PHP if you need to for something else
答案 1 :(得分:0)
您必须调试PHP代码,因为它看起来正确。
开始调试的最佳方法是尝试在$ _FILES中使用伪静态值来查看会发生什么情况:
<?php
$_FILES["fileName"] = [];
$_FILES["fileName"]["name"] = 'test.jpg';
$_FILES["fileName"]["type"] = 'image/jpg';
$_FILES["fileName"]["size"] = 134767;
$_FILES["fileName"]["tmp_name"] = '/tmp/temp_filename.jpg';
echo "<table border=\"1\">";
echo "<tr><td>Client Filename: </td>
<td>" . $_FILES["fileName"]["name"] . "</td></tr>";
echo "<tr><td>File Type: </td>
<td>" . $_FILES["fileName"]["type"] . "</td></tr>";
echo "<tr><td>File Size: </td>
<td>" . ($_FILES["fileName"]["size"] / 1024) . " Kb</td></tr>";
echo "<tr><td>Name of Temporary File: </td>
<td>" . $_FILES["fileName"]["tmp_name"] . "</td></tr>";
echo "</table>";
?>
如果它不起作用,请复制一个有效的php文件,将相同的代码放入其中,然后替换其中的upload_table.php
。因为如果upload_table.php
的开头具有BOM(Byte Order Mark can cause display problem in PHP),它将在顶部打印一些空白字符,并可能阻止php代码执行。或者,您可以保存不带BOM的utf8文件。但是我怀疑这与BOM有关,但你永远都不知道。