好的我有一个页面,用户上传的文件如下所示:
upload.php的:
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
Choose a file to upload: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
upload_file.php:
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
$uploaded_type= $_FILES['uploaded']['type'];
$uploaded_size= $_FILES['uploaded']['size'];
//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
每当我测试它时,网页就会返回此
注意:未定义的索引:在第3行的 \ htdocs \ upload_file.php 上传
注意:第7行的 \ htdocs \ upload_file.php 中的未定义变量:uploaded_size
注意:未定义的变量:第14行的 \ htdocs \ upload_file.php 中的uploaded_type
注意:未定义的索引:在第29行的 A:\ xampp \ htdocs \ upload_file.php 中上传
有人可以告诉我这里我做错了什么。对不起,如果它是一个非常简单的修复。
提前谢谢!
答案 0 :(得分:1)
这里有几个问题。很多小新手程序员的东西,但我做了一些清理工作。最令人震惊的问题是使事情难以阅读的基本格式。所以我清理了一下。另外,我在脚本的开头设置了一个检查,它将进行一项非常基本的检查,看看是否设置了$_FILES['uploaded']
。此外,您还有一个名为$ok
的变量,用于执行基本的TRUE
/ FALSE
检查。我将其调整为实际的TRUE
/ FALSE
值。
另外,您的专栏内容为:
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
没有任何意义,因为脚本的其余部分引用了$_FILES['uploaded']
。所以我也纠正了这一点。
现在已经过了所有这些,当您在脚本中执行基本变量转储时会出现这样的情况:
echo '<pre>';
print_r($_FILES);
echo '<pre>';
这将向您展示$_FILES
数组中的实际内容,以便更轻松地进行调试。哎呀,我用一个基本的TRUE
/ FALSE
开关添加到脚本中,以便您快速启用它。在需要时关闭。
<?php
if (TRUE) {
echo '<pre>';
print_r($_FILES);
echo '<pre>';
}
$ok = TRUE;
// Check if there is an uploaded file.
if (!array_key_exists('uploaded', $_FILES) && !empty($_FILES['uploaded'])) {
echo "Sorry, no file seems to be uploaded.";
$ok = FALSE;
}
$target = "upload/";
$target = $target . basename($_FILES['uploaded']['name']) ;
$uploaded_type = $_FILES['uploaded']['type'];
$uploaded_size = $_FILES['uploaded']['size'];
// This is our size condition.
if ($uploaded_size > 350000) {
echo "Your file is too large.<br>";
$ok = FALSE;
}
//This is our limit file type condition
if ($uploaded_type =="text/php") {
echo "No PHP files<br>";
$ok = FALSE;
}
// Here we check that $ok was not set to 0 by an error
if ($ok) {
echo "Sorry your file was not uploaded";
}
// If everything is ok we try to upload it
else {
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>