php - How do i check if my file was uploaded and then echo my file uploaded statement to paticular part of my website
Get the solution ↓↓↓Solution:
move_uploaded_file
returns true on success. In upload.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");
}
And in form.php;
<?php
.....
$res = $_GET["res"];
......
?>
<div>
<?php if ($res == "success" && !empty($_SESSION["res"])) {
echo "File uploaded";
} else {
echo "Error occured";
}
unset($_SESSION["res"]);
?>
</div>
.....
Answer
Solution:
In upload.php, use this:
<?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");
}
?>
And in form.php, check using this:
<?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>
.....
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot access offset of type string on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.