Hello everyone! I am new to PHP and am trying to redesign my company's Web site from classic ASP to PHP and things have been going relatively well, but I am running into problems now and would appreciate any guidance or tips on this. So thanks in advance!
We are a daily newspaper and have a text file for our classified ads that we upload to a SQL Server table and parse the classified category and ad data into separate columns in the table. I am trying to rewrite this code in PHP, but just can't seem to get the hang of it. I want to upload the text file and parse it into MySQL table I have called SimpleClass. The only PHP code I have written thus far is opening the text file in the browser and splitting the category and ad text using an array.
Can anyone tell me how I can do the following in PHP:
Here is the ASP code I am using:
<% Language = "VBScript" %>
<!--#include file="../../Connections/JCPress.asp" -->
<%
Set Upload = Server.CreateObject("Persits.Upload.1")
' Upload files to temporary directory on Web server
Upload.Save "m:\web\users\v008u24guf\html\Classified_adv\"
sub AddRecord(RS, param1, param2)
RS.addnew
RS("Category") = param1
'if len(param2) > 8000 then
'RS("AdText") = Left(param2, 8000)
'else
RS("AdText") = param2
'end if
RS.update
end sub
%>
<%
set Conn = server.createobject("ADODB.Connection")
conn.open MM_JCPress_STRING
'Clear the table out for new data
sqlDelete = "DELETE FROM SimpleClass WHERE ID > 0"
Conn.execute sqlDelete
'Open a recordset for inserting data into the table
sql = "SELECT * FROM SimpleClass"
set RS = Server.Createobject("ADODB.Recordset")
RS.open sql, Conn, 1, 3
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Dim objTextStream
For each File in Upload.Files
tempFilename = File.ExtractFileName
Next
const fsoForReading = 1
sFilename = "m:\web\users\v008u24guf\html\classified_adv\" & tempFilename
If objFSO.FileExists(sFilename) then
response.write "File Found: " & tempFilename & " "
'The file exists, so open it and output its contents
Set objTextStream = objFSO.OpenTextFile(sFilename, fsoForReading)
sStream = objTextStream.ReadAll
objTextStream.Close
Set objTextStream = Nothing
Else
'The file did not exist
Response.Write strFileName & " was not found."
End If
'Clean up
Set objFSO = Nothing
arrStream = split(sStream, chr(25))
for i = 0 to UBound(arrStream) - 1
call AddRecord(RS, arrStream(i), arrStream(i+1))
i = i + 1
next
response.write "Successfully overwritten the Classified Database with " & RS.recordcount & " new records<br><br>"
RS.Close
set RS = nothing
Conn.close
set Conn = nothing
%>
The only PHP code I have written so far is:
<?php require_once('../../Connections/JCPress.php'); ?>
<?php
$file = @fopen("class_text.txt", "r") or die("Couldn't open file");
$data = fread($file);
while(!feof($file))
{
$data .= fgets($file);
}
//fclose($file);
$arrStream = split(chr(25), $data);
for($i = 0; $i <= (count($arrStream) - 1); $i++)
{
print_r ($arrStream);
}
?>
Any help would be much appreciated!