简体   繁体   中英

upload doc, docx, pdf files on server using php

I wish to upload documents having extensions doc, docx and pdf on server. for this i used the following code but i am not able to upload the files

<?php
if($_POST['save'])
    {

        $target_dir = "reqdoc/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        if (file_exists($target_file)) 
            {
                echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
        if ($_FILES["fileToUpload"]["size"] > 500000)
            {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
        if($imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "pdf" ) 
            {
                echo "Sorry, only doc, docx, pdf";
                $uploadOk = 0;
            }
        if ($uploadOk == 0) 
            {
                echo "Sorry, your file was not uploaded.";
            } 
        else 
            {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
                    {
                        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                    } 
                else 
                    {
                        echo "Sorry, there was an error uploading your file.";
                    }
            }
    }
?>

<p>&nbsp;</p>
<form class="form-horizontal" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <button type="submit" value="submit" name="save">Save</button>
</form>

i got the error saying

Sorry, file already exists.Sorry, only doc, docx, pdfSorry, your file was not uploaded.

can anyone please tell where the code went wrong

Try this....

you are miss 'enctype="multipart/form-data"' in your form

you are writing client-side code, all you need to know is use multipart/form-data when your form includes any elements.

http://www.faqs.org/rfcs/rfc1867.html

<?php
if($_POST['save'])
    {

        $target_dir = "media/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        if (file_exists($target_file)) 
            {
                echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
        if ($_FILES["fileToUpload"]["size"] > 500000)
            {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
        if($imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "pdf" ) 
            {
                echo "Sorry, only doc, docx, pdf";
                $uploadOk = 0;
            }
        if ($uploadOk == 0) 
            {
                echo "Sorry, your file was not uploaded.";
            } 
        else 
            {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
                    {
                        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                    } 
                else 
                    {
                        echo "Sorry, there was an error uploading your file.";
                    }
            }
    }
?>

<p>&nbsp;</p>
<form class="form-horizontal" enctype="multipart/form-data" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <button type="submit" value="submit" name="save">Save</button>
</form>

您忘记在表单标签中放入enctype="multipart/form-data"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM