Current location: Hot Scripts Forums » Programming Languages » PHP » Auto-rename when upload?


Auto-rename when upload?

Reply
  #1 (permalink)  
Old 01-14-04, 12:55 PM
dragge dragge is offline
New Member
 
Join Date: Dec 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Auto-rename when upload?

I have a image upload script on my site and when the user submits a file with a space in it my other "Random Image" script flips out and cant read/print the image out and i get that "cute" little box with a red X in it.

What and Where, should i write in my upload script so that it checks if the filename has a space in it and auto renames the file when uploaded.

Like:
User uploading file "me and myself.jpg" the script will make it "me_and_myself.jpg" or simply take the spaces away like "meandmyself.jpg".

Where should i put this kinda code in my upload script?

Code:
<?
if($_POST["submit_file"]) {

    $path = "file/";
    $maxsize = 400000;

    if(is_uploaded_file($HTTP_POST_FILES["userfile"]["tmp_name"])) {

       if($HTTP_POST_FILES["userfile"]["size"]>$maxsize) {
          echo("File is too big<br>\n"); exit;
          }

       if(($HTTP_POST_FILES["userfile"]["type"]=='image/gif') || ($HTTP_POST_FILES["userfile"]["type"]=='image/pjpeg') || ($HTTP_POST_FILES["userfile"]["type"]=='image/jpg')) {
          if(file_exists($path . $HTTP_POST_FILES["userfile"]["name"])) {
             echo("The file already exists!<br>\n"); exit;
             }

          $res = copy($HTTP_POST_FILES["userfile"]["tmp_name"], $path . $HTTP_POST_FILES["userfile"]["name"]);

          if(!$res) {
              echo("Upload failed!<br>\n"); exit;
              } else {
              echo("Upload sucessful!<br>\n");
              }

          echo("File link: <a target='_blank' href='http://www.turboklubben.se/file/".$HTTP_POST_FILES["userfile"]["name"]."'>http://www.turboklubben.se/file/".$HTTP_POST_FILES["userfile"]["name"]."</a><br>\n");

       } else {

          echo("Wrong filetype!<br>\n"); exit;

       }
    }
}
?>
Reply With Quote
  #2 (permalink)  
Old 01-15-04, 03:03 AM
Gtwy Gtwy is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Pittsburgh, Pennsylvania, USA
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Try using something along these lines...
You can make changes to the code later.. this is something I use on my site:

For the HTML page with the form:

Code:
<form enctype="multipart/form-data" action="images/upload.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
    choose image: <input name="userfile" type="file" />
    <input type="submit" value="Upload File" />
</form>
For the PHP page with the PHP (duh):

PHP Code:

<?php

$uploadDir 
'/path/to/images/';
$uploadFile $uploadDir $_FILES['userfile']['name'];
$localFile $_FILES['userfile']['name'];
$before strtolower($localFile);
$brokenfile explode("."$before);
$end $brokenfile[1];

if ((
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) AND ($end == "jpg" || $end == "gif" || $end == "jpeg" || $end == "bmp" || $end == "png"))
{
    print 
"<center><br><br><br><a href=\"http://www.website.net/images\"><img src=\"$localFile\" border=0></a><br><br></center>";
}
else
{
    print 
"Possible file upload attack!  Here's some debugging info:\n";
    
print_r($_FILES);
}

?>

And then for the PHP in your image directory try using this (make it index.php):

PHP Code:

<?php

$exts 
"jpg jpeg gif png bmp";
$files = array();
$i = -1;
$handle opendir("./");
$exts explode(" "$exts);

while (
false !== ($file readdir($handle))) {
        foreach(
$exts as $ext) {
                if (
preg_match("/\.".$ext."$/i"$file$test)) {
                        
$files[] = $file;
                        
$i++;
                }
        }
}
closedir($handle);
for(
$i=0;$i<count($files);$i++){
echo 
"<img src=\"".$files[$i]."\"><br>\n";
}
?>

Of course, you're going to want to throw in some actual HTML around all that code/php so that it doesn't look like shit. I didn't write all that code, I just modified it a bunch. Honestly I can't say who came up with it originally because my friend Davin sent it to me.

Last edited by Gtwy; 01-15-04 at 03:06 AM.
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
Upload rename, resize & database insert therat PHP 3 01-16-04 02:42 PM
Need help with upload size Lexmx PHP 4 12-24-03 01:54 AM
Help with my upload script TheMetsAreBad PHP 2 12-04-03 06:10 PM
What to watch when implementing an upload script johno PHP 0 11-21-03 07:00 PM
File upload and path to mysql dalio PHP 0 08-12-03 08:24 AM


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