我正在探索PHP的文件上传和文本解析功能,但我的第一步是错误的,我无法弄明白。代码的目标是显示一个表单来上传文本,然后显示相应的$_FILES
数组的值。但是,它不起作用 - 它运行时没有错误但不显示print_r($_FILES['upload'])
。我错过了什么?
这是我的Index_txtParsing
文件:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>test</title>
</head>
<body>
<form enctype="multipart/form-data" class="Txt_upload" method='POST' action='index_txtParsing.php'>
<label for="file">Enter upload here:</label>
<input type='file' name='upload'/>
<input type='submit' name='submit' value="upload here"/>
</form>
</body>
</html>
<?php
if(isset($_POST['upload'])){
$upload=$_POST['upload'];
print_r($_FILES['upload']);
}else{
$upload="unknown";
}
?>
编辑:在合并以下建议后,此代码有效:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>test</title>
</head>
<body>
<form enctype="multipart/form-data" class="Txt_upload" method='POST' action='index_txtParsing.php'>
<label for="file">Enter upload here:</label>
<input type='file' name='upload'/>
<input type='submit' name='submit' value="upload here"/>
</form>
</body>
</html>
<?php
if(isset($_FILES['upload'])){
echo "Value of FILES['upload'] ";
print_r($_FILES['upload']);
echo "<br/>";
}else{
}
?>
答案 0 :(得分:3)
要获取上传的文件,请使用$ _FILES而不是$ _POST:
<?php
if(isset($_FILES['upload'])){
$upload=$_FILES['upload'];
print_r($_FILES['upload']);
}else{
$upload="unknown";
}
?>
另请阅读 manual on File Uploads 。
答案 1 :(得分:2)
您的if语句应如下所示:
if ($_FILES['upload']['name']!="")
答案 2 :(得分:1)
不要在POST数组中查找文件,在FILES数组中查找它:
if(isset($_FILES['upload']['tmp_name']) && $_FILES['upload']['size']>0){ //make sure the file has been uploaded correctly
$upload=$_FILES['upload'];
print_r($_FILES['upload']);
}else{
$upload="unknown";
}
$_FILES['name of field']
是一个可以按如下方式访问的数组(来自PHP.net):
$ _ FILES [ 'userfile的'] [ '名称']
The original name of the file on the client machine.
$ _ FILES [ 'userfile的'] [ '类型']
The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
$ _ FILES [ 'userfile的'] [ '尺寸']
The size, in bytes, of the uploaded file.
$ _ FILES [ 'userfile的'] [ 'tmp_name的值']
The temporary filename of the file in which the uploaded file was stored on the server.
$ _ FILES ['userfile'] ['error']
答案 3 :(得分:1)
尝试清除action =“”的值并检查_POST中的submit元素,而不是在_POST中上传,以便获得:
<form enctype="multipart/form-data" method="POST" action="">
<input type="file" name="upload" />
<input type="submit" name="submit" value="upload here"/>
</form>
<?php if ( isset( $_POST['submit'] ) ) { print_r( $_FILES ); } ?>
这应该有用。
答案 4 :(得分:1)
当你试图显示$ _POST ['upload']时,因为它是一个文件而无法使用,所以如果你想要文件的名称,请尝试这样的事情:
if(isset($_POST['submit'])){
print_r($_FILES['upload']);
echo $_FILES['upload']['name'];
}else{
$upload="unknown";
}
正如我在第一点告诉你的,如果你需要检查一个文件是否已被选中进行上传,请尝试更改你的if语句,这样你首先要检查表单是否已经提交,然后如果文件已被提交选自:
if(isset($_POST['submit'])){
if($_FILES['upload']['name']!="")
$upload = $_FILES['upload']['name'];
else
$upload = "no file";
}else{
$upload="form not submitted";
}
echo $upload;
这当然只是一个例子,而不是一个完整的解决方案。在实际情况中,您可能想要检查文件大小,文件类型等...
答案 5 :(得分:0)
你需要
<input type="hidden" name="MAX_FILE_SIZE" value="same_or_lower_than_in_your_php.ini">
在您的表单中
答案 6 :(得分:0)
我相信这是你想要的方向:
// Check if you uploaded a file previously
if(isset($_POST['upload']))
{
// Load the new file
if (!empty($HTTP_POST_FILES['ufile']['tmp_name']))
{
// Extension?
$temp_name = basename($_FILES['ufile']['name']);
$extension = explode(".", $temp_name);
$extension = end($extension);
$filename .= $extension;
$path = $uploaddir . $filename;
if ($ufile != none)
{
if (copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
// It copied to where ever you want it to go!
}
else
//error
}
else
//error
}
else
//error
}