Jump to content

Recommended Posts

I have a bit of code that I have used for years to upload to upoad data from a csv file to a database. I built a website on a Godaddy Server, then after the site was tested and approved, the site was moved another server. I do not have any access to the new server and the code does not work on the new server. I'm sure that it has to do with security issues on the server?

 

The first part of the code that truncates the table did work. So I know that the connection to the database is correct.

 

Here is the code:

 

<?php
$sql3 = "TRUNCATE TABLE this_table";
$result3 = mysql_query($sql3,$db) or die(mysql_error(). "<br />SQL: $sql3");
ini_set('auto_detect_line_endings',true);
if (($handle = fopen($csvfile, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                $sql = "INSERT INTO this_table (first_name,last_name,title,field_1,field_2,field_3,field_4) VALUES(";
                $search = array(" & ",'"',"'",",");
                $replace = array(" & ",'"', '’',",");
        
                for ($c=0; $c < $num; $c++) {
                        $sql .= "'". str_replace($search,$replace,$data[$c]) . "',";                    
                }
                $sql = substr($sql, 0, -1) .")";
                $result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql");    
        }
        fclose($handle);
}
ini_set('auto_detect_line_endings',false);
?>

Link to comment
https://forums.phpfreaks.com/topic/287682-upload-csv-to-mysql-database/
Share on other sites

the code does not work

 

 

sorry, but that's useless information. by not telling us what it does do (even telling us it resulted in a blank page is a clue that helps narrow down the problem), we cannot help you with the many possible things that could go wrong with any piece of code.

 

the only apparent issue with the posted code is it doesn't have any logic in it to tell you if the fopen() statement has failed.

The page does not go blank. The code does not produce any php errors when the csv is uploaded to the site. I say this because the page loads completely.

Does that help any? Sorry, this is difficult for me because I do not have access to the server. I can not ftp into the site.

Maybe it is this code that is not working on their server. I might assume that since it is a server with security on it, that the temporary files are accessed differently. Does that make sense?

 

$csvfile = $_FILES['userfile']['tmp_name'];
$size = $_FILES['userfile']['size'];
 

  • 4 weeks later...

@Peg,
 
I'm with GoDaddy and came across your post.
 
Are you still running into the issues you were experiencing with your code? Additionally, you mentioned the site was built on a Linux Server with GoDaddy, is the Windows Server also with us?
 
If you still are running into issues feel free to reply or send me a private message.

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.