+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: php Upload Function help please

  1. #1
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    php Upload Function help please

    My Site is a customer relationship management system, fully written from scratch.

    Within it is a function to import existing contact details (.csv file) into the DB. However, this relies on the file_get_contents from a URL, which means that a user has to have their .csv uploaded to a server.... which many won't have a clue how to do.

    What I would prefer is for them to just upload a file from their system and then the code loads the data from that temporary file.

    I know this is probably possible but don't have clue where to start.

    Any assistance is appreciated.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: php Upload Function help please

    This is where to start: "Handling file uploads".

  3. #3
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: php Upload Function help please

    Quote Originally Posted by misson View Post
    This is where to start: "Handling file uploads".
    OK - thanks for the starting point. This page didn't exactly help, but it did lead me to a similar helpful one...

    I now have a 3 page process.

    1) upload file to tmp dir

    PHP Code:
    <form action="datapreview.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
        <
    label>
        <
    input type="hidden" name="MAX_FILE_SIZE" value="30000" />
        <
    input name="userfile" type="file" size="80" />
        </
    label>
        <
    p>
          <
    label>
          <
    input type="submit" name="button" id="button" value="Upload" />
          </
    label>
        </
    p>
      </
    form
    2) Check data and preview

    PHP Code:
    <h2>File Information</h2>
    <br />
    File Name: <?php echo $_FILES['userfile']['name'];?>
      <br />
    File Type: <?php echo $_FILES['userfile']['type'];?>
    <br/>
    File Size (bytes): <?php echo $_FILES['userfile']['size'];?>
    <br/>
    Temp Uploaded File Name: <?php echo $_FILES['userfile']['tmp_name'];?>
    <br/>
    Error Code: <?php echo $_FILES['userfile']['error'];?>
    <br/>
    <br/>
    <h2>Preview Data to be imported</h2>
    <?php
    $filecontent 
    file_get_contents($_FILES['userfile']['tmp_name']);
    echo 
    "<br/>";
    print_r($filecontent);
    ?>

    3) import into DB

    Don't think I'll have any problems here.

    My questions are..

    a) how do I verify that the file is .csv format?
    b) on page 2, how do I get the data to preview in tabular format? Will this have to be on a loop and create rows based on exploding by /n?
    c) what happens to the temporary file? Is it automatically deleted on browser closure?

  4. #4
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: php Upload Function help please

    a) Look for the features that make it a .csv file and just verify. Firstly, check file extension. Secondly, is the format correct. Finally, use strip_tags() and make sure the file does not pose a security risk.
    b) Just loop through, replacing the blanks
    c) The server deletes it after the PHP execution completes

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: php Upload Function help please

    Quote Originally Posted by freecrm View Post
    OK - thanks for the starting point. This page didn't exactly help, but it did lead me to a similar helpful one...
    Which is the point of the page. As the entry point for the PHP file upload documentation, it links to specific subject pages.

    Quote Originally Posted by freecrm View Post
    PHP Code:
    <form action="datapreview.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
        <
    label>
        <
    input type="hidden" name="MAX_FILE_SIZE" value="30000" />
        <
    input name="userfile" type="file" size="80" />
        </
    label
    The <label> element is supposed to contain a label for a form control, not a form control itself. Why are you using them as <input> containers?
    Example:
    HTML Code:
    <form action="datapreview.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
        <label for="userfile">File:</label>
        <input name="userfile" type="file" size="80" />
    ...
    I also don't recommend using the "MAX_FILE_SIZE" form variable even though it shows up in the PHP documentation examples. Form variables shouldn't contain trusted data and the only way of validating that particular value is to store the same information server side.

    Quote Originally Posted by freecrm View Post
    c) what happens to the temporary file? Is it automatically deleted on browser closure?
    Note that c) is answered immediately above the last example on the POST method uploads page.
    Last edited by misson; 04-12-2009 at 05:44 PM.

  6. #6
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: php Upload Function help please

    Thanks for the info - I'm working on this part by part.

    First - getting the file extention. I've tried using pathinfo, but for some reason, the extention part of the array is not supported by the x10 version. Any ideas?

    i.e. $pathinfo = pathinfo($_FILES['userfile']['tmp_name'], PATHINFO_EXTENSION);

    I've also tried to split it off using $ext = $pathinfo['extension']; but got nowhere.

    In fact, if you print_r the array, there is nothing there containing the extension information!

    I have used strip_tags as suggested as this could be a problem (although if it remains in the /tmp/ directory, surely if this is not public access, it wouldn't cause a problem.

    I've also pretty much done the loop.

    Thanks for the help so far.

  7. #7
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: php Upload Function help please

    I have used strip_tags as suggested as this could be a problem (although if it remains in the /tmp/ directory, surely if this is not public access, it wouldn't cause a problem.
    Very true, but I just do it as a safety precaution, you shouldn't really have to do it.

    To get the extension, I generally just split the string by the '.' and look at the last item in the array.

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: php Upload Function help please

    Quote Originally Posted by freecrm View Post
    First - getting the file extention. I've tried using pathinfo, but for some reason, the extention part of the array is not supported by the x10 version. Any ideas?

    i.e. $pathinfo = pathinfo($_FILES['userfile']['tmp_name'], PATHINFO_EXTENSION);
    The temporary name is pseudo-random and not based on the original file name. Use $_FILES['userfile']['name'] to get the original file name, including extension.

  9. #9
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: php Upload Function help please

    That would explain a lot - thanks.

    I have now completed the preview page but am struggling to get my head around how to validate the format.

    I think I can also use a character count to establish a size restriction.

    I'll get there I'm sure.

  10. #10
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: php Upload Function help please

    Quote Originally Posted by freecrm View Post
    I have now completed the preview page but am struggling to get my head around how to validate the format.
    The standard approach would be to parse the file and throw an exception if the parse fails. For CSV files, use fgetcsv() to parse the next line & then check that the number of fields is correct. If fgetcsv() fails, make sure you use feof() to check whether you've reached EOF or had a parse error.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Upload problem with my php form... Please help!
    By uplinked in forum Free Hosting
    Replies: 6
    Last Post: 12-12-2008, 09:13 AM
  2. New Stoli Gallery 2 and Set_Time_Limit php function
    By zgosto in forum Free Hosting
    Replies: 1
    Last Post: 11-16-2008, 02:49 AM
  3. mail() Function not working After PHP Upgrade
    By ElijahT in forum Free Hosting
    Replies: 2
    Last Post: 11-10-2007, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers