Import of CSV file with PHP works offline but not with my ISP

Posted on 2011-10-29
Last Modified: 2012-05-12
Dear All,

I'm trying to import 2 CSV files from a local computer to the database of my ISP.
The code I wrote works fine in my offline develop environment but it does not work when I upload the files to the server of my ISP.

When running the code online it loops and gives me 1.5 million empty records in the first table.

Any help would be welcome.

Kind regards.


Question by:Geerd
    LVL 5

    Accepted Solution

    Probably has to do with how you handle the file upload.


                         $handle = fopen("$filename", "r");

    try ->  

            $uploads_dir = 'your/upload/dir';
            $tmp_name = $_FILES['filename1']['tmp_name'];
            $name = $_FILES['filename1']['name'];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
            $handle = fopen("$uploads_dir/$name", "r");
    LVL 1

    Author Comment

    Thank you hex1.

    I do have a couple of questions about how to implement your solution.

    $uploads_dir = 'your/upload/dir';

    What should I replace 'your/upload/dir' with? Should I use a local path ie: c:\upload\  from my local computer or should I use ''

    $tmp_name = $_FILES['filename1']['tmp_name'];

    Where did the tmp_name come from? I only have the filename.

    $name = $_FILES['filename1']['name'];

    Again, where does the 'name' come from?

    Thanks again for helping me.

    LVL 5

    Expert Comment


    you need to choose neither, but the directory of the server where your files are being stored.

    you can try setting it relative (f.e. $uploads_dir='./uploads')

    (be sure to create the folder in the directory where your script lies in and set the permissions so that the web server can write in there (777 f.e.)).

    the filename and temp name comes from your upload from, the form field should look like this:

    <input type="file" name="filename1" id="filename1" />
    <input type="file" name="filename2" id="filename2" />

    and be sure that the form enctype is set to multipart/form-data

    <form action="yourphpfile.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
    LVL 1

    Author Comment

    I made the changes you've suggested but there is still one issue.

    The input type 'file' statements makes me select a file from my local PC. I cannot type a file name manually. I made the upload directory on the server and placed the files that need to be imported there. But when I click the 'file' button I can only choose local files.

    Any ideas?

    Expert Comment

    how big are the 2 files ? You should always think about the php.ini limitation on upload files, witch default is 8MB.

    You can always print out the errors you get from file uploading.
    Here's a little help.

    after you send it to your php, from the form use this.

    <form action="action.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <input type="file" name="filename1" id="filename1" />
    <input type="file" name="filename2" id="filename2" />


    // ######## action.php
    $file1 = $_FILES['filename1'];
    $file2 = $_FILES['filename2'];

    $upErrors = array(
                    UPLOAD_ERR_OK                     => "No Error.",
                UPLOAD_ERR_INI_SIZE          => "File is too large.",
                UPLOAD_ERR_FORM_SIZE      => "File is too large.",
                UPLOAD_ERR_PARTIAL        => "Partial upload.",
                UPLOAD_ERR_NO_FILE        => "No file.",
                UPLOAD_ERR_NO_TMP_DIR     => "No Temporary Directory.",
                UPLOAD_ERR_CANT_WRITE         => "Can't write to disk.",
                UPLOAD_ERR_EXTENSION        => "File upload stopped by extension."


    //now for each file, apart from name, tmpname
    echo $upErrors[$file1['error']];

     now you should see what prints out. I'm sure you can take it from there

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now