Jump to content

Using $filename - CREATE new file with 'Title'+ 'appFirstNames' to fwrite tO0o??


Recommended Posts

i am trying to save info entered into a form to a csv file. The problem im having is i need the csv file to be CREATED using the supplied form info. Heres what im trying.

 

function sendApplicationDataEmail() {

$filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv';
$somecontent = _cleanUp($_REQUEST['Title']).","._cleanUp($_REQUEST['FirstNames']).","._cleanUp($_REQUEST['Surname']).","._cleanUp($_REQUEST['DOB'])." ".$lineEnd;

// make sure the file exists and is writable first.
if (is_writable($filename)) {

    // open $filename in append mode.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if  (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Thank you for your application";

    fclose($handle);

} else {
    echo "The file $filename is not writable";

 

 

Its the filename im getting stuck on am i way off or what?

the error im getting is that the file is not writteable...

 

thanks in advance  ???

Hey jonsjava im still having problems. I tried chmodding the folder to 666 but the server doesnt like that and ALL the included files and folders are not accessible via http. im wondering if i could output to a different folder thats chmodded to 666 so the current files/folders are not affected. this would be preferrable because it would collect them all in one place.What change would i need for this?

 

also will this code even work

$filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv';

 

??????

appreciate the help on this :)

<?php
function sendApplicationDataEmail() {
$folder = "your_folder/"; /* <-- Change this! */
$filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv';
$somecontent = _cleanUp($_REQUEST['Title']).","._cleanUp($_REQUEST['FirstNames']).","._cleanUp($_REQUEST['Surname']).","._cleanUp($_REQUEST['DOB'])." ".$lineEnd;

// make sure the file exists and is writable first.
if (is_writable($filename)) {

    // open $filename in append mode.
    if (!$handle = fopen($filename, 'w+')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if  (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Thank you for your application";

    fclose($handle);

} else {
    echo "The file $filename is not writable";

Hey jons i really appreciate that know it was a newbie question but im a real nooob at php. I kind of dabble in a few codes... (zencart etc) just when i need to lol.  anyway the final code ive just tested and am happy with (using the additional file line you suggested) is.

<?php
function sendApplicationDataEmail() {
$folder = "csv_folder/"; /* <-- Change this! */
$filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv';
$somecontent = _cleanUp($_REQUEST['Title']).","._cleanUp($_REQUEST['FirstNames']).","._cleanUp($_REQUEST['Surname']).","._cleanUp($_REQUEST['DOB'])." ".$lineEnd;
    // open $filename in write mode.
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if  (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Thank you for your application";

    fclose($handle);

} 

 

think ive just about removed any redundant code. let me know if you can offer any advice. I appreciate your time jonsjava its helped me alot. Ive been sat here for a few hours now trying to figure this out and have just done it with your help in litterally 10mins lol.  cheers  ;D

It looks like fairly clean code. I can't say that that's the way I'd do it, but every programmer does things their own way. That's what makes PHP so different. 1 million ways to do 1 thing.  Just remember, the way you're opening the files, it opens the file and overwrites anything in the file. If the file doesn't exist, attempts to create the file.  If you want to append to an existing file, change "w+" to "a".

 

Yes im really starting to like php. im also taking an interest in ruby on rails. But im deffo working my way through a few lynda php disks and php on w3schools soon.

 

I think im still having problems getting it to place the csv file in the folder though ill let you know if i get it sorted or if i need to pester you some more. :) thanks again.

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.