0

Hello friends, Please help me to sort out my problem , actually it is strange one and don't know why it appears.
Actually I was trying to make a file upload form which I got success although a bit. A bit in the sense, if I include just <form> in the body tag it works, but if i include some more Html along with it stops working ? Have anyone gone through same problem. Please someone helps me out.

This is the code snippet :-

<?php    
        if(isset($_POST['submit'])) {
            echo "<pre>";
            print_r($_FILES);
            echo "</pre><br />";

            if($_FILES['file']['error'] == 0) {
                echo "File uploaded successfully";
            } else {
                echo "File not uploaded check it again";
            }

            echo $_FILES['file']['name'];
            echo "<br />".$_FILES['file']['tmp_name'];

            echo "<br /><hr /> ";
            /// NOw to move the files to the the server
            $target = "/var/www/php_sandbox/";

            $target = $target.basename($_FILES['file']['name']);

            $ok = 1;
            if(!file_exists($target)) {
                if(move_uploaded_file($_FILES['file']['tmp_name'],$target)) {
                    echo "The file at ".$_FILES['file']['tmp_name']." moved successfully ";
                    header("Location: index.php");
                    exit(1);
                } else {
                    echo "Sorry there was probleming uploading files. PLease resolve it soon. :)";
                }    
            } else {
                echo "Files already exists \n";
            }
    }
    ?>    
        <form id="fileUpload" action="" method="post" enctype="multipart/form-data" >
            <label for="file">File :</label>
            <input type="file" name="file">
            <br />
            <input name="submit" type="submit" value="Submit Data">
        </form>   
2
Contributors
6
Replies
34
Views
2 Years
Discussion Span
Last Post by Gurjit_1
0

If I add some html above the starting <?php ---- ?>
Apologies for not posting the complete file,,sory for that. Below is complete file

<!DOCTYPE html>
<html>
    <head>
        <title>File Uploading Form</title>
    </head>
    <body>
        <header>
            <nav>
                <ul>
                    <li><a href="#">home</a></li>
                    <li><a href="#">Download files</a></li>
                    <li><a href="#">Contact Us</a></li>
                </ul>
            </nav>
        </header>

        <div class="panel custom_color">
            <div class="row">            
                <div class="large-2 columns">
                    <p></p>            
                </div>
                <div class="large-8 columns centered">
                    <h1 class="give_space">See the magic of scripting !</h1>
                </div>
                <div class="large-2 columns">

                </div>
            </div>

            <div class="row">
                <div class="large-4 columns">
                    <p>
                        <img src="images/text44.png" alt="semantic">
                    </p>
                    <p>
                        Your web sites will have a great professional look with this. All u need is to include the css foundation file in it. 
                    </p>
                </div>

                <div class="large-4 columns">
                    <p>
                        <img src="images/idea.png" alt="semantic">
                    </p>
                    <p>
                        It is best idea to build for mobile first. Then, as devices get larger and larger, layer in more complexity for a complete responsive design 
                    </p>
                </div>

                <div class="large-4 columns">
                    <p>
                        <img src="images/verify.png" alt="semantic">
                    </p>
                    <p>
                        Get verified websites upto the mark with professional looks and all u need to see them run and earn for u. 
                    </p>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="large-2 columns">
                <p></p>
            </div>
            <div class="large-8 columns">
                <form id="file_upload" action="" method="post"
                <div class="row">
                    <div class="large-8 columns">

                    </div>
                </div>
            </div>
            <div class="large-2 columns">
                <p></p>
            </div>
        </div>


        <div class="row">
            <div class="small-6 large-centered columns">
                <h1>FILES UPLOAD</h1>
            </div>
        </div>
    <?php    
        if(isset($_POST['submit'])) {
            echo "<pre>";
            print_r($_FILES);
            echo "</pre><br />";

            if($_FILES['file']['error'] == 0) {
                echo "File uploaded successfully";
            } else {
                echo "File not uploaded check it again";
            }

            echo $_FILES['file']['name'];
            echo "<br />".$_FILES['file']['tmp_name'];

            echo "<br /><hr /> ";
            /// NOw to move the files to the the server
            $target = "/var/www/php_sandbox/";

            $target = $target.basename($_FILES['file']['name']);

            $ok = 1;
            if(!file_exists($target)) {
                if(move_uploaded_file($_FILES['file']['tmp_name'],$target)) {
                    echo "The file at ".$_FILES['file']['tmp_name']." moved successfully ";
                    header("Location: index.php");
                    exit(1);
                } else {
                    echo "Sorry there was probleming uploading files. PLease resolve it soon. :)";
                }    
            } else {
                echo "Files already exists \n";
            }
    }
    ?>    
        <form id="fileUpload" action="" method="post" enctype="multipart/form-data" >
            <label for="file">File :</label>
            <input type="file" name="file">
            <br />
            <input name="submit" type="submit" value="Submit Data">
        </form>   

    </body>
</html>
0

The problem is that you are using
header("Location: index.php");
at line 108.
The header command cannot have any output before it on the page. PHP is fine of course but the minute you put any HTML before it you run into problems. You will either need to relocate your code or use output buffering (google will help).

0

It is working fine @herciles . but still if i remove those lines too , the form is still not working. Again the same problem persists

0

If I use the html I always got the $_Files['file'] array empty and keep on getting undefined index file : on line 105 .

Edited by Gurjit_1: Mentioned some more things

0

Actually I have found the answer , and it is actually my mistake on the line number 65 , I have left a form tag open which is causing the problem and not allowung the file to be opened.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.