Page 1 of 1

Uploading Script

Posted: Tue Nov 28, 2006 8:29 pm
by admaster
I have an uploading script, and have a few probelms.

processFiles.php:

Code: Select all

<?
//THIS IS THE SCRIPT
//THIS IS THE SCRIPT
//THE PROCESS SCRIPT IS BELOW
//EDIT THE SCRIPT BELOW FOR INFORMATION
$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],"uploads/$_POST['user']/$file_name");
 // check if successfully copied
 if($copy){
 echo "The File $file_name was uploaded sucessfully!<br>";
 }else{
 echo "Sorry! The File $file_name could not be uploaded!<br>";
 }
} // end of loop
//THE PHP SCRIPT ENDS HERE
?>
uploadForm1.php

Code: Select all

<form name="form1" method="post" action="uploadForm2.php">
  <p>Enter the amount of boxes you will need below.</p>
  <p>
    <input name="uploadNeed" type="text" id="uploadNeed" maxlength="3" onfocus="highlight(this);" onblur="unHighlight(this);">
  </p>
<td class="input"><input class="" name="pagename" onfocus="highlight(this);" onblur="unHighlight(this);" type="text"></td>

  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>

Code: Select all

<form name="form1" enctype="multipart/form-data" method="post" action="processFiles.php">
  <p>
  <?
  // start of dynamic form
  $uploadNeed = $_POST['uploadNeed'];
  for($x=0;$x<$uploadNeed;$x++){
  ?>
    <input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>">
  </p>
<p>
User*: <input name="user" type="text" id="user" maxlength="100">
</p>
  <?
  // end of for loop
  }
  ?>
  <p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
I want a script that will upload files, but it doesn't work...

Posted: Tue Nov 28, 2006 8:34 pm
by evilchris2003
could we see some errors for the script ?

also i would suggest if you want to use a hidden field just use a hidden php variable as
any1 can see that field if they view the source the way it is currently

Posted: Tue Nov 28, 2006 9:03 pm
by admaster
ERROR:

Code: Select all

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\xampp\htdocs\php\processFiles.php on line 38

Posted: Tue Nov 28, 2006 9:08 pm
by Zoxive
admaster wrote:ERROR:

Code: Select all

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\xampp\htdocs\php\processFiles.php on line 38
Should of showed this in the origonal post, and point out what line 38 is.. seeing how theres only 20 some shown...

But.

Try changing

Code: Select all

$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],"uploads/$_POST['user']/$file_name");
to:

Code: Select all

$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],"uploads/" . $_POST['user'] . "/" . $file_name);

Posted: Tue Nov 28, 2006 9:13 pm
by admaster
Yay!!! It works!!! :D

Thanks a lot. !!!

Posted: Wed Nov 29, 2006 12:34 am
by feyd
You should probably switch to move_uploaded_file() from copy().

If your code is ever run on a host with safe_mode enabled, it will likely break otherwise.

Posted: Wed Nov 29, 2006 8:46 am
by Mordred
1. About this: $_FILES['uploadFile'. $x]
Name your inputs "uploadFile[]", read "Uploading multiple files" in the manual.

2. You must also validate $_POST['user'] and $file_name, otherwise you've just given write access to the filesystem.
Using the user-provided name as a local filename is a generally bad idea, better generate your own filename with tempnam()