Web Analytics Made Easy -
StatCounter Need help understanding arrays - CodingForum

Announcement

Collapse
No announcement yet.

Need help understanding arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help understanding arrays

    Searched (even the php manual-like trying to read the shuttle launch sequence) => attempted multiple times(failed) => came here

    O.k. here we go:

    I have a family website to allow my geographically challenged family members to stay in contact and share photos/video clips.

    I've managed to write a single file upload script and now want to write a multiple file upload script. The way my single file upload script works (for security purposes) is it passes the html form through a dispatch script to verify the file they are uploading is in fact a media file format. If it is, great, include...upload.php, if not, it defaults to a "Sorry not a valid fdile format" page.

    (This single file upload form works fine, just looking to allow multiple file uploads)

    Here is my html form:

    Code:
    <form action="upload_secure.php"  method="post" enctype="multipart/form-data">
    <input type="hidden" name="family" value="upload" />
    <input type="file" class="fbgcolor" name="image" size="65" /> <br />
    			   
    <input type="submit" class="send" name="go" value="Click to Upload Photo" /> 
    </form>
    Which is sent here to verify file type:

    Code:
    case 'upload';
    
    	$filetype = $_FILES['image']['type'];
    	$mimestring = substr($filetype, 0, 15);
    
        switch($mimestring) {
    
    	case 'image/jpeg':
    	    include 'scripts/upload.php';
    	    break;
    	case 'image/pjpeg':
    	    include 'scripts/upload.php';
    	    break;
    	case 'image/png':
    	    include 'scripts/upload.php';
    	    break;
    	case 'image/x-png':
    	    include 'scripts/upload.php';
    	    break;
    	case 'image/gif':
    	    include 'scripts/upload.php';
    	    break;
    	case 'image/bmp':
    	    include 'scripts/upload.php';
    	    break;
    
    	default:
    	    include 'scripts/invalid_file.php';
    	    break;
    	}
    And finally here if it is the correct file type and recaps their uploaded file:

    Code:
    <?php
    
    if(isset($_POST['go'])) {
    
    $imagedir = "c:/htdocs/local/family/uploads/";
    $imagefile = $imagedir . basename($_FILES['image']['name']);
    
    $imagetype = $_FILES['image'] ['type'];
    $imagename = $_FILES['image'] ['name'];
    $imagesize = $_FILES['image'] ['size'];
    
    chmod($imagedir, 0755);
    
        move_uploaded_file($_FILES['image'] ['tmp_name'], $imagefile)
    	or die("<h1>No File Selected...</h1>
    		<p></p>
    		<div class='box'>Please <a href='/local/family/family_zone.php'>Try Again</a></div>");
    
    ?>
    
      <h1>Image Uploaded Successfully</h1>
    
        <h2>Your File Information:</h2>
    
        <ul>
    
    	<li><span class="bold">Image Name:</span> <?php echo $imagename; ?> <span class="bold">**</span></li>
    	<li><span class="bold">Image Type:</span> <?php echo $imagetype; ?></li>
    	<li><span class="bold">Image Size:</span> <?php echo $imagesize; ?> <span class="small">bytes</span></li>
    
        </ul>
    Could someone point me in the right direction?

    Forever GRATEFUL for remembering when you were learning and helping me learn.

    Sincerely,

    Integrated-Tech

  • #2
    <a href="upload.html">Click here to upload another image</a>
    This will save you alot of time fiddling lol!

    But never the less, did you still want a set number of images at a time like 5 or do you want to allow them to choose how many they want to upload then display that many input boxes.

    Set number:
    Now, add some more input boxes if you just want it to have 5 or so and name them image0 image1 etc
    Add a loop around verify file type, if you dont know how to create a loop this is on the php manual. (either for, while)
    Then the same for the upload text.
    Now getting the information into the php array create a for loop to make $image[$i] = $_GET[image$i]...

    Select amount:
    Do much of the other, but need some javascript to create the amount of text boxes.. for the javascript forum.

    There will need to be quite a few tweaks involved, I havnt copied you code and tested all this so knowing me, there will be a few things to fix up, but you get the gist.

    Comment


    • #3
      This should work:
      PHP Code:
      <html>
          <head>
              <script type="text/javascript">
                  var imageNum = 2;
                  function morePhotos(f) {
                      var fileInput = document.getElementById('cloneMe').cloneNode(true), label = fileInput.getElementsByTagName('label')[0], input = fileInput.getElementsByTagName('input')[0], beforeMe = document.getElementById('beforeMe');
                      fileInput.id = '';
                      label.setAttribute('for', 'image' + imageNum);
                      while(label.lastChild) {
                          label.removeChild(label.lastChild);
                      }
                      label.appendChild(document.createTextNode(' Image ' + imageNum + ' '));
                      input.id = 'image' + imageNum++;
                      beforeMe.parentNode.insertBefore(fileInput, beforeMe);
                  }
              </script>
          </head>
          <body>
          <?php
          
      if(isset($_POST['upload']) && $_POST['upload'] == 1) {
              
      ?>
              <h1>Image Upload Information</h1>
              <ul>
              <?php
              define
      ('IMAGE_DIR''c:/htdocs/local/family/uploads/');
              if([email protected]
      chmod(IMAGE_DIR0755)) die('<li><h2>Unable to match prerequisites for uploading files</h2></li></ul>');
              for(
      $i 0$count count($_FILES['image']['name']); $i $count$i++) {
                  
      $type $_FILES['image']['type'][$i];
                  
      $baseName basename($_FILES['image']['name'][$i]);
                  switch(
      substr($type015)) {
                      case 
      'image/jpeg':
                      case 
      'image/pjpeg':
                      case 
      'image/png':
                      case 
      'image/x-png':
                      case 
      'image/gif':
                      case 
      'image/bmp':
                          if(@
      move_uploaded_file($_FILES['image']['tmp_name'][$i], IMAGE_DIR.'/'.$baseName)) {
                              
      ?>
                                <li><h2>Image "<?php echo $baseName?>" Uploaded Successfully</h2>
                                  <ul>
                                      <li><h3>File Information:</h3>
                                          <ul>
                                              <li><span class="bold">Image Name:</span> <?php echo $baseName?> <span class="bold">**</span></li>
                                              <li><span class="bold">Image Type:</span> <?php echo $type?></li>
                                              <li><span class="bold">Image Size:</span> <?php echo $_FILES['image']['size'][$i]; ?> <span class="small">bytes</span></li>
                                          </ul>
                                      </li>
                                  </ul>
                              </li>
                              <?php
                          
      }
                          else {
                              
      ?>
                              <li><h2>Image "<?php echo $baseName?>" could not be uploaded</h2></li>
                              <?php
                          
      }
                          break;
                      default:
                          
      ?>
                          <li><h2>"<?php echo $baseName?>" could not be uploaded (file format not allowed)</h2></li>
                          <?php
                          
      break;
                  }
              }
              if(
      $i == 0) {
                  
      ?>
                  <li><h2>No File Selected</h2></li>
                  <?php
              
      }
              
      ?>
              </ul>
              <div class='box'><a href=''>Upload more files</a></div>
              <?php
          
      }
          else {
              
      ?>
              <form method="post" enctype="multipart/form-data">
                  <input type="hidden" name="upload" value="1" />
                  <input type="hidden" name="family" value="upload" />
                  <div id="cloneMe"><label for="image1"> Image 1 </label><input type="file" class="fbgcolor" name="image[]" id="image1" size="65" /></div>
                  <input type="button" id="beforeMe" value="Upload more Photos" onClick="morePhotos(this.form)" /> <input type="submit" class="send" name="go" value="Click to Upload Photos" /> 
              </form>
              <?php
          
      }
          
      ?>
          </body>
      </html>
      The brackets of the inputs' name ("image[]") cause the files to be sent as an array. On the server-side you can access the informations of each file with $_FILE['image']['name'][<number of file>] (type, size and temporary name of the file are accessed likewise).

      The "Upload more Photos" button just triggers a bit javascript, which inserts another input before the button.

      dumpfi
      "Failure is not an option. It comes bundled with the software."
      ....../)/)..(\__/).(\(\................../)_/)......
      .....(-.-).(='.'=).(-.-)................(o.O)...../<)
      ....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
      ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
      Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

      Comment

      Working...
      X