php - How do i check if my file was uploaded and then echo my file uploaded statement to paticular part of my website

723

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>

.....
614

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>

.....

People are also looking for solutions to the problem: php - Regular Expression working in online test tool, but not in my app

Source

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.

Ask a Question

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.

Similar questions

Find the answer in similar questions on our website.