automation needed in php.. is this possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
burnmic
Forum Newbie
Posts: 4
Joined: Wed Feb 11, 2009 1:15 pm

automation needed in php.. is this possible?

Post by burnmic »

Hi,

I'm relatively new to PHP, and have set myself a challenge to get something done.
I guess this isn't so much a question, but more me asking for advice really.. i.e., can it be done?!

I'm looking to do the following:

Stage 1:

1. Create a form that allows me to upload a .csv file to a directory on my server [done]
2. Create a script that reads the csv file during the upload, takes out a value from a specific line from within the .csv file (line 3 in this example) and use this variable as the value to rename the .csv file.

The form is complete, and allows me to upload a file into a directory.
I can read the name of the file using the following code:

Code: Select all

$filename = basename( $_FILES['file']['name']);
..but can't for the life of me workout how to read a csv file, locate a specific string, and set this string as a variable to rename the file. Any ideas?

Stage 2:
Once the file is saved with it's new unique name, I need to read the data & style it (I already have a stylesheet set). I can read and render the data from the file using this code:

Code: Select all

<?php
$row = 1;
$handle = fopen("report/mycsvfile.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);
?>
Is there a way to add styling to this using div id's / classes without needing to enter the data into the csv file first, or copy the rendered html into another file? (maybe I could save the html output and read that into another file.. not sure).

Stage 3:

Finally, once I've managed to read the data, rename the file & style the page, I would like to be able to save the page automatically into a PDF format. I've looked at a lot of pdf apps and a php pdf library also, but really need to be able to save a rendered page (with website branding and styled csv data) into the pdf. Does anybody know whether this is possible?

Not bad for my first post... a reasonably tall order i'm sure!

I guess once I know it's possible, it's just a case of research research research and a lot of hard work :)

hope to hear from you soon,
mic
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: automation needed in php.. is this possible?

Post by requinix »

Stage 1:
$_FILES["file"]["tmp_name"] is the location of the uploaded file. $_FILES["file"]["name"] is what it was called on the user's computer.
After you've figured out the name you want to use, move_uploaded_file can move the file wherever you want.

Stage 2:
CSV files contain data and nothing more. No style information, no formatting. If you want something fancier then you need to add the formatting yourself.
I can't help you much now. Yes, it is possible to do what you need, but you haven't given enough information for a definite answer. How is it going to be styled? How are you printing the data? Do you realize that a <table> is probably the best way to go?

Stage 3:
It's impractical to take a screenshot of the browser (or similar). PHP has a PDF library but it may be easier if you found third-party code to do it for you.
burnmic
Forum Newbie
Posts: 4
Joined: Wed Feb 11, 2009 1:15 pm

Re: automation needed in php.. is this possible?

Post by burnmic »

Hi tasairis - thanks for your response, it's appreciated :)

I think I'll take your advice on Stage 2, and use a table which in-turn will be styled by existing CSS - thanks!

I've looked into the PDF library link you sent over (thanks for that), and it looks a like the basic functions (stuff I'm probably cable of) aren't advanced enough.. and the interesting parts would likely be beyond my php skillset at the moment!
I'm told it may be possible to add the csv data dynamically over the top of a pre-designed PDF template. I haven't checked this yet but sounds like it could actually be a better method of doing this.

Ok, so Stage 1 is still the main issue. uploading, saving & moving the file seem ok, but generating a filename for the .csv file based on static information inside of the same .csv file on a specific line is a mind boggler 8O

I thought about reading the whole csv file into a variable, and then using trim or str_replace or similar to remove everything other than the data on line 3 after the comma. Still not sure this is possible!

Thanks again for your feedback, it's a point in the right direction :)
t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

Re: automation needed in php.. is this possible?

Post by t2birkey »

Just so happens I recently did something similar to this. You will need to extract the data from the CSV file if there is data in there that you wish to use as the name of the file. It is really up to you for how to extract this data. Looks like the code you posted already extracts all the data from the CSV file. From your code you could say

Code: Select all

if($row==3 && $c==2) echo 'this is the third row and second column. I can use this for the info for the name of the file by calling $data[$c] which is: '.$data[$c];
burnmic wrote:2. Create a script that reads the csv file during the upload, takes out a value from a specific line from within the .csv file (line 3 in this example) and use this variable as the value to rename the .csv file.
I do not see where this line 3 is that contains csv data.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: automation needed in php.. is this possible?

Post by requinix »

Oh yeah, FDF. It's still a relatively new idea to me so I don't think about it much. PHP can do it too.

For reading the file, keep it simple. You can read the file with fgetcsv: that gets one line at a time. If you don't care about the first or second lines then don't do anything with them. Then read the third line, figure out the filename...

Code: Select all

$h = fopen($_FILES["file"]["tmp_name"], "rb");
fgets($h); // ignore the first line
fgets($h); // ignore the second line
$data = fgetcsv($h, 1024); // get the third line
fclose($h);
 
// $filename = ???
move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
You should be checking a couple things though.
  • If $_FILES[file][error] isn't 0 then there was a problem (if it's 4 then there wasn't a file to begin with).
  • If fopen doesn't work then $h will be false: shouldn't continue if that's the case.
  • move_uploaded_file is the one most likely to fail - permissions issues. If that returns false the it didn't move the file.
Post Reply