Php 显示从$\u文件上载的文件
我在将文件从HTML上传到基于PHP的web服务器时遇到了一些问题。 下面是我的HTML代码。我相信这里没有问题Php 显示从$\u文件上载的文件,php,html,web-applications,Php,Html,Web Applications,我在将文件从HTML上传到基于PHP的web服务器时遇到了一些问题。 下面是我的HTML代码。我相信这里没有问题 <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data" action="2.2.2.cgi"> <input type="file" name="file" id="file" size="35"><
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data" action="2.2.2.cgi">
<input type="file" name="file" id="file" size="35"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
<form action="upload.php" method="post"
enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" size="35"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
PHP代码:
<?php
define ('MAX_FILE_SIZE', 1000000);
$permitted = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg', 'text/plain');
if ($_FILES['file']['type'] == $permitted
&& $_FILES['file']['size'] > 0
&& $_FILES['file']['size'] <= MAX_FILE_SIZE) {
move_uploaded_file($_FILES, $uploadedfile);
echo ('<img src="'.$uploadedfile'" />');
}
?>
<form action="upload.php" method="post"
enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" size="35"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
我不知道如何让它在PHP中工作。我一直在尝试几种不同的方法,上面的代码只是我最新的尝试,通过尝试将值存储到一个新变量中来解决这个问题。我想做的是:
<form action="upload.php" method="post"
enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" size="35"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
- 限制可以上载的文件的类型和大小李>
- 文件是图片吗?显示图片
- 它是纯文本文件吗?显示文件的内容
- 这是未经许可的文件吗?显示文件的名称、类型和大小
<form action="upload.php" method="post"
enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" size="35"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
另外,它不会上线,所以目前任何安全问题都无关紧要。好吧。完全测试并注释了一段代码
<form action="upload.php" method="post"
enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" size="35"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
upload.php
define ('MAX_FILE_SIZE', 1000000);
$permitted = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg', 'text/plain'); //Set array of permittet filetypes
$error = true; //Define an error boolean variable
$filetype = ""; //Just define it empty.
foreach( $permitted as $key => $value ) //Run through all permitted filetypes
{
if( $_FILES['file']['type'] == $value ) //If this filetype is actually permitted
{
$error = false; //Yay! We can go through
$filetype = explode("/",$_FILES['file']['type']); //Save the filetype and explode it into an array at /
$filetype = $filetype[0]; //Take the first part. Image/text etc and stomp it into the filetype variable
}
}
if ($_FILES['file']['size'] > 0 && $_FILES['file']['size'] <= MAX_FILE_SIZE) //Check for the size
{
if( $error == false ) //If the file is permitted
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); //Move the file from the temporary position till a new one.
if( $filetype == "image" ) //If the filetype is image, show it!
{
echo '<img src="upload/'.$_FILES["file"]["name"].'">';
}
elseif($filetype == "text") //If its text, print it.
{
echo nl2br( file_get_contents("upload/".$_FILES["file"]["name"]) );
}
}
else
{
echo "Not permitted filetype.";
}
}
else
{
echo "File is too large.";
}
<form action="upload.php" method="post"
enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" size="35"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
define('MAX\u FILE\u SIZE',1000000);
$allowed=数组('image/gif','image/jpeg','image/png','image/pjpeg','text/plain')//设置许可证文件类型的数组
$error=true//定义一个错误布尔变量
$filetype=“”//只需将其定义为空。
foreach($key=>$value)//运行所有允许的文件类型
{
if($\u FILES['file']['type']=$value)//如果实际允许使用此文件类型
{
$error=false;//耶!我们可以通过
$filetype=explode(“/”,$\u FILES['file']['type']);//保存文件类型并在/
$filetype=$filetype[0];//获取第一部分的.Image/text等并将其跺到filetype变量中
}
}
如果($\u FILES['file']['size']>0&&$\u FILES['file']['size']为什么有action=“2.2.2.cgi”
?请阅读。将文件从临时位置复制到公共目录,然后创建适当的链接否,您的答案是错误的。没有定义$uploadedfile
,并且移动上传的文件
使用不当,因为他应该指向tmp\u name
键谢谢彼得,时间不早了。编辑了我的答案。感谢您的帮助伙计们。@PeterSzymkowski:我可能应该更仔细地阅读手册。我对这方面很陌生,所以我还在学习诀窍。