How to upload a CSV file to php

Written by tiesha whatley
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to upload a CSV file to php
Upload a CSV file to the web server with PHP to be used later in a database. (database on paper image by .shock from Fotolia.com)

The PHP scripting language allows you to execute functions on a web server. One of the things that you can do with PHP is to upload files to the server. CSV, or comma-separated values, is a file that holds several rows of data. It can be imported or exported into a database and usually holds the data that corresponds with the PHP coding. To upload CSV files with PHP, create a form that will be used as an interface to search and select the files for upload. Specify in the PHP coding that only CSV files are allowed.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Start a new web page in a program like Notepad (text-editing) or Dreamweaver (web-authoring). For web-authoring programs, switch to "HTML" or "code" view to input the code needed to complete this task.

  2. 2

    Type in the code to create the user interface for uploading the CSV files. A HTML form is sufficient.

    "<html>

    <head>

    <body>

    <form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">

    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />

    Choose a CSV file to upload: <input name="upload_file" type="file" />

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

    </form>"

    Do not type in the quotation marks that starts and ends the line of code; you will receive an error message if you do. This is a simple form with two fields; one to browse for the CSV file that you want to upload and a "Submit" button to start the PHP code within the same document that will handle the uploading of the file.

  3. 3

    Type in the coding that checks to make sure that the file is in the acceptable file type, which is a CSV file type.

    "<?php

    $allowed Extension = array("csv");

    foreach ($_FILES as $file) {

    if ($file['tmp_name'] &gt; '') {
    
      if (!in_array(end(explode(".",
    
            strtolower($file['name']))),
    
            $allowedExtension)) {
    
       die($file['name'].' is an invalid file type!&lt;br/&gt;'.
    
        '&lt;a href="javascript:history.go(-1);"&gt;'.
    
        '&lt;&lt; Go Back&lt;/a&gt;');
    
      }
    
    }
    

    } "

    This code checks to see if the file that the user is trying to upload is a CSV file. If it is not, the user will see an error message.

  4. 4

    Type in the PHP code that continues handling the CSV file once it has been certified as a valid file type.

    "$target_path = "CSV_Files/";

    $target_path = $target_path . basename( $_FILES['upload_file']['name']);

    if(move_uploaded_file($_FILES['upload_file']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['upload_file']['name']).
    
    " has been uploaded";
    

    } else{

    echo "There was an error trying to upload the file. Check the file and file location before trying again!";
    

    }

    ?>

    </body>

    </head>

    </html>"

    The code starts off setting the folder on the server that the CSV files will be kept. Change the "CSV_Files" to the name of the folder on your server that you want to save the uploaded CSV files to.

  5. 5

    Save the document with a .php file extension. Upload it to your web server in the same directory that holds the folder that the CSV files will be uploaded to. For instance, if the folder is found at "<root directory>/CSV_Files," upload the PHP file to the root directory folder.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.