Jump to content

While loop, changing variable name


curtis_b

Recommended Posts

I'm building a system where a variable amount of files (1 to 6) will need to be uploaded depending on the situation. The upload process is currently a 3 page ordeal. I am trying to manipulate this script:

[a href=\"http://www.phpfreaks.com/quickcode/A-Simple-File-Upload-trust-me-its-good/335.php\" target=\"_blank\"]http://www.phpfreaks.com/quickcode/A-Simpl...ts-good/335.php[/a]


1st page: basic information is filled out, the number of file uploads is entered in an input field

2nd page: number of file uploads required is stored as session variable, a while loop is used to create that specific number of upload dialogues - so far so good.

3rd page: again, a while loop is used to perform the upload actions, this is where my script seems to be failing. I suspect that it is because I'm trying to stick one variable (a counter) inside of another's name
example: $_FILES['file_'.$Version_Counter.' ']

I have also tried

$_FILES['file_$Version_Counter'] with no luck

I realize I could probably do this without a loop since there is a maximum of 6 repititions, but I think the loop would make for cleaner code.

Let me know what you think, thanks for your help!

Curt
Link to comment
https://forums.phpfreaks.com/topic/5508-while-loop-changing-variable-name/
Share on other sites

ok, here's a stripped down version of what we're looking at.

For the sake of this post, I put questions marks ??? right in the spot where I beleive the problem lies. I don't know how to insert a variable/counter inside of the $_File variables name (should be $_FILE['file_1'], $_FILE['file_2'], etc. I want that number to come from the counter variable.)
============
PAGE 2:
============
<?
session_start();

?>
<form action="upload3.php" method="post" enctype="multipart/form-data">
<?
$_SESSION['$Num_Versions'] = $Num_Versions_Temp;
$Version_Counter = 1;
While ($Num_Versions_Temp > 0)
{
?>
Browse a File to Upload: 50mb limit.<b>Version <? echo $Version_Counter; ?></b> <br>
<input type="file" name="file_<? echo $Version_Counter;?>"> << any punctuation or spaces in filename may cause the upload to fail. Use_underscores_if_you_need_to.<br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<?
$Version_Counter++;
$Num_Versions_Temp--;
}
?>
<input type="Submit" value="Submit Proof">
</form>

================
PAGE 3:
================
<?
session_start();
$_SESSION['$Num_Versions'] = $Num_Versions_Temp;
$Version_Counter=1;
while ($Num_Versions_Temp>0)
{

/* Description -----------------------------------------------------
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES['upload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['upload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['upload']['name'] ==> Returns the Actual Name of the File.
$_FILES['upload']['type'] ==> Returns the Type of the File.

So if I file_1 the file 'test.doc', the $_FILES['upload']['name']
would be 'phptut.doc' and $_FILES['upload']['type'] would be 'application/msword'.
---------------------------------------------------------------------*/
// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "d:\\home\\sites\\mywebsite.com\\wwwroot\\proofs\\files\\"; //change to whatever you want.
// files less than 50MB
$size_bytes = 50248576; //bytes will be uploaded
//check if the directory exist or not.
if (!is_dir("$upload_dir")) {
die ("The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)");
}

//Check first if a file has been selected
//is_file_1_file('filename') returns true if
//a file was file_1 via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['file_???']['tmp_name']))
{

//Get the Size of the File
$size = $_FILES['file_???']['size'];
//Make sure that $size is less than 1MB (1000000 bytes)
if ($size > $size_bytes)
{
echo "File Too Large. Please try again.";
exit();

}
// $filename will hold the value of the file name submetted from the form.
$filename = $_FILES['file_???']['name'];

//this is where you should have the filename inserted to the related row


// Check if file is Already EXISTS.
if(file_exists($upload_dir.$filename)){
echo "Oops! The file named <b>$filename </b>already exists";
exit();
}

//Move the File to the Directory of your choice
//move_file_1_file('filename','destination') Moves an file_??? file to a new location.
if (move_uploaded_file($_FILES['file_???']['tmp_name'],$upload_dir.$filename)) {


}
else
{
//Print error
echo "There was a problem moving your file";
exit();

}
}

$Version_Counter++;
$Num_Versions_Temp--;
echo "<b>".$filename."</b> has been uploaded.<br><br>";
}
There are quite a few errors in your code. Let's first start with your form. You have:
[code]<?php
session_start();
?>
<form action="upload3.php" method="post" enctype="multipart/form-data">
<?
$_SESSION['$Num_Versions'] = $Num_Versions_Temp;
$Version_Counter = 1;
While ($Num_Versions_Temp > 0)
{
?>
Browse a File to Upload: 50mb limit.<b>Version <? echo $Version_Counter; ?></b> <br>
<input type="file" name="file_<? echo $Version_Counter;?>"> << any punctuation or spaces in filename may cause the upload to fail. Use_underscores_if_you_need_to.<br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<?
$Version_Counter++;
$Num_Versions_Temp--;
}
?>
<input type="Submit" value="Submit Proof">
</form>[/code]
In these lines:
[code]<?php
$_SESSION['$Num_Versions'] = $Num_Versions_Temp;
$Version_Counter = 1;
While ($Num_Versions_Temp > 0)
?>[/code]
You are storing the value of "$Num_Versions_Temp" in the session variable $_SESSION['$Num_Versions'] and then you use ""$Num_Versions_Temp" in a while statement. Where is the variable "$Num_Versions_Temp" set?

You can simplify the generation of your form and the rest of your code by turning the name on the "<input type="file">" tag into an array:
[code]<?
session_start();
$size_bytes = 50000; // I didn't know where this was set in your code
?>
<form action="upload_multiple.php" method="post" enctype="multipart/form-data">
<?
$Num_Versions_Temp = (isset($_SESSION['Num_Versions']))?$_SESSION['Num_Versions']:3;
$Version_Counter = 1;
for ($Version_Counter=1;$Version_Counter <= $Num_Versions_Temp;$Version_Counter++) {
?>
Browse a File to Upload: 50mb limit.<b>Version <? echo $Version_Counter; ?></b> <br>
<input type="file" name="input_file[]"> << any punctuation or spaces in filename may cause the upload to fail. Use_underscores_if_you_need_to.<br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<?
}
?>
<input type="Submit" name="submit_proof" value="Submit Proof">
</form>[/code]

To see how this works, create a script named "upload_multiple.php" that contains
[code]<?php echo '<pre>'.print_r($_FILES,true).'</pre>'; ?>[/code]

You then should be able to modify the rest of your code accordingly.

Ken

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.