SitePoint Sponsor |
|
User Tag List
Results 1 to 18 of 18
Thread: how to upload .DOC,.CSV
-
Jun 26, 2007, 12:33 #1
- 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
-
Jun 26, 2007, 23:09 #2
- 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.
-
Jun 27, 2007, 01:16 #3
- 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
-
Jun 27, 2007, 01:58 #4
- 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?
-
Jun 27, 2007, 02:41 #5
- 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>Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Jun 27, 2007, 03:23 #6
- 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?
-
Jun 27, 2007, 04:17 #7
- 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']);
PHP Code:mysql_query("insert into tablename set id='$id',filename='$filename'") or die(mysql_error());
Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Jun 27, 2007, 05:31 #8
- 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?
-
Jun 27, 2007, 05:50 #9
- 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(1, 9999);
//concatenate the two vars
$new_filename = $filename ."_" .$id;
D
-
Jun 27, 2007, 06:24 #10
- 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(1, 9999);
//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!";
}
?>
-
Jun 27, 2007, 06:36 #11
- 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(1, 9999);
//concatenate the two vars, and add the extension
$new_filename = $filename ."_" .$id.".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";
-
Jun 27, 2007, 07:04 #12
- 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.
-
Jun 27, 2007, 07:30 #13
- 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
-
Jun 27, 2007, 07:46 #14
- 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']);
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']);
?>
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!";
}
?>
-
Jun 27, 2007, 08:23 #15
- 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']);
Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Jun 27, 2007, 11:33 #16
- 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
-
Jun 27, 2007, 13:14 #17
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Jun 28, 2007, 05:01 #18
- Join Date
- Apr 2006
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks