Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

uploading files to folder and writing name to mySQL

Codes here !

Moderators: macek, egami, gesf

uploading files to folder and writing name to mySQL

Postby mightyhokie » Wed Jun 13, 2012 8:35 am

I have clients that need to upload images via a web browser into a folder and have the name of the image stored into a mysql db so I can retrieve them later.
I asked a similar question on this forum and got pointed into a good direction but I've tried several, several, several tutorials and codes to no end. The code and tutorials i discovered were at first to simply upload one image into a folder. That worked no problem. Then I tried several where I should have been able to upload multiple images at once but that didn't work for me (but did for the presenter and lots of over people). Still, even if it worked I don't have the knowledge yet at php to make it to where it writes the name of the file into a database field for later use.

So, if I'm going to bother you guys for help I might as well ask for the whole thing at once.

So far all I have gotten is a form that uploads to a folder on the directory.

01_mypage.php:
Code: Select all
<form action="01_upload.php" method="post" enctype="multipart/form-data">

   <input type="file" name="file" />
    <br />
    <br />
    <input type="submit" value="submit" />

</form>


01_upload.php:
Code: Select all
<?php
$name = $_FILES['file']['name'];
//$size = $_FILES['file']['size'];
//$type = $_FILES['file']['tmp_name'];   
$tmp_name = $_FILES['file']['tmp_name'];
   if(isset($name)){
      if(!empty($name)){
         
         $location = 'upload/';
         if(move_uploaded_file($tmp_name, $location.$name)){
            echo $name;
            }else{
               echo 'There was an error.';
               }
         
         }else{
            echo 'Please select a file';
            }
      }
?>


Like I said, this works but it only uploads one image (not multiple) and doesn't write the file name to a database.

Please help. This has literally been 20 hours of research and this is all I have.
mightyhokie
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Jun 01, 2012 2:51 pm

Re: uploading files to folder and writing name to mySQL

Postby ejsexton82 » Wed Jun 13, 2012 11:55 pm

To be able to upload multiple files, you must add the "multiple" HTML attribute to the <input> tag. The "name" HTML attribute should also be an array:

Code: Select all
<input type="file" name="file[]" multiple="multiple"/>


You can then iterate over the $_FILES['file'] variable to upload all of the files:

Code: Select all
foreach($_FILES['file'] as $index => $file) {
   $name = $file['name']; 
   $tmp_name = $file['tmp_name'];
   // Upload the file...
}


As far as writing the name to MySQL, that is not too difficult. The condensed version would look something like this:

Code: Select all
mysql_connect($hostname,$username,$password);
mysql_select_db($database);
mysql_query(sprintf('INSERT INTO `table` (`name`) VALUES ('%s');',
   mysql_real_escape_string($name)
   ));
mysql_close();


I hope that helps. Good luck!

Sources:
User avatar
ejsexton82
New php-forum User
New php-forum User
 
Posts: 86
Joined: Mon Jun 04, 2012 10:05 pm
Location: Ankara, Turkey

Re: uploading files to folder and writing name to mySQL

Postby mightyhokie » Fri Jun 15, 2012 7:37 am

Thanks for your help. I did make some progress then somehow took a step back.
I got this code working at work using the same db structure:

Code: Select all
<?php require_once('Connections/chp.php'); ?>
<?php

mysql_select_db('chp');


if(isset($_FILES['files'])){
      foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
         move_uploaded_file($tmp_name, "upload/{$_FILES['files']['name'][$key]}");
         $path = ($_FILES['files']['name'][$key]);
         //$sqlst = ("INSERT INTO img (files) VALUES (".$path.")");
         //print_r($sqlst."<br>");
         
         mysql_query(sprintf("INSERT INTO img (files) VALUES ('".$path."')"));
         }
      }
?>
<html>
<head>
</head>

<body>
<form action="04_form_add_sql.php" name="form" method="POST" enctype="multipart/form-data">

   <input type="file" name="files[]" multiple />
  <input type="submit" value="Upload" />
  <input type="hidden" name="MM_insert" value="form">
</form>

</body>
</html>


Then I took it home and tried to 'plug it in' to a freelance job. For some reason it is 'waiting for localhost' forever. All the other dynamic elements for this site work no problem. But this lags.

You fill in this form then hit 'submit' and it takes you to the adminPhotos.php page where you select the images you want then hit 'submit' and it is supposed to write the name of the image to the database.
Code: Select all
<?php
   include "_include/addImages.php";
?>



<!DOCTYPE>
<html>
<head>


<?php
   include "_include/phpCode.php";
?>


<?php
   include "_include/styles.php";
?>

<title>Dottie Dooley and V. Ann Rozier Realtors</title>
</head>

<body>
<div id="wrapper">
                       
                         <?php
                                include "_include/header.php";
                               ?>
                           
                              <div id="contentContainer">
                              <form action="adminList.php" name="form" method="POST" enctype="multipart/form-data">

                                      <input type="file" name="files[]" multiple />
                                      <input type="submit" value="Upload" />
                                <!--<input name="listingsID" type="hidden" value="listingsID">-->
                                      <input type="hidden" name="MM_insert" value="form">
                            </form>        </div>
                                                         
                             
                              <?php
                                include "_include/footer.php";
                               ?>
       
</div> <!--end wrapper-->
</body>
</html>


And here is the _include/addImages.php file:
Code: Select all
<?php require_once('Connections/dottieann.php'); ?>
<?php

$imgPath = "img/photos/listings/";

if(isset($_FILES['files'])){
      foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
         move_uploaded_file($tmp_name, "$imgPath{$_FILES['files']['name'][$key]}");
         $path = ($_FILES['files']['name'][$key]);
         $sqlst = ("INSERT INTO img (files) VALUES (".$path.")");
         print_r($sqlst."<br>");
         
         mysql_query(sprintf("INSERT INTO img (files) VALUES ('".$path."')"));
         }
      }
?>


and here is a screencap of the table in the mySQL db:
Untitled-1.jpg
Untitled-1.jpg (165.81 KiB) Viewed 202 times


any ideas why this is hanging up? or any ideas on how to make it work? haha
mightyhokie
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Jun 01, 2012 2:51 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.