Uploading Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Uploading Script

Post 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...
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post 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
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post 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
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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);
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post by admaster »

Yay!!! It works!!! :D

Thanks a lot. !!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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()
Post Reply