我有两个php文档。 form.php
和upload.php
.表单.php显示我的上传文件按钮,最终用户可以看到它。 upload.php
执行操作,它对用户不可见,但具有include("form.php");
语句,因此我在此页面上回显的任何内容都会返回到我的表单.php页面显示。
首先,我想知道如何使用 if 语句检查我的上传代码,以查看文件是否正确上传到服务器。 我的上传代码是
法典
move_uploaded_file($_FILES['file']['tmp_name'],"./medetrax_backup/{$_FILES['file']['name']}");.
其次,如果上传成功,我希望 if 语句回显"文件已上传"或回显"文件未上传"。
最后,由于回声被回显回我的形式.php我想知道如何在我的form.php
的特定div中回显消息。而不是在页面底部回响。
move_uploaded_file
在成功时返回 true。上传中.php;
<?php
....
$res = move_uploaded_file($_FILES['file']['tmp_name'],"./medetrax_backup/{$_FILES['file']['name']}");
$_SESSION["res"]=true;
if ($res) {
header("Location:form.php?res=success");
} else {
header("Location:form.php?res=error");
}
在形式上.php;
<?php
.....
$res = $_GET["res"];
......
?>
<div>
<?php if ($res == "success" && !empty($_SESSION["res"])) {
echo "File uploaded";
} else {
echo "Error occured";
}
unset($_SESSION["res"]);
?>
</div>
.....
在 upload.php 中,使用以下命令:
<?php
....
try
{
$res = move_uploaded_file($_FILES['file']['tmp_name'],"./medetrax_backup/{$_FILES['file']['name']}");
}
catch(Exception $e)
{
echo "An exception occurred while uploading the file. Process will quit now";
die;
}
if (isset($res))
{
header("Location:form.php?res=success");
}
else
{
header("Location:form.php?res=error");
}
?>
在表格.php中,使用这个检查:
<?php
.....
$res = isset($_GET["res"])?$_GET["res"]:false;
......
?>
<div>
<?php if ($res)
{
if($res=="success")
echo "File uploaded";
else
echo "Failed to upload file";
}
else
{
//draw your upload button here, in same file.
}
?>
</div>
.....