Project im working on.

PHP coding talk.

Moderator: Moderators

Project im working on.

Postby oxynic on Sun Apr 30, 2006 12:53 am

Hello.

My names Nic and I am a very new to php coding, so I just started coding today.

So far, I have 3 files, and all work perfectly, but it needs tweaking!

I tweaked the last part to display the link to the file and a hr after to show that it finished the upload of that file.

Here is what I have so far.
There is the style and stuff but you only need the important stuff.

http://files.gamer-grid.com

index.php
Code: Select all
<form name="form1" method="post" action="uploadForm2.php">
  <p>Enter the amount of uploads you will need below. Max = 5.</p>
  <p>
    <select name="uploadNeed" id="uploadNeed">
  <option value="1">1 Upload</option>
  <option value="2">2 Uploads</option>
<option value="3">3 Uploads</option>
<option value="4">4 Uploads</option>
<option value="5">5 Uploads</option></select>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>


uploadform2.php
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>
  <?
  // end of for loop
  }
  ?>
  <p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>


processfiles.php
Code: Select all
<?php
$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'],$file_name);
// check if successfully copied
if($copy){
echo "$file_name | uploaded sucessfully!<br>";
echo "Use this link to download it and send it to others!<br>";
echo "<a href='http://files.gamer-grid.com/$file_name'>http://files.gamer-grid.com/$file_name</a><br><br><hr>";
}else{
echo "$file_name | could not be uploaded!<br>";
}
} // end of loop
?>


Now I want it at the very end to say like "3/4 uploads sucessfull". where yea.

im thinking for the processfiles:
Code: Select all
<?php
$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'],$file_name);
// check if successfully copied
if($copy){
echo "$file_name | uploaded sucessfully!<br>";
echo "Use this link to download it and send it to others!<br>";
echo "<a href='http://files.gamer-grid.com/$file_name'>http://files.gamer-grid.com/$file_name</a><br><br><hr>";
$successful + 1 == $succesfull
}else{
echo "$file_name | could not be uploaded!<br>";
}
} // end of loop
?>
<center><php echo $successful ?>/<php echo $boxes ?></center>


Something like that.

Is it like BASIC programming, like I created programs for my calculator, but php looks somewhat similar so i think ill learn quickly.

Thanks.
oxynic
Registered User
Registered User
 
Posts: 6
Joined: Sat Apr 29, 2006 11:53 pm

Postby Synaptic Anarchy on Sun Apr 30, 2006 9:37 am

Most of these languages look similar. If you have any experience with the C family, you'll notice a lot of similarities between PHP and that, too.
Die wunder dieser welt werden dir geschenkt.

Ò_ó [ b r e a k . s t u f f ] - Finally broken!
User avatar
Synaptic Anarchy
Registered User
Registered User
 
Posts: 294
Joined: Thu Feb 23, 2006 6:51 am
Location: Anarchy

Postby oxynic on Sun Apr 30, 2006 3:29 pm

I tried using C, never really got interested in it.

I got C++ for dummies, but didnt help much o.0

Anyways, I can look through my book more or might as well get a PHP for dummies book.
oxynic
Registered User
Registered User
 
Posts: 6
Joined: Sat Apr 29, 2006 11:53 pm

Postby UseLess on Sun Apr 30, 2006 5:47 pm

Greetings,

Instead of having lots of files why not just have a single file that does everything, as it's all related anyway, you then just output a form based on the previous selection.

Basically;

Code: Select all
<?php
/*
   filename: upload.php
*/

$mode = ( isset($_POST['mode']) ? $_POST['mode'] : '');

$filepath = $_SERVER['PATH_TRANSLATED'];
$pathparts = pathinfo($filepath);
$form_action = $pathparts['basename'];

$max_uploads = 5;

// debugging should be removed in completed script
echo 'The current mode is: ' . ( $mode != '' ? $mode : 'not set' ) . '<br />';

switch( $mode )
{
   case 'upload':
      // do the upload stuff in here
      $num_uploads = ( isset($_POST['uploads']) ? intval($_POST['uploads']) : 0);
      
      if( $num_uploads != 0 )
      {
         $html = '<form name="form1" method="post" action="' . $form_action . '">';

         for($x = 0; $x < $num_uploads; $x++)
         {
            $html .= '<input name="uploadfile' . $x . '" type="file">';
            $html .= '<br />';
         }

         $html .= '<input name="uploads" type="hidden" value="' . $num_uploads . '">';
         $html .= '<input type="hidden" name="mode" value="process">';
         $html .= '<p><input type="submit" name="Submit" value="Submit"></p>';
         $html .= '</form>';
         
         echo $html;
      }
      else
      {
         // output an error which should never happen...
         echo 'For some very strange reason the number of uploads is zero, this should not happen';
         exit;
      }
      break;
   
   case 'process':
      // do the processing of the files in here
      
      break;
   
   default:
      // no mode set so just output the main selection form
      $html = '<form name="form1" method="post" action="' . $form_action . '">';
      $html .= '<p>Enter the amount of uploads you will need below. Max = ' . $max_uploads . '.</p>';
      $html .= '<select name="uploads">';
      
      for($i = 1; $i < $max_uploads + 1; $i++)
      {
         $html .= '<option value="' . $i . '">' . $i . ( $i == 1 ? ' Upload' : ' Uploads') . '</option>';
      }
      
      $html .= '</select>';
      $html .= '<input type="hidden" name="mode" value="upload">';
      $html .= '<p><input type="submit" name="Submit" value="Submit"></p>';
      $html .= '</form>';

      echo $html;      
      break;
}

?>


I'll leave it to you to complete it.... ;)
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby oxynic on Sun Apr 30, 2006 6:53 pm

Well, i like to have everything seperated to debug it easier.

Also a question,

if i put.


Code: Select all
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],$file_name);
// check if successfully copied
if($copy){
echo "$file_name | uploaded sucessfully!<br>";
echo "Use this link to download it and send it to others!<br>";
echo "<a href='http://files.gamer-grid.com/$file_name'>http://files.gamer-grid.com/$file_name</a><br><br><hr>";
$uploaded = $uploaded + 1;
}else{
echo "$file_name | could not be uploaded!<br>";
}
} // end of loop
echo "$uploaded/$uploadNeed Uploads Successful";
?></center>


can i just put
Code: Select all
if $uploaded = $uploadeneed;
then {
echo "<span style="color: green">All uploads succesful</span>";
}
else {
echo "<span style="color: red">All uploads were not succesful, please look at the file listing above for more details</span>";
}


will that work, if soo then php is VERY similar to BASIC coding.

Will that work?
oxynic
Registered User
Registered User
 
Posts: 6
Joined: Sat Apr 29, 2006 11:53 pm

Postby oxynic on Sun Apr 30, 2006 6:57 pm

ok i tried it and it got

Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/csclan/public_html/files/processFiles.php on line 272
oxynic
Registered User
Registered User
 
Posts: 6
Joined: Sat Apr 29, 2006 11:53 pm

Postby UseLess on Sun Apr 30, 2006 7:04 pm

Greetings,

Yes logical structure of a PHP script is the same as most other languages.

The 'if' statement requires the condition to be enclosed in '( ... )' so 'if condition' won't work where as 'if(condition)' will.

You want to learn PHP then buy a book on it or just read the php manual available from http://www.php.net there are html and chm versions you can download.

And as the files your creating do one thing, upload files, I'd create one file that did everything as debugging a single file is a lot easier than debugging seperate files.
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby oxynic on Sun Apr 30, 2006 7:10 pm

thats true, but im just experimenting first.

my next one might have one file.

also how can I restrict html and php files with this script?
oxynic
Registered User
Registered User
 
Posts: 6
Joined: Sat Apr 29, 2006 11:53 pm

Postby oxynic on Sun Apr 30, 2006 11:44 pm

Ok I got it to the point I REALLY like it.

Now im making a "get.php" file.

I want it to automatically download the file after 5 seconds but i dont want it to redirect.
This is what I have so far.

Code: Select all
<?php
$file = $_REQUEST['file'];
echo "Retrieving $file Please wait 5 seconds";

// sleep for 5 seconds
sleep(5);

?>


Also ill edit the processfiles.php to have it link to the get.php?file=$filename or the variable i defined for the filename.

Any help appreciated.

http://files.gamer-grid.com
http://files.gamer-grid.com/get.php
oxynic
Registered User
Registered User
 
Posts: 6
Joined: Sat Apr 29, 2006 11:53 pm


Return to PHP Programming

Who is online

Users browsing this forum: Majestic-12 [Bot] and 0 guests

cron