I have a an html page with a form where users can upload a .txt file to the server. They choose the file and press an upload button and the file gets uploaded to the website root folder via a PHP script. In this root folder is a .xlsm file that the user use to upload the file they uploaded into an MS SQL server database that runs queries and send the result back to the .xlsm file.
The problem i have is that when a user upload the file, the temp file gets moved (with move_uploaded_file
method) to the website root directory and that saved file gets uploaded to the database, but the user keep getting an error message that the file does not exist. When i copy the file manually from another directory to the root directory the user can upload the data to the database.
It is like the temp file gets moved to the root directory but does not make it available for use. Here is my code:
Html: upload.html
<html>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" />
Choose a file to upload: <input name="nfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<br><br>
<a href='Lostsales.xlsm'>Spreadsheet</a>
</html>
PHP: php.html
<?php
$name = $_FILES["nfile"]["name"];
$tmp_name = $_FILES["nfile"]["tmp_name"];
move_uploaded_file($tmp_name, $name);
echo $tmp_name;
?>
<html>
<form action="upload.html">
<input type="submit" value="Done" />
</form>
</html>
VBA code for uploading data to MS SQL database: Lostsales.xlsm
Sub Button1_Click()
Dim con As New ADODB.Connection
con.Open "Provider=SQLOLEDB.1;Password=valencia;Persist Security Info=True;User ID=lostsls;Initial Catalog=Lost_sales;Data Source=PCS2635\SQLEXPRESS"
con.Execute "BULK INSERT lsdata FROM '" & Cells(7, 1).Value & "' WITH(FIRSTROW = 2, FIELDTERMINATOR='\t' ,ROWTERMINATOR='
')"
con.Close
Set con = Nothing
End Sub
Cells(7, 1).Value
= C:\inetpub\wwwroot\test\lostsls\import.txt
Aparently when the remote user run the VBA macro, he/she gets the message that the file C:\inetpub\wwwroot\test\lostsls\import.txt
does not exist. Where import.txt is the file the user uploaded in the HTML/PHP codes. And when I try it does get uploaded.
I got it right. Instead of using move_uploaded_file($tmp_name, $name)
I used copy($tmp_name, $name)
in my PHP script.
The uploaded_file method doesn't seem to copy the file to a folder and make it available for use.