SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to upload .DOC,.CSV

    Hi guys

    Could anyone ponit me in the way of a guide to upload .doc and .csv files to the mysql database

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a particular reason you need the files to be in the database? I did a site that stored large files in MySQL once, and it slowed the database server to a grinding halt. You may find it preferable to store the files in the filesystem with each file's MD5 hash as the filename, then put the filenames and any other desired information about the files into the database. Not only would this be more efficient, it would save a lot of tinkering with the database configuration files.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello dnbidder2005,

    Do you mean that importing date from .csv and .doc files? If not World Wide Weird's idea is the best one instead of storing the files. If you mean to importing the data from CSV files then there are some functions like fgetcsv() in PHP see the manual. And uumm about the .doc you have to read the data with file handing functions.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sounds like a good idea

    Is there a guide or tutorial for it?

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont think that there are such particular tutorials for it but you need to integration some things to one for your goal. Like upload file with some move_uploaded_file() or copy() for uploading the browsed files and you can get lots of file information with $_FILES array i.e. $_FILES['browsefile']['tmp_name'], $_FILES['browsefile']['type'], $_FILES['browsefile']['size'] etc.

    See the simple example here:
    PHP Code:
    <?php
    if($_POST['Submit']){
        if(
    is_uploaded_file($_FILES['file']['tmp_name'])){
            
    $filename basename($_FILES['file']['name']);
            
    $target_folder "./uploads/";
            
    $target $target_folder $filename;
            if(
    move_uploaded_file($_FILES['file']['tmp_name'], $target)){
                
    //now store file information in your table
                
    mysql_query("insert into tablename set filename='$filename'") or die(mysql_error());
            }
        }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>

    <body>
    <form name="form1" enctype="multipart/form-data" method="post" action="">
      <input type="file" name="file">
      <input type="submit" name="Submit" value="Submit">
    </form>
    </body>
    </html>
    Does this give any idea???
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have created a form and now the file is uploaded to a folder on my server.
    The problem is how do I assign a id to the file so that I know who upload the file?

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could not be clear about your short description but from the what i could understand; you can insert the ID to some extra field in the table while inserting the file name. Also you can concatenate the ID with the filename too.
    PHP Code:
    $filename $id "_" basename($_FILES['file']['name']); 
    Or insert the id in your database table separately like this:
    PHP Code:
    mysql_query("insert into tablename set id='$id',filename='$filename'") or die(mysql_error()); 
    Umm, does this mean??
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep that is helpful

    Problem I might had is as every user will upload CV. there could be a lot of files with same name e.g cv.csv

    How can i get around this?

  9. #9
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do something like:

    PHP Code:
    // get the name of the uploaded file
    $filename basename($_FILES['file']['name']);
    // select a random number from 1 - 999
    $id rand(19999);
    //concatenate the two vars
    $new_filename $filename ."_" .$id
    You can then insert the new filename into the db along with the other details for referencing ...

    D

  10. #10
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried that

    its entering this into the database

    _8967

    it should be

    chris CV.wps


    heres my code


    PHP Code:
    <?





    // Where the file is going to be placed 
    $target_path "uploads/";

    /* Add the original filename to our target path.  
    Result is "cv/filename.extension" */
    $target_path $target_path basename$_FILES['uploadedfile']['name']); 
    $_FILES['uploadedfile']['tmp_name'];  
    $target_path "cv/";
    $target_path $target_path basename$_FILES['uploadedfile']['name']); 





    $con mysql_connect("localhost","notexper_two","three"); if (!$con){ die('Could not connect: ' mysql_error()); }
    mysql_select_db("notexper_one"$con);


    // get the name of the uploaded file
    $filename basename($_FILES['uploadfile']['name']);
    // select a random number from 1 - 999
    $id rand(19999);
    //concatenate the two vars
    $new_filename $filename ."_" .$id



    $sql "INSERT INTO CV
            (user_id,filename,uploadedcv)
      VALUES('"
    .$_SESSION['seeker']."','$new_filename','yes')";

    $query mysql_query($sql,$con) or die('Error: ' mysql_error());
    mysql_close($con);


    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))










     {
        echo 
    "The file <B>".  basename$_FILES['uploadedfile']['name']). 
        
    "</b> has been uploaded";
    } else{
        echo 
    "There was an error uploading the file, please try again!";
    }


    ?>

  11. #11
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I should have been more specific, my code was a kittle incorrect.

    PHP Code:
    // get the name of the uploaded file
    $filename basename($_FILES['uploadfile']['name']);
    // select a random number from 1 - 999
    $id rand(19999);
    //concatenate the two vars, and add the extension
    $new_filename $filename ."_" .$id.".wps"
    That should give you "Chris CV_randommnumberhere.wps"

    Additionally, what I normally do for this sort of thing is the following:

    PHP Code:
    $filename basename($_FILES['uploadfile']['name']);
    // get rid of spaces and convert all letters to lower case
    $filename strtolower(str_replace(" ","",$filename));
    //put together the new name
    $new_filename $filename ."_" .$id.".wps"

  12. #12
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're going to store only one file per user you might store the filename in the database but have the real file renamed to $id.doc or $id.csv.

    Another way is to just create a folder for each user in the database. For example users/$id/ and upload their files there. I generally do this for scalability. So if tomorrow the user wants to add pictures or other media i would just add subfolders like: users/$id/pics/, users/$id/music/, etc.

  13. #13
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how can i rename the file that is added to the folder?

    also how could i create a directory for each user? would it be auto generated when they add there cv

  14. #14
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the move_uploaded_file function.

    The parameters are:
    1) the source file (the one uploaded by the user).
    2) the destination path, (where you put it, including name).

    In this section:

    PHP Code:
    <?php

    // Where the file is going to be placed 
    $target_path "uploads/";

    /* 
        Add the original filename to our target path.  
        Result is "cv/filename.extension" 
    */
    $target_path $target_path basename$_FILES['uploadedfile']['name']); 
    $_FILES['uploadedfile']['tmp_name'];  

    $target_path "cv/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']);
    I'll do:

    PHP Code:
    // Where the file is going to be placed 
    // I added the user Id
    $target_path "uploads/".$_SESSION['seeker']."/";

    // Check if the dir exists for this user and if dont i create it. 
    if (!is_dir($target_path) ) {
      
    mkdir$target_path );
    }

    /* 
        I keep the same file name as now i have a special folder for this user.
        Result is "uploads/$_SESSION['seeker']/cv/filename.extension" 
    */

    $target_path .= "cv/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']); 

    ?> 
    Then I'll just use this name in the database so the rest of the script would be:

    PHP Code:

    <?php

    $con 
    mysql_connect("localhost","notexper_two","three"); if (!$con){       die('Could not connect: ' mysql_error()); }
    mysql_select_db("notexper_one"$con);

    // get the name of the uploaded file

    $filename basename($_FILES['uploadfile']['name']);

    $sql "INSERT INTO CV

            (user_id,filename,uploadedcv)

      VALUES('"
    .$_SESSION['seeker']."','$filename','yes')";


    $query mysql_query($sql,$con) or die('Error: ' mysql_error());

    mysql_close($con);


    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
    {

        echo 
    "The file <B>".  basename$_FILES['uploadedfile']['name']). 

        
    "</b> has been uploaded";

    } else{

        echo 
    "There was an error uploading the file, please try again!";

    }

    ?>
    Good luck!

  15. #15
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since one user should have only one CV in the database then you dont have do anything more as i know so far. Just concatenate the user's id with the name and save it as above in my second post.
    PHP Code:
    $filename $id "_" basename($_FILES['file']['name']); 
    Here is assuming the user's id is stored in $id variable. Then you won't have any problem if anyone uploaded the file with same name. I also assume that the user's ids are unique.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  16. #16
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used that script but the file isnt going into the folder that has been created

  17. #17
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dnbidder2005 View Post
    I have created a form and now the file is uploaded to a folder on my server.
    The problem is how do I assign a id to the file so that I know who upload the file?
    What I do in my forum's image dump is MD5 the file and use that as the filename. Not only does it provide a unique filename, it helps ensure every file is unique. Thus, if someone else subsequently uploads an identical file, the script informs them that the file already exists.

    Once you've done that, you simply put the MD5 filename in the database along with the original filename or the user-defined virtual filename.

  18. #18
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dnbidder2005 View Post
    I used that script but the file isnt going into the folder that has been created
    Please tell me what's the value of $target_path before move_uploaded_file()

    PHP Code:

    echo  $target_path;

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))

    {



        echo 
    "The file <B>".  basename$_FILES['uploadedfile']['name']). 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •