Jump to content

uploading multiple files to mysql


sridhar golyandla

Recommended Posts

Hi,

 

 

I'm also a newbie but hopefully my experiences can help you.

 

I also wanted to upload multiple images to a mysql table but after many hours of reading forumns and bloggs I realised that it would be better to store a string with the path to my files on my server rather than store it in the mysql db as it can be a performance hit when trying to retrieve the files. In my case loads of images.

 

do you have phpmyadmin? if not I would suggest you get it as its quiet a user friendly front end to a mysql db. Then you need to create your tables and then you can code your php.

 

In my example which I'm still busy with. I point to a particular folder on my server and get php to loop through each directory and insert a row for each file in each folder.

 

it might be worth checking out php.net and searching the functions list for scandir($dir) functions.

 

Could you example to us your current setup? ie os system php version, mysql and anything else that we could help you with.

 

 

 

 

Link to comment
Share on other sites

Thanx for quick reply

 

      Iam searching in the manual of the PHP..

currently am using php4 and mysql5 on winxp as OS.

 

i have two diff tables called tab1 and tab2 for time being...

it is working well with one file upload with unique id but i want to upload multiple files with unique tab1.id which is foreign key for tab2.

 

suppose mr.x has been uploaded with 3 files... a1, a2, a3...

 

tab2.id    tab1.id  name  type    size    path

2            1        a1.doc  mswrd  20k  aamdkakd86786767.doc

          (Unique    a2.doc  mswrd  20k  232143434mhjhj.doc

              id)        a3.doc  mswrd  20k  212csfds54545.doc

                       

now i have to display all the files which that particular person has uploaded?

 

Please help me out...

Link to comment
Share on other sites

sridhar,

 

Do you have phpmyadmin? If you have a xamp or wamp installation then you should have it already setup. There is an option to auto increment a column id which is what you looking for. Basically it will add the next unique available id when you do an insert into your table.

 

Could you post some of your code that you got to work with the single file upload?

 

I still suggest you use a string to point to your file on your windows file structure. Am I right in thinking that you are already doing this and not actually storing the uploaded files in your db as blobs?

Link to comment
Share on other sites

the uploaded file is stored in folder called CV. i am using xamp.

Here is the my code which is working well with one file with unique id

 

$uploaddir = "table/";

$fileName = $_FILES['tab']['name'];

$tmpName = $_FILES['tab']['tmp_name'];

$fileSize = $_FILES['tab']['size'];

$fileType = $_FILES['tab']['type'];

 

$filePath = $uploadDir . $fileName;

// get the file extension first

$ext = substr(strrchr($fileName, "."), 1);

 

// make the random file name

$randName = md5(rand() * time());

 

// and now we have the unique file name for the upload file

$filePath = $uploadDir . $randName . '.' . $ext;

 

$result = move_uploaded_file($tmpName, $filePath);

if (!$result) {

echo "Error uploading file";

exit;

}

 

if(!get_magic_quotes_gpc())

{

$fileName = addslashes($fileName);

$filePath = addslashes($filePath);

}

 

$qry1="insert into table2(table2id,table1id,name, size, type, path) values('".$table2id."','".$table1id."','$fileName', '$fileSize', '$fileType', '$filePath')" or die(mysql_error());

 

Please help me out.

Link to comment
Share on other sites

first off to get a unique id login into phpmyadmin and amend your table edit the id column you should see an option like "A_I" or it might say "Auto Increment" tick it and apply changes. now your table will auto increment your id.

 

As a test php

 

create a php file called list_files.php then paste the following

 

<?php
   $dir = "C:/CV"; //This is your directory you want to scan
   $files= scandir($dir); //This will scann the directory and store directories and files into an array
   //Time to loop through them
   foreach ($files as $fileobject){
       //this will identify any folders in your directory
       if (is_dir($dir . "/" . $fileobject) && ($fileobject != "." $fileobject != "..")){
          echo $dir . "/" . $fileobject . " is a directory";
       }else
         echo $dir . "/" . $fileobject . " is a file";
       }
   }

?>

 

If you run that itshould output any folder and files your have in your specified folders. If I was you would do the insert if the file is a file.

 

Hopefully this can be some sort of help to you.

 

 

Link to comment
Share on other sites

Hello....

 

 

    Plz help me out "HOW TO UPLOAD MULTIPLE FILES".

 

Eg : "a" person wants to upload more than 3 CVs.

 

Here i have two tables, one is user and another is cv

user table :

id    uname    pw 

1    a          a

 

cv table

id      uid      name      type      size      path

1        1        cv.doc    msword  1009    9889809898-090.doc

(unique id)    cv1.doc  msword  1009    adadasdasdas.doc

                  cv2.doc  msword  1009    wtwetwtrtrt.doc

 

the cv has to be inserted into mysql table and as well as dir which i given the dir.

 

now the uid is the id from the user table which is not increment in the cv table.

 

the uid is increment of another person who uploads the new cv.

Plz help me out.

Link to comment
Share on other sites

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.