Jump to content

file uploads with mysql


dropfaith

Recommended Posts

i have this upload script that handles the files perfectly

http://dropfaithproductions.com/test/add.php form is here all i need is how to make it insert to mysql with a set id so i can pull them back out per set with the other details in the form listed above im more then likely going to add all the data into one table and the images in another unless theres an easier way to do this i have no idea where to go from here this script below took all night to find and get working so help is desperatly needed and much appreciated

<?php     // images dir - relative from document root
    // this needs to be a folder that is writeable by the server
    $image_dir = '/images/';
    
    // upload dir
    $destination = $_SERVER['DOCUMENT_ROOT'].$image_dir;
        
    if(isset($_FILES))
        {
            // initialize error var for processing
            $error = array();     
            
            // acceptable files
            // if array is blank then all file types will be accepted
            $filetypes = array(
                        'ai' => 'application/postscript',
                        'bin' => 'application/octet-stream',
                        'bmp' => 'image/x-ms-bmp',
                        'css' => 'text/css',
                        'csv' => 'text/plain',
                        'doc' => 'application/msword',
                        'dot' => 'application/msword',
                        'eps' => 'application/postscript',
                        'gif' => 'image/gif',
                        'gz' => 'application/x-gzip',
                        'htm' => 'text/html',
                        'html' => 'text/html',
                        'ico' => 'image/x-icon',
                        'jpg' => 'image/jpeg',
                        'jpe' => 'image/jpeg',
                        'jpeg' => 'image/jpeg',
                        'js' => 'text/javascript',
                        'mov' => 'video/quicktime',
                        'mp3' => 'audio/mpeg',
                        'mp4' => 'video/mp4',                        
                        'mpeg' => 'video/mpeg',
                        'mpg' => 'video/mpeg',
                        'pdf' => 'application/pdf',
                        'png' => 'image/x-png',
                        'pot' => 'application/vnd.ms-powerpoint',
                        'pps' => 'application/vnd.ms-powerpoint',
                        'ppt' => 'application/vnd.ms-powerpoint',
                        'qt' => 'video/quicktime',
                        'ra' => 'audio/x-pn-realaudio',
                        'ram' => 'audio/x-pn-realaudio',
                        'rtf' => 'application/rtf',
                        'swf' => 'application/x-shockwave-flash',
                        'tar' => 'application/x-tar',
                        'tgz' => 'application/x-compressed',
                        'tif' => 'image/tiff',
                        'tiff' => 'image/tiff',
                        'txt' => 'text/plain',
                        'xls' => 'application/vnd.ms-excel',
                        'zip' => 'application/zip'
                    );
                    
            // function to check for accpetable file type
            function okFileType($type)
                {
                    // if filetypes array is empty then let everything through
                    if(count($GLOBALS['filetypes']) < 1)
                        {
                            return true;
                        }
                    // if no match is made to a valid file types array then kick it back
                    elseif(!in_array($type,$GLOBALS['filetypes']))
                        {
                            $GLOBALS['error'][] = $type.' is not an acceptable file type. '.
                                                  $type.' has been ignored.';
                            return false;
                        }
                    // else - let the file through
                    else
                        {                        
                            return true;
                        }
                }
            
            // function to check and move file
            function processFile($file)
                {    
                    // set full path/name of file to be moved
                    $upload_file = $GLOBALS['destination'].$file['name'];
                    
                    if(file_exists($upload_file))
                        {
                            $GLOBALS['error'][] = $file['name'].' - Filename exists - please change your image filename';
                            return false;
                        }
                    
                    if(!move_uploaded_file($file['tmp_name'], $upload_file)) 
                        {
                            // failed to move file
                            $GLOBALS['error'][] = 'File Upload Failed on '.$file['name'].' - Please try again';
                            return false;
                        } 
                    else 
                        {
                            // upload OK - change file permissions
                            chmod($upload_file, 0755);
                            return true;
                        }    
                }
                
            // check to make sure files were uploaded
            $no_files = 0;
            $uploaded = array();
            foreach($_FILES as $file)
                {
                    switch($file['error'])
                        {
                            case 0:
                                // file found
                                if($file['name'] != NULL && okFileType($file['type']) != false)
                                    {
                                        // process the file
                                        if(processFile($file) == true)
                                            $uploaded = $file['name'];
                                    }
                                break;
                                
                            case (1|2):
                                // upload too large
                                $error[] = 'file upload is too large for '.$file['name'];
                                break;
                                
                            case 4:
                                // no file uploaded
                                break;
                                
                            case (6|7):
                                // no temp folder or failed write - server config errors
                                $error[] = 'internal error - flog the webmaster on '.$file['name'];
                                break;
                        }
                }
                
        }
?> 

Link to comment
https://forums.phpfreaks.com/topic/122539-file-uploads-with-mysql/
Share on other sites

i didnt plan on storing the file in mysql maybe i worded that wrong the files upload just like i want i just have no idea how to get the location intomysql with the other data from the form so i can pull it out as a full set..

 

Sorry if i worded that the wrong way earlier

 

but i need just the path in mysql as the files already upload properly.

 

 

so, you don't know how to use mysql functions in php? you need to start with a tutorial:

 

http://www.php-mysql-tutorial.com/php-mysql-upload.php

 

that tutorial tells you how to physically insert the files into mysql, which i have never found to be a good idea. you can do it their way or simply store the file name.

i understand the basics of inserting and pulling data from mysql but ive never had to insert a file path to a database before i can get all the data itself to insert but as for getting the image path and such im clueless. plus im not sure how it will work out with multiple images (i allow 25) in the form  would i need 25 varchar fields in a table then have another for setId my mysql table for the text fields is below. any advice on inserting the images and how to get the path into mysql and advice on how to format the table that stores the images?  Id will be the set of images id so it would be in both tables

 

CREATE TABLE `gallery` (
  `Id` int(5) NOT NULL auto_increment,
  `Title` varchar(250) NOT NULL,
  `Cat` varchar(250) NOT NULL,
  `Photographer` varchar(250) NOT NULL,
  `Makeup` varchar(250) NOT NULL,
  `Hair` varchar(250) NOT NULL,
  `Wardrobe` varchar(250) NOT NULL,
  `pcredit` varchar(250) NOT NULL,
  `mcredit` varchar(250) NOT NULL,
  `hcredit` varchar(250) NOT NULL,
  `wcredit` varchar(250) NOT NULL,
  PRIMARY KEY  (`Id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

if the images will be stored in one directory, there is no need to store the directory/path, since you already know it. if that's the case, i'd store just the image name, probably in a varchar(64). storing a path or file name is the same as storing any other string, e.g.,

 

$sql = "INSERT INTO some_table (image_name, image_width, image_height, image_description)
VALUES ('some_image.jpg', '500', '375', 'This image is blah blah blah blah...')";

 

when you pull the data, simply append the known path/directory to the image name pulled from the database.

everytime i add an insert it stops uploading for some odd reason i can get this script uploading as it does above and  i can write an insert but i cant get them to work as one script

 

anyone want to combine them up for me

 

im sure it cant be that hard if one is skilled in php im still new at this

not sure if the js  file that runs the form is needed

but it can be found here

 

http://dropfaithproductions.com/test/multifile_compressed.js

also still wondering if i need to make 25 fields for images or not sorry im clueless when it comes to php im trying to learn all this but so far im still pretty lost on this kinda stuff

Archived

This topic is now archived and is closed to further replies.

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