Hi guys
Could anyone ponit me in the way of a guide to upload .doc and .csv files to the mysql database
| SitePoint Sponsor |




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





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.
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.




sounds like a good idea
Is there a guide or tutorial for it?
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:
Does this give any idea???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>




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?
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.
Or insert the id in your database table separately like this:PHP Code:$filename = $id . "_" . basename($_FILES['file']['name']);
Umm, does this mean??PHP Code:mysql_query("insert into tablename set id='$id',filename='$filename'") or die(mysql_error());




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?




You could do something like:
You can then insert the new filename into the db along with the other details for referencing ...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




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!";
}
?>




Sorry, I should have been more specific, my code was a kittle incorrect.
That should give you "Chris CV_randommnumberhere.wps"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";
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.




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
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:
I'll do: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']);
Then I'll just use this name in the database so the rest of the script would be: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']);
?>
Good luck!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!";
}
?>
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.
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.PHP Code:$filename = $id . "_" . basename($_FILES['file']['name']);




I used that script but the file isnt going into the folder that has been created





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.
Bookmarks