Question

I am writing a page where it allows user to upload a file into my remote database.

So when the user has successfully uploaded the file, my label will display "File Uploaded Successfully".

If the file is not of the type that my server accepts, the label will display "Incorrect file format. Please try again."

Is it possible?

<form method="post" role="form" action="/LS/import_export.html">

<div class="form-group">

<input type="file" name="file" id="file" size="75">
<label for="file">PHP Code here?</label>

</div>

<button type="submit" name="Import" value="Import">Upload</button>

</form>
Was it helpful?

Solution

This is for you to just get started with the basics.

 <?php
    if( isset($_FILES["file"]) and $_FILES["file"]["tmp_name"] != ""){
        /* Do your validation here
            Reference URL
            http://www.w3schools.com/php/php_file_upload.asp
        */

        if($move_file){
            $status = "File Uploaded Successfully";
        } else {
            $status = "Incorrect file format. Please try again.";
        }
    }
    ?>

<form method="post" role="form" method="post" enctype="multipart/form-data">
  <div class="form-group">
  <input type="file" name="file" id="file" size="75">
  <label for="file">PHP Code here?</label>
  </div>
  <button type="submit" name="Import" value="Import">Upload</button>
</form>

OTHER TIPS

Yes you will want to check the

$_FILES['file']['type']

of your upload which you can compare to an array of exceptable file formats, something like

$aExceptedFormats = array('image/jpg', 'image/png');
if (!in_array($_FILES['file']['type'], $aExceptedFormats)) {
    echo "Incorrect file format. Please try again.";
}
$con = mysql_connect(...) or die(mysql_error());
$db = mysql_select_db('database', $con);
            if($db)
                 {
                    $query = "INSERT INTO upload(u_name,name,size,type,content) VALUES (specific values ...)";
                    mysql_query($query) or die('Error, query failed'); 
                    mysql_close();
                    echo "<ul class='testimonials'>
                            <li>
                            <blockquote>
                                <center><span style='color: green;'>File Uploaded Successfully</span></center>
                            </blockquote>
                            </li>
                          </ul>";
                }
                else 
                { 
                    echo "<ul class='testimonials'>
                            <li>
                            <blockquote>
                                <center>span style='color: red;'>File Uploading Failed</span></center>
                            </blockquote>
                            </li>
                          </ul>"; 
                }
            } 

you can try this..

Change your action attribute to blank or use $_SERVER['PHP_SELF'].

Place your POST action before your form.

Note: use enctype="multipart/form-data" for file uploads

eg.

if(isset($_POST['Import'])){
if ($_FILES["file"]["error"] > 0) {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}

<form method="post" role="form" action="/LS/import_export.html" enctype="multipart/form-data">

<div class="form-group">

<input type="file" name="file" id="file" size="75">
<label for="file">PHP Code here?</label>

</div>

<input type="submit" name="Import" value="Upload" /> 

</form>

For a reference about PHP upload. You may check it here.

you can restrict used by using HTML5 accept attribute :

<input type="file" name="my-image" id="image" accept="image/gif, image/jpeg, image/png" />

OR using PHP

$approved_types = array("image/png","image/jpg","image/jpeg");
$approved_exts = array("png","jpg","jpeg");
if (!in_array($_FILES['new_image']['type'], $approved_types)) {
   die("Wrong type of file submitted press use your back button and try again.");
   }

Refernece : http://www.dreamincode.net/forums/topic/148485-restricting-file-types-and-size-in-upload-form/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top