Link to home
Start Free TrialLog in
Avatar of livegirllove
livegirllove

asked on

very simple file upload script for multiple files

I need an upload script that will upload multiple files at once.  I need to set the file names to be the same everytime.  So it renames the files after uploading.  I need to upload 5-10 files and have them automatically named 1.gif through 10.gif.   I have a couple of scripts that do this fine for one file at a time but I need to do multiple for a slide show.

Thanks-

Heres what I use now.  If I could some how use a meta refresh after submitting.  

Thanks-

<?php

$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = "../images/";
$upload_url = "http://####";
$message ="";

//create upload_files directory if not exist
//If it does not work, create on your own and change permission.
if (!is_dir("images")) {
      die ("upload_files directory doesn't exist");
}

if ($_FILES['userfile']) {
      $message = do_upload($upload_dir, $upload_url);
}
else {
      $message = "Invalid File Specified. Ignore if you havent clicked upload yet";
}

print $message;

function do_upload($upload_dir, $upload_url) {

      $temp_name = $_FILES['userfile']['tmp_name'];
      $file_name = '1.gif';
      $file_type = $_FILES['userfile']['type'];
      $file_size = $_FILES['userfile']['size'];
      $result    = $_FILES['userfile']['error'];
      $file_url  = $upload_url.$file_name;
      $file_path = $upload_dir.$file_name;

      //File Name Check
    if ( $file_name =="") {
          $message = "Invalid File Name Specified";
          return $message;
    }
    //File Size Check
    else if ( $file_size > 500000) {
        $message = "The file size is over 500K.";
        return $message;
    }
    //File Type Check
    else if ( $file_type == "text/plain" ) {
        $message = "Sorry, You cannot upload any script file" ;
        return $message;
    }

    $result  =  move_uploaded_file($temp_name, $file_path);
    $message = ($result)?"File url <a href=$file_url>$file_url</a><br><img src=$file_url height=\"210\" width=\"210\">" :
                "Somthing is wrong with uploading a file.";

    return $message;
}
?>
<style type="text/css">
<!--
.style1 {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-weight: bold;
}
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style>

<title>Slideshow Upload Page</title><form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
  <span class="style1">Upload Slideshow Image<br>
  </span>  
  <input type="file" id="userfile" name="userfile"><br>
  <input type="submit" name="upload" value="Upload">
</form>
<p class="style2">Click Browse to choose the slideshow file (1.gif) on your computer or office network.</p>
<p class="style2">Click Upload</p>
<p class="style2">The image displayed will be in the slideshow on the front page of lulushoponline </p>
<p class="style2"><a href="collage_upload.php">Change Collage Picture</a></p>
<p class="style2"><a href="slideshow_upload.php">Change Slideshow Image</a></p>
<p><span class="style2"><a href="featured_upload.php">Change Featured Items</a></span> </p>
SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you just explain why the other solution fits better for you than mine?
Avatar of livegirllove
livegirllove

ASKER

Cut and pasted and it worked.  Doesnt fit any better than that.

Although I thought I split the points 325/175 because your answer was informative and worked with the script I was already familiar with.  

Must be tired.  Ill post a comment in community to have it reopened.
I saw this was one of the top 10 viewed answers, but it took me over an hour to figure somethin out here. So i know this is closed, but so no one else gets confused, The above was wrong:
 $_FILES['userfile'][$id]['type'];
$_FILES['userfile'][$id]['size'];
$_FILES['userfile'][$id]['error'];

is all wrong, so i am correcting it all of those and the others should be

$_FILES['userfile']['type'][$id];
$_FILES['userfile']['size'][$id];
$_FILES['userfile']['error'][$id];
hnmm.
ok, i seem to remember it working, but ill have to check through my files.  I cant remember what I used this on....
Hello,

The below script has been causing me problems for quite some time. All it does is upload the filename in to the database. I need it to upload an image into the images directory on our website. Please help.

$sqlQuery = "SELECT * FROM motorcycles WHERE id = '$this->_id'";
$sqlServer->execQuery($sqlQuery);
$data = $sqlServer->getDataSetArray();

$this->_product_image_1 = $data["image_name_1"];
$this->_product_image_2 = $data["image_name_2"];
$this->_product_image_3 = $data["image_name_3"];
$this->_product_image_4 = $data["image_name_4"];
$this->_product_thumbnail = $data["thumbnail"];
             
             

$this->_product_image_dir = "http://www.ourwebsite.com/images" . "/";

$filedir = $GLOBALS["base_imagedir"];
$file_name = "thumb_" . $_FILES['new_thumb']['name'];
 if($_FILES['new_thumb']['name'] != '')
 {
     @unlink($filedir . $file_name);
     $copy = copy($_FILES['new_thumb']['tmp_name'],$filedir . $file_name);
     if($copy){
          echo "$file_name | uploaded successfuly!<br>";
          $real_path = realpath($uploads);
          echo $real_path;
              }else{
         echo "$file_name | could not be uploaded!<br>";
         }      
        $this->_product_thumbnail = $file_name;
 }
Thanks,
mbigrad