Merge not working

I am trying to get the information in a csv file that is uploaded to merge with a database after the file is moved to a particular folder based on the extension.

I have a working script to merge information to the database, which I modified to upload and move the file into a particular folder. That works really well. THe problem is that the merge doesn’t work anymore.

I am thinking that the problem is from what I had to do to modify the form. I changed the input type to ‘file’ from text… along with some other changes.


case 'csv':
								$destfile='C:\\wamp\\www\\Test\\Upload\\Files\\csv\\\\' . basename($_FILES['upload']['name']);
								$ret = move_uploaded_file($_FILES['upload']['tmp_name'], $destfile);
								switch ($ret)
									{
										case false:
											echo htmlspecialchars('Unable to move file', ENT_QUOTES, 'utf-8');
										break;
										default:
											$sql="LOAD DATA LOCAL INFILE '" . $_POST['upload'] . ".csv'
								             INTO TABLE page
									         FIELDS TERMINATED BY ','
								             LINES TERMINATED BY '\\r\
'
								             IGNORE 1 LINES
								             (id, caption);";
											 echo ($sql); 
											 mysqli_query ($link, $sql);
											 include 'upload.html.php';
											 echo htmlspecialchars('File uploaded successfully', ENT_QUOTES, 'utf-8');
										break;
									}
														
							break;

Here is the form code


<form enctype="multipart/form-data" action="index.php" method="post">
	<div> 
        <label for="upload">File to Be Uploaded:</label> 
        <input name="upload" type="file" size="45"/>
	</div> 
    <p></p>
    <div>
	 	<input type="submit" name="file_upload" value="Upload File"/>
	</div>
</form>

Is there a way to assign both text and file to the input type of the form?

$_POST[‘upload’]

the file name is in the $_FILES array not $_POST, but you should just use $destfile

Tried that as well… Same thing no errors… but the database does not update

I would probably try having mysql print out any errors it may be encountering.

From mysql manual:

  • For security reasons, when reading text files located on the server, the files must either reside in the database directory or be readable by all.
  • Also, to use LOAD DATA INFILE on server files, you must have the FILE privilege.
  • Using LOCAL you do not need the FILE privilege to load local files … but … LOCAL works only if your server and your client both have been enabled to allow it. For example, if mysqld was started with --local-infile=0, LOCAL does not work.

Try checking query for errors:


mysqli_query ($link, $sql)
    or exit('query failed: '.mysqli_error($link));

  • Note that it is currently not possible to load data files that use the ucs2, utf16, or utf32 character set.

Here is the error that came up

query failed: Can’t find file ‘C:wampwwwTestUploadFilescsvBook1.csv’.

I also had it output the actual search string that it generates and here it is:

LOAD DATA LOCAL INFILE ‘C:\wamp\www\Test\Upload\Files\csv\Book1.csv’ INTO TABLE page FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ’ ’ IGNORE 1 LINES (id, caption)

I also tried doubling up the slashes and it came up with the same error and search string…

Solution 1: you need 4 backslashes in quotes (mysql will see 2 of them, so it will be escaped properly).

2 backslashes in quotes = 1 backslash:


$s = '\\\\';
echo $s;

Output:


\\

4 backslashes in quotes = 2 backslashes:


$s = '\\\\\\\\';
echo $s;

Output:


\\\\

Solution 2: escape it using mysqli_real_escape_string:


$destfile='C:\\wamp\\www\\Test\\Upload\\Files\\csv\\\\' . basename($_FILES['upload']['name']);
$destfile = mysqli_real_escape_string($link, $destfile);

Solution 3: you can just go with forward slash, and 1 will be enough:


$destfile='C:/wamp/www/Test/Upload/Files/csv/' . basename($_FILES['upload']['name']);

Awesome…Thanks for the help all…