Jump to content

Multiple File upload problem


corkdood

Recommended Posts

I have  a problem with some php code which I am using to upload several files to a directory on my web server.

 

In the past this code worked fine but my host moved the site to a different server and it hasn't worked right since.

The code will allow me to upload files which are less than a megabyte in size.  Anything larger and the script just times out.

 

I've checked with my web host about size restrictions and they tell me that the following limits are in place

max_execution_time = 3600    ; Maximum execution time of each script, in seconds

max_input_time = 1200 ; Maximum amount of time each script may spend parsing request data

memory_limit = 16M      ; Maximum amount of memory a script may consume

 

Any help would be appreciated

The code is as follows

$folder = stripslashes($folder);

$basedir = "projectuploads".$folder;

$maxfilesize=2097152;

if ($HTTP_POST_VARS['submit']) //if the submit button is pressed

{

$file_name=$_FILES['file']['name'][0];

$file_size=$_FILES['file']['size'][0];

$file_name2=$_FILES['file']['name'][1];

$file_size2=$_FILES['file']['size'][1];

$file_name3=$_FILES['file']['name'][2];

$file_size3=$_FILES['file']['size'][2];

$file_name4=$_FILES['file']['name'][3];

$file_size4=$_FILES['file']['size'][3];

$file_name5=$_FILES['file']['name'][4];

$file_size5=$_FILES['file']['size'][4];

if ($file[0]=="")

  {

    $error = "Please Select a File to Upload!";

    @unlink($HTTP_POST_FILES['file']['tmp_name']);

  }

else if (!@is_uploaded_file($_FILES['file']['tmp_name'][0]))

  {

    $error = "File has not been Uploaded!";

    @unlink($_FILES['file']['tmp_name'][0]);

  }

//else if (($_FILES['file']['type'][0] != "application/x-zip-compressed")&&($_FILES['file']['type'][0] != "application/pdf")&&($_FILES['file']['type'][0] != "application/msword"))//these are the filetypes allowed, more can be added

  // {

      // $error = "This file type is not allowed";

      // @unlink($_FILES['file']['tmp_name'][0]);

        // assign error message, remove uploaded file, redisplay form.

      //}

  else if (@file_exists($basedir."/".$file_name))

{

  $error = "This folder already contains a file of that name1, Please rename the file if you wish to upload it";

  @unlink($_FILES['file']['tmp_name'][0]);

    }

  /*else if ($_FILES['file']['size'][0]>$MAX_FILE_SIZE)//||($file_size==0))

{

    $error = "This file is too large";

    @unlink($_FILES['file']['tmp_name'][0]);

    }*/

else if (!@move_uploaded_file($_FILES['file']['tmp_name'][0],$basedir."/".$file_name))  //File has passed all validation, copy it to the final destination and remove the temporary file:

{

$error = "Sorry File could not be Uploaded";

@unlink($_FILES['file']['tmp_name'][0]);

}

    else if (!chmod($basedir."/".$file_name,0777))

  {

    $error = "Sorry chmod didn't work";

    }

else //if file upload is successful add file details to db

{//use replace instead of insert to ensure that the same filepath cannot be entered twice, it just overwrites the original one.

//Along with the fileexists() function this ensures that there cannot be duplicate files in a folder

$query = "replace into filedata(projectid,createdby,filename,filepath,filetype,filesize,datecreated,fullfilepath) values ('".getprojfolder($folder)."','$valid_user','$file_name','".addslashes($folder)."','".getext($file_name)."','$file_size',current_timestamp(),'".addslashes($folder)."/$file_name');";

$result = mysql_query($query,$db);

if ($result)

{

@unlink($_FILES['file']['tmp_name'][0]);

if ($file2 == "")

{

echo "<script type='text/javascript'>

window.opener.location.reload();

self.close();

</script>";

}

}

else

$error = "Sorry an Error Occurred, File Details could not be Saved";

}

}

//Added 2nd file option

if($file[1] != "")

{

if (!@is_uploaded_file($_FILES['file']['tmp_name'][1]))

  {

    $error = "File 2 has not been Uploaded!";

    @unlink($_FILES['file']['tmp_name'][1]);

  }

//else if (($_FILES['file']['type'][1] != "application/x-zip-compressed")&&($_FILES['file']['type'][1] != "application/pdf")&&($_FILES['file']['type'][1] != "application/msword"))//these are the filetypes allowed, more can be added

  // {

      // $error = "This file type is not allowed";

      // @unlink($_FILES['file']['tmp_name'][1]);

        // assign error message, remove uploaded file, redisplay form.

      // }

  else if (@file_exists($basedir."/".$file_name2))

{

  $error = "This folder already contains a file of that name, Please rename the file if you wish to upload it";

  @unlink($_FILES['file']['tmp_name'][1]);

    }

  else if ($_FILES['file']['size'][1]>$MAX_FILE_SIZE)//||($file_size==0))

{

    $error = "This file is too large";

    @unlink($_FILES['file']['tmp_name'][1]);

    }

else if (!@move_uploaded_file($_FILES['file']['tmp_name'][1],$basedir."/".$file_name2))  //File has passed all validation, copy it to the final destination and remove the temporary file:

{

$error = "Sorry File could not be Uploaded";

@unlink($_FILES['file']['tmp_name'][1]);

}

else if (!chmod($basedir."/".$file_name2,0777))

{

$error = "Sorry chmod didn't work";

}

else //if file upload is successful add file details to db

{

//use replace instead of insert to ensure that the same filepath cannot be entered twice, it just overwrites the original one.

//Along with the fileexists() function this ensures that there cannot be duplicate files in a folder

$query = "replace into filedata(projectid,createdby,filename,filepath,filetype,filesize,datecreated,fullfilepath)

    values ('".getprojfolder($folder)."','$valid_user','$file_name2','".addslashes($folder)."','".getext($file_name2)."','$file_size2',current_timestamp(),'".addslashes($folder)."/$file_name2');";

$result = mysql_query($query,$db);

if ($result)

{

@unlink($_FILES['file']['tmp_name'][1]);

if ($file[2] == "")

{

echo "<script type='text/javascript'>

window.opener.location.reload();

self.close();

</script>";

}

}

else

$error = "Sorry an Error Occurred, File Details for file 2 could not be Saved";

}

}

//End of added second file option

//Start third file option

if($file[2] != "")

{

if (!@is_uploaded_file($_FILES['file']['tmp_name'][2]))

  {

    $error = "File 3 has not been Uploaded!";

    @unlink($_FILES['file']['tmp_name'][2]);

  }

//else if (($_FILES['file']['type'][2] != "application/x-zip-compressed")&&($_FILES['file']['type'][2] != "application/pdf")&&($_FILES['file']['type'][2] != "application/msword"))//these are the filetypes allowed, more can be added

  // {

        //$error = "This file type is not allowed";

      // @unlink($_FILES['file']['tmp_name'][2]);

        // assign error message, remove uploaded file, redisplay form.

      //}

  else if (@file_exists($basedir."/".$file_name3))

{

  $error = "This folder already contains a file of that name, Please rename the file if you wish to upload it";

  @unlink($_FILES['file']['tmp_name'][2]);

    }

  else if ($_FILES['file']['size'][2]>$MAX_FILE_SIZE)//||($file_size==0))

{

    $error = "This file is too large";

    @unlink($_FILES['file']['tmp_name'][2]);

    }

else if (!@move_uploaded_file($_FILES['file']['tmp_name'][2],$basedir."/".$file_name3))  //File has passed all validation, copy it to the final destination and remove the temporary file:

{

$error = "Sorry File could not be Uploaded";

@unlink($_FILES['file']['tmp_name'][2]);

}

else if (!chmod($basedir."/".$file_name3,0777))

{

$error = "Sorry chmod didn't work";

}

else //if file upload is successful add file details to db

{//use replace instead of insert to ensure that the same filepath cannot be entered twice, it just overwrites the original one.

//Along with the fileexists() function this ensures that there cannot be duplicate files in a folder

$query = "replace into filedata(projectid,createdby,filename,filepath,filetype,filesize,datecreated,fullfilepath)

    values ('".getprojfolder($folder)."','$valid_user','$file_name3','".addslashes($folder)."','".getext($file_name3)."','$file_size3',current_timestamp(),'".addslashes($folder)."/$file_name3');";

$result = mysql_query($query,$db);

if ($result)

{

@unlink($_FILES['file']['tmp_name'][2]);

if($file[3] == "")

{

echo "<script type='text/javascript'>

window.opener.location.reload();

self.close();

</script>";

}

}

else

$error = "Sorry an Error Occurred, File Details for file 3 could not be Saved";

}

}

//End of added third file option

//4th file option

if($file[3] != "")

{

if (!@is_uploaded_file($_FILES['file']['tmp_name'][3]))

  {

    $error = "File 4 has not been Uploaded!";

    @unlink($_FILES['file']['tmp_name'][3]);

  }

//else if (($_FILES['file']['type'][3] != "application/x-zip-compressed")&&($_FILES['file']['type'][3] != "application/pdf")&&($_FILES['file']['type'][3] != "application/msword"))//these are the filetypes allowed, more can be added

  // {

      // $error = "This file type is not allowed";

      // @unlink($_FILES['file']['tmp_name'][3]);

        // assign error message, remove uploaded file, redisplay form.

      // }

  else if (@file_exists($basedir."/".$file_name4))

{

  $error = "This folder already contains a file of that name, Please rename the file if you wish to upload it";

  @unlink($_FILES['file']['tmp_name'][3]);

    }

  else if ($_FILES['file']['size'][3]>$MAX_FILE_SIZE)//||($file_size==0))

{

    $error = "This file is too large";

    @unlink($_FILES['file']['tmp_name'][3]);

    }

else if (!@move_uploaded_file($_FILES['file']['tmp_name'][3],$basedir."/".$file_name4))  //File has passed all validation, copy it to the final destination and remove the temporary file:

{

$error = "Sorry File could not be Uploaded";

@unlink($_FILES['file']['tmp_name'][3]);

}

else if (!chmod($basedir."/".$file_name4,0777))

{

$error = "Sorry chmod didn't work";

}

else //if file upload is successful add file details to db

{//use replace instead of insert to ensure that the same filepath cannot be entered twice, it just overwrites the original one.

//Along with the fileexists() function this ensures that there cannot be duplicate files in a folder

$query = "replace into filedata(projectid,createdby,filename,filepath,filetype,filesize,datecreated,fullfilepath)

    values ('".getprojfolder($folder)."','$valid_user','$file_name4','".addslashes($folder)."','".getext($file_name4)."','$file_size4',current_timestamp(),'".addslashes($folder)."/$file_name4');";

$result = mysql_query($query,$db);

if ($result)

{

@unlink($_FILES['file']['tmp_name'][3]);

if($file[4] == "")

{

echo "<script type='text/javascript'>

window.opener.location.reload();

self.close();

</script>";

}

}

else

$error = "Sorry an Error Occurred, File Details for file 4 could not be Saved";

}

}

//End of added 4th file option

//4th File Option

if($file[4] != "")

{

if (!@is_uploaded_file($_FILES['file']['tmp_name'][4]))

  {

    $error = "File 5 has not been Uploaded!";

    @unlink($_FILES['file']['tmp_name'][4]);

  }

//else if (($_FILES['file']['type'][4] != "application/x-zip-compressed")&&($_FILES['file']['type'][4] != "application/pdf")&&($_FILES['file']['type'][4] != "application/msword"))//these are the filetypes allowed, more can be added

// {

    //  $error = "This file type is not allowed";

      //  @unlink($_FILES['file']['tmp_name'][4]);

        // assign error message, remove uploaded file, redisplay form.

      // }

  else if (@file_exists($basedir."/".$file_name5))

{

  $error = "This folder already contains a file of that name, Please rename the file if you wish to upload it";

  @unlink($_FILES['file']['tmp_name'][4]);

    }

  /*else if ($_FILES['file']['size'][4]>$MAX_FILE_SIZE)//||($file_size==0))

{

    $error = "This file is too large";

    @unlink($_FILES['file']['tmp_name'][4]);

    }*/

else if (!@move_uploaded_file($_FILES['file']['tmp_name'][4],$basedir."/".$file_name5))  //File has passed all validation, copy it to the final destination and remove the temporary file:

{

$error = "Sorry File could not be Uploaded";

@unlink($_FILES['file']['tmp_name'][4]);

}

else if (!chmod($basedir."/".$file_name5,0777))

{

$error = "Sorry chmod didn't work";

}

else //if file upload is successful add file details to db

{//use replace instead of insert to ensure that the same filepath cannot be entered twice, it just overwrites the original one.

//Along with the fileexists() function this ensures that there cannot be duplicate files in a folder

$query = "replace into filedata(projectid,createdby,filename,filepath,filetype,filesize,datecreated,fullfilepath)

    values ('".getprojfolder($folder)."','$valid_user','$file_name5','".addslashes($folder)."','".getext($file_name5)."','$file_size5',current_timestamp(),'".addslashes($folder)."/$file_name5');";

$result = mysql_query($query,$db);

if ($result)

{

@unlink($_FILES['file']['tmp_name'][4]);

echo "<script type='text/javascript'>

window.opener.location.reload();

self.close();

</script>";

}

else

$error = "Sorry an Error Occurred, File Details for file 5 could not be Saved";

}

}

//End of added 4th file option

}

 

echo "<p align='center'><font class='text10'><b>UPLOAD FILES TO: </b>$folder</font></p>";

echo "<font face=arial size=2 color='#000033'>Please choose file names carefully and if necessary rename files before uploading.  Punctuation marks such as <b>' \" , .</b> and <b>&</b> (ampersand) should never be used in File Names.</font>";

 

start_table("85%", "ADD FILES");

echo "<form name='addfile' action='$PHP_SELF?folder=".rawurlencode($folder)."' method='post' enctype='multipart/form-data'>

<table border=0 cellpadding=4 cellspacing=0 width='100%'>

<tr bgcolor='#ebebeb'>

<td width='50%'><b>Select File 1: </b></td>

<td>";

//<input type=hidden name='MAX_FILE_SIZE' value='2097152'>

echo "<input type=file name='file[]'></td></tr>

<tr bgcolor='#ebebeb'>

<td width='50%'><b>Select File 2: </b></td>

<td><input type=file name='file[]'></td></tr>

<tr bgcolor='#ebebeb'>

<td width='50%'><b>Select File 3: </b></td>

<td><input type=file name='file[]'></td></tr>

<tr bgcolor='#ebebeb'>

<td width='50%'><b>Select File 4: </b></td>

<td><input type=file name='file[]'></td></tr>

<tr bgcolor='#ebebeb'>

<td width='50%'><b>Select File 5: </b></td>

<td><input type=file name='file[]'></td></tr>

<tr bgcolor='#ebebeb'>

<td align='center' colspan='2'><input type=submit name='submit' value='ADD FILE' onclick='Upload()'></td>

</tr>

</form>

</table>";

 

Link to comment
https://forums.phpfreaks.com/topic/44626-multiple-file-upload-problem/
Share on other sites

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.