Hi,

Is it possible to user to move_uploaded_file
to upload files to a server and insert the file name into MySQL to act as a image source.

I have file upload working but need now to
get this to populate my MySQL DB with these files names so I can automatically create image links.

<?php

$db = mysql_connect("localhost", "user", "passwd")
or die ("cant connect to the DB");

// define the base image dir
$base_img_dir = "/var/www/html/test/images/";

// generate unique id for use in filename
$uniq = uniqid("");

// new file name
$filename = $base_img_dir.$uniq;

//I know the following is wrong but you get the //idea.
//$filename = $image_MySQL_database_id.jpg;

// move uploaded file to destination
move_uploaded_file($HTTP_POST_FILES["file"]["tmp_name"], $filename);

?>

Any ideas folks?

G

    Just add something like this to your code:

    $query = mysql_query("INSERT INTO your_table (image_field) 
       VALUES('" . $HTTP_POST_FILES["file"]["name"] . "')") or die(mysql_error());

    Cgraz

      This is what I posted earlier - any ideas?

      The file uploads perfectly, but the I can't get the image filename to populate the MySQL database - nor any of the other entries! It looks right to me can anyone tell me what's wrong???

      Thanks in advance for your help!

      <?php
      	$file_dir = "/users/rupertbj/domains/rupstar.co.uk/html/images/weblog"; 
      	$file_url = "http://www.rupstar.co.uk/images/weblog"; 
      	if (isset( $fupload ) )
      		{
      		print "path: $fupload<br>\n";
      		print "name: $fupload_name<br>\n";
      		print "size: $fupload_size bytes<br>\n";
      		print "type: $fupload_type<p>\n\n";
      		if ( $fupload_type == "image/pjpeg" )
      			{
      			$db = "weblog";
      			$link = mysql_connect( "localhost:/users/rupertbj/domains/rupstar.co.uk/sql/mysql.sock", "root" );
      			if ( !$link ) die( "Couldn't connect to MySQL".mysql_error() );
      			mysql_select_db( $db, $link ) or die( "Couldn't open $db: ".mysql_error() );
      			$sql = "INSERT INTO weblogTable VALUES ( '', now(), '$description', '$content', '$fupload_name' )";
      			mysql_query( $sql, $link );
      			mysql_close( $link );
      			copy ( $fupload, "$file_dir/$fupload_name" ) or die ("Couldn't copy");			
      			}
      		}
      ?>
      
        10 days later

        Hi cgraz

        Thanks for you help I am now getting somewhere
        I can upload my file and also get the name of the file inserted into the MySQL DB this works a treat
        🙂
        The only thing is that when I upload the file it is getting a different random name.
        I have tried disabeling the uniqid but with no success.

        Do you know how I can keep the uploaded filename the same as the original file name?.

        Many thanks
        G

        <?php

        $db = mysql_connect("localhost", "username", "passwd")
        or die ("cant connect to the DB");
        print ("connected to database successfully");

        mysql_select_db("test",$db);

        // define the base image dir
        $base_img_dir = "/var/www/html/test/images/";

        // generate unique id for use in filename
        $uniq = uniqid("");

        // new file name
        $filename = $base_img_dir.$uniq;
        //$filename = $_FILES["userfile"];

        // move uploaded file to destination
        move_uploaded_file($HTTP_POST_FILES["file"]["tmp_name"], $filename);

        $query = mysql_query("INSERT INTO employees (image)
        VALUES('" . $HTTP_POST_FILES["file"]["name"] . "')") or die(mysql_error());

        ?>

          Look at my script above and use that - it does in fact work, I just had a wee problem with my database before!!!

            Yeah. Change

            // generate unique id for use in filename 
            $uniq = uniqid(""); 
            
            // new file name 
            $filename = $base_img_dir.$uniq; 
            //$filename = $_FILES["userfile"]; 

            to

            // new file name 
            
            $orig_name = $_FILES["userfile"]["name"]; 
            $filename = $base_img_dir.$orig_name; 
            
            // just as a test I would echo out the values
            echo "File path and name: " . $base_img_dir.$orig_name;

            Cgraz

              Hi cgraz

              Thanks for your help,I have inserted this code and I am now getting the following error

              connected to database successfullyFile path and name: /var/www/html/test/images/
              Warning: Unable to create '/var/www/html/test/images/': Is a directory in /var/www/html/test1/upload.php on line 24

              Warning: Unable to move '/tmp/phpBnj11d' to '/var/www/html/test/images/' in /var/www/html/test1/upload.php on line 24

              =============
              <?php

              $db = mysql_connect("localhost", "user", "passwd")
              or die ("cant connect to the DB");
              print ("connected to database successfully");

              mysql_select_db("test",$db);

              // define the base image dir
              $base_img_dir = "/var/www/html/test/images/";

              // file name

              $orig_name = $_FILES["userfile"]["name"];

              $filename = $base_img_dir.$orig_name;

              //echo file path

              echo "File path and name: " . $base_img_dir.$orig_name;

              // move uploaded file to destination
              move_uploaded_file($HTTP_POST_FILES["file"]["tmp_name"], $filename);

              ?>

                make sure the directory../images is in fact in existence.

                also, make sure that you are referring to the correct directory i.e. ../test/images rather than ../test1/images

                if you have a look at my working code above you should be able to understand it - it's very simple:

                $fupload is the path of the file to be uploaded. other information about that file is taken by adding an extension preceded by _

                i.e. $fupload_name is the name of the file to be uploaded - the bit you want to keep the same!

                I hope this clarifys things for you!

                  make sure you have write privilages (chmod folder to 777)

                  Cgraz

                    Hi,

                    The permissions on the folder as set to 777

                    The thing is the code works fine as long as I use unqid and fails when I use filename

                    Any ideas??

                    G

                      $orig_name = $_FILES["userfile"]["name"]; 
                      
                      $filename = $base_img_dir.$orig_name; 
                      
                      //echo file path 
                      
                      echo "File path and name: " . $base_img_dir.$orig_name; 
                      
                      // move uploaded file to destination 
                      move_uploaded_file($HTTP_POST_FILES["file"]["tmp_name"], $filename); 

                      Why are you using $FILES uptop, then $HTTP_POST_FILES in the same script? You should either be using one or the other. What i'd do is echo $filename to see what value it has. If it has a value only for the directory and not the actual filename, then you need to change $FILES to $HTTP_POST_FILES (or if you know what version of PHP you're running, then you'll know which of the two to use).

                      Echoing out variables may help to see what's going on.

                      Cgraz

                        5 days later

                        Hi cgraz,

                        Thanks for you advice this is the output I get with echo.
                        i.e. no file name

                        (I am running PHP 4.2.2 on Redhat8)


                        connected to database successfully

                        The base image dir is : /var/www/html/test1/images/

                        filename :

                        The File path and name are: /var/www/html/test1/images/
                        Warning: Unable to create '/var/www/html/test1/images/': Is a directory in /var/www/html/test1/upload10.php on line 39

                        Warning: Unable to move '/tmp/phpJy0pO2' to '/var/www/html/test1/images/' in /var/www/html/test1/upload10.php on line 39

                        Thanks
                        G

                          is the name of your file actually userfile in your form? Can you post the code to your form?

                          Cgraz

                            Thanks a mill. cgraz

                            You have shown me the error of my ways !!!!!!!

                            It is now working perfectly thanks to you.

                            Here is the code.

                            upload.html

                            <html>
                            <form enctype="multipart/form-data" method="post" action="upload10.php">
                            <b>Upload image</b><br>
                            Select image: <input name="userfile" type="file"><br>
                            Title: <input name="title" type="text"><br>
                            Description: <textarea name="descr"></textarea><br>
                            Alt-text: <input name="alt" type="text"><br>
                            <input type="submit" value="Upload">
                            </form>
                            </html>

                            upload10.php
                            <?php

                            $db = mysql_connect("localhost", "user", "passwd")
                            or die ("cant connect to the DB");
                            print ("connected to database successfully");

                            mysql_select_db("test",$db);

                            ?><html><br><br><?php

                            // define the base image dir
                            $base_img_dir = "/var/www/html/test1/images/";

                            echo "The base image dir is : " . $base_img_dir;

                            ?><html><br><br><?php

                            $orig_name = $HTTP_POST_FILES["userfile"]["name"];

                            ?><html><br><br><?php

                            $orig_name = $_FILES["userfile"]["name"];
                            echo "filename : " . $orig_name;

                            ?><html><br><br><?php

                            $filename = $base_img_dir.$orig_name;

                            //echo file path
                            echo "The File path and name are: " . $base_img_dir.$orig_name;

                            // move uploaded file to destination
                            move_uploaded_file($HTTP_POST_FILES["userfile"]["tmp_name"], $filename);

                            $query = mysql_query("INSERT INTO employees (image)
                            VALUES('" . $HTTP_POST_FILES["userfile"]["name"] . "')") or die(mysql_error());

                            //

                            ?>

                              Write a Reply...