PHP/Form/is uploaded file

Материал из Web эксперт
Перейти к: навигация, поиск

Checking for the existence of an uploaded file

 
<?php
if (!is_uploaded_file($HTTP_POST_FILES["upload_file"]["tmp_name"])) {
    $error = "You must upload a file!";
    unlink($HTTP_POST_FILES["upload_file"]["tmp_name"]);
}
else {
    
}
?>



Uploading a file

 
<?php if ($_SERVER["REQUEST_METHOD"] == "GET") { ?>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"] ?>"
      enctype="multipart/form-data">
<input type="file" name="document"/>
<input type="submit" value="Send File"/>
</form>
<?php } else { 
    if (isset($_FILES["document"]) &&
    ($_FILES["document"]["error"] == UPLOAD_ERR_OK)) {
        $newPath = "/tmp/" . basename($_FILES["document"]["name"]);
        if (move_uploaded_file($_FILES["document"]["tmp_name"], $newPath)) {
            print "File saved in $newPath";
        } else {
            print "Couldn"t move file to $newPath";
        }
    } else {
        print "No valid file uploaded.";
    }
}
?>