Current location: Hot Scripts Forums » Programming Languages » PHP » Rename file uploads


Rename file uploads

Reply
  #1 (permalink)  
Old 12-20-03, 12:27 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Rename file uploads

Anyone know how to implement the rename function to this setup? I want to rename files to avoid the possibility of filenames with the same name.

PHP Code:

 $file_types = array(   
     
'image/pjpeg' => 'jpg'
     
'image/jpeg' => 'jpg'
     
'image/bmp' => 'bmp',
    ); 
     
$fname=$_POST['fname']; 
     
$picname=$_POST['picname'];
     
$desc=$_POST['desc'];
    
     
$max_size 2000000;
     
$filesize $_FILES['upfile']['size'];
     
$filetype $_FILES['upfile']['type'];
     
$upload_dir $_SERVER['DOCUMENT_ROOT'] . "/files/"
       
        if (
$filesize $max_size){
           
header ("location: size_error.php");
           exit;
        }
         if (!
array_key_exists($filetype$file_types)) { 
                
header ("location: type_error.php"); 
            exit;
        } 
        else
        {   
                if (
move_uploaded_file($_FILES['upfile']['tmp_name'], $upload_dir $_FILES['upfile']['name'])) 
                { 
                
$filename $_FILES['upfile']['name'];
              include(
"connect.php");
              
$conn mysql_connect("localhost","$user","$pass") or die ("Could not connect to MySQL"); 
              
mysql_select_db($db,$conn) or die ("Could not open database"); 
              
$query "INSERT INTO ratings VALUES('','$fname','$picname','$desc','$filename','','',now())"
              
$result=mysql_query($query); 
      }
   }
mysql_close(); 
header ('Location: upload_success.php'); 
Reply With Quote
  #2 (permalink)  
Old 12-21-03, 01:03 AM
Beaver Beaver is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Anaheim CA
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
When you say rename the image files, do you mean to a completely random filename?
Reply With Quote
  #3 (permalink)  
Old 12-21-03, 01:06 AM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Yes...I tried using the randonfilename function with the above, and the files uploaded and info went into the db, but the file extension was lost when it was renamed.
Reply With Quote
  #4 (permalink)  
Old 12-21-03, 01:16 AM
Beaver Beaver is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Anaheim CA
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

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

rename($upload_dir $_FILES['upfile']['name'], $new_file); 
Perhaps something like that after the if move uploaded file line.
Reply With Quote
  #5 (permalink)  
Old 12-21-03, 10:01 AM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by Beaver
PHP Code:

$new_file substr(sha1(rand(10time())), 08) . '.' $file_types[$filetype];
rename($upload_dir $_FILES['upfile']['name'], $new_file); 
Perhaps something like that after the if move uploaded file line.
if (move_uploaded_file($_FILES['upfile']['tmp_name'], $upload_dir . $_FILES['upfile']['name']))
{
$new_file = substr(sha1(rand(10, time())), 0, 8) . '.' . $file_types[$filetype];
rename($upload_dir . $_FILES['upfile']['name'], $new_file);
$filename=$new_file;


include("connect.php");
$conn = mysql_connect("localhost","$dbuser","$dbpass") or die ("Could not connect to MySQL");
mysql_select_db($db,$conn) or die ("Could not open database");
$query = "INSERT INTO items VALUES('','$fname','$title','$desc','$filename',no w())";
$result=mysql_query($query);
}

Renames if perfectly, but now the file isn't uploading.

If I change to this....

$new_file = substr(sha1(rand(10, time())), 0, 8) . '.' . $file_types[$filetype];
rename($_FILES['upfile']['name'], $new_file);

it will rename the filename put into the db and upload the pic, but the pic in the folder still has the original name.

Last edited by mdhall; 12-21-03 at 10:59 AM.
Reply With Quote
  #6 (permalink)  
Old 12-21-03, 02:36 PM
Beaver Beaver is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Anaheim CA
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
I suppose you mean, it dosen't put the new file name image into the db?
Reply With Quote
  #7 (permalink)  
Old 12-21-03, 02:44 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Yes, the new filename will go into the database...the file put into the folder retains the users original filename.
Reply With Quote
  #8 (permalink)  
Old 12-21-03, 02:54 PM
Beaver Beaver is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Anaheim CA
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Ok when you used my first example, and say the file isn't uploading, are you sure? The reason the modified example of mine isn't working, is because its renaming something that dosen't exist. It's not looking in the $upload_dir as it should for the original, and then rename to the $new_file name.
Reply With Quote
  #9 (permalink)  
Old 12-21-03, 02:57 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Yes, I checked the folder and the file was never uploaded into it.
Reply With Quote
  #10 (permalink)  
Old 12-21-03, 03:03 PM
Beaver Beaver is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Anaheim CA
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Ok I haven't tested this but try changing this line:
PHP Code:

if (move_uploaded_file($_FILES['upfile']['tmp_name'], $upload_dir $_FILES['upfile']['name'])) 

to
PHP Code:

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

if (
move_uploaded_file($_FILES['upfile']['tmp_name'], $upload_dir $new_file)) 
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
Please I Need help before all my hair is gone! LisatheNovice Perl 6 11-22-03 03:05 PM
ZIP/RAR file comment/.diz/.nfo viewer script net4ward Script Requests 0 11-19-03 04:41 PM
Writes to a text file gamextremer2003 JavaScript 4 09-11-03 09:43 AM
Upload file type and size limiter! Arctic ASP 1 08-02-03 07:06 PM
ASP Renaming a file (with some filename parsing) camt ASP 2 06-30-03 09:53 PM


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