Current location: Hot Scripts Forums » Programming Languages » PHP » Upload rename, resize & database insert


Upload rename, resize & database insert

Reply
  #1 (permalink)  
Old 01-14-04, 09:16 AM
therat's Avatar
therat therat is offline
Newbie Coder
 
Join Date: Jan 2004
Location: London
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Upload rename, resize & database insert

I need to be able to upload 2 files. The first has to be an image file and he second should be a zip/rar file. I have the following code, from here, that is uploading the files correctly.

PHP Code:

<?php


  $upload_dir 
$_SERVER['DOCUMENT_ROOT'] . "/uploads/"
    
// Get the upload file array 
    // and upload them to a destination 
    // by looping through that array 

    // An array of allowed file types 
    
$file_types = array(   
     
'image/pjpeg' => 'jpg',
     
'image/jpeg' => 'jpg',
     
'image/gif' => 'gif',
     
'image/x-png' => 'png',
     
'application/zip zip' => 'zip'
    
);

    
// Get the variables from previous form 
    
$up_files $_FILES['filename']; 

    
// set the while counter 
    
$counter 0
    
    
// Start the file upload to server 
    
while($counter <= count($up_files))  { 
         
        if(
$up_files['size'][$counter] > 0) { 
             
            
// Read the mime type of current file 
            
$filetype $up_files['type'][$counter]; 
            
$filename $up_files['name'][$counter]; 
            
$tempname $up_files['tmp_name'][$counter]; 
             
            if (!
array_key_exists($filetype$file_types))  { 
                 print 
"<font color='#990000'>File <b>$filename</b> 
of type <b>
$filetype</b> is not valid for upload!</font><br />"
            } else { 
                 
                
$upload_file $upload_dir $filename

                
// THIS UPLOADS THE FILE!!!! 
                
move_uploaded_file($tempname$upload_file); 
                 
                
// Print the uploaded file 
                //print "<font color='#009900'>File <b>$filename</b> uploaded... 
                //new location: <b>$upload_file</b></font><br />"; 
            

        } 
        
$counter++; 
    } 

//echo header ("Location: index.php");

?>
I now need to store the 2 filename in a database in the same row. I also want to rename the files so they have the same filename but keeping their original extensions. Is it possible to adapt this script, or is there anything else that can already do this sort of thing. I am fairly new to PHP but am willing to learn.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-14-04, 02:13 PM
hyjacked hyjacked is offline
Wannabe Coder
 
Join Date: Nov 2003
Location: New Brunswick, Canada
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not sure of the code exactly but I do know it is possible.

in order to save them in a database, just save the filenames, paths also if you want that to, into an array, then once you are done uploading files, add the names to the database that you wish. one insert statement containing both names.

as for the renaming part, you can assume that all file extensions are 3 characters long, and since you know where the files are being saved to, you can call the rename() function(http://ca3.php.net/manual/en/function.rename.php) to rename it as you wish.

hope this helps you get started.
__________________
hyjacked
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-14-04, 04:50 PM
therat's Avatar
therat therat is offline
Newbie Coder
 
Join Date: Jan 2004
Location: London
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

I have added the following into to the code for the database insertion. I have added the following to insert into the database. Trouble is it adds 2 new entries each time. Will try saving to an array as suggested though. Thanks for the link, very helpfull.
PHP Code:

$thumb $up_files['name'][0];

$archive $up_files['name'][1];
$zipmime $up_files['type'][1];
                
$query "INSERT INTO skinz_images (image_thumb_file,image_download_url)
VALUES ('
$thumb','$archive')";
$result mysql_query($query$skinz) or die(mysql_error()); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 01-16-04, 03:42 PM
therat's Avatar
therat therat is offline
Newbie Coder
 
Join Date: Jan 2004
Location: London
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
I now have the files being renamed correctly by adding the following.

PHP Code:

$new_file substr(sha1(rand(10time())), 08) . '.' $file_types[$filetype]; 

if (
move_uploaded_file($tempname$upload_dir $new_file)); 
How do i now get the 2 new names and store them in the database. At the moment the insert query looks like this

PHP Code:

$thumb $new_file;

$archive $new_file;
$zipmime $up_files['type'][1];

$query "INSERT INTO skinz_images (image_thumb_file,image_download_url) VALUES ('$thumb',$archive','$zipmime')";
$result mysql_query($query$skinz) or die(mysql_error());
mysql_close(); 
This is located right at the ned of the script. Is this the best place for it or, should it be moved inside the actual uploading part.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL table problem perleo PHP 9 12-16-03 02:16 PM
Picture upload to database? dayzeday PHP 9 10-09-03 02:14 AM
insert into SQL database Dahquim ASP 1 07-27-03 08:34 PM
Looking for a script to upload images in a database. boskyvora PHP 2 06-16-03 05:18 AM


All times are GMT -5. The time now is 08:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.