www.webdeveloper.com
Results 1 to 4 of 4

Thread: Get input box lenght

  1. #1
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779

    Get input box lenght

    This should be very simple, but I'm being dumb and bad at PHP...

    I have the following script that allows a user to upload files onto my server:

    HTML Code:
    <form action="multiple-uploads.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
                    <p><label for="selectFile">Select file: </label><input name="ufile[]" type="file" id="ufile[]" size="25"></p>
                    <p><label for="selectFile">Select file: </label><input name="ufile[]" type="file" id="ufile[]" size="25"></p>
                    <p><label for="selectFile">Select file: </label><input name="ufile[]" type="file" id="ufile[]" size="25"></p>
                    <p><label for="selectFile">Select file: </label><input name="ufile[]" type="file" id="ufile[]" size="25"></p>
                    <p><label for="selectFile">Select file: </label><input name="ufile[]" type="file" id="ufile[]" size="25"></p>
                    <p style="text-align:center;"><input type="submit" name="Submit" value="Upload" class="submitButton"></p>
                  </form>
    PHP:
    Code:
    <?php
                    $path1= "uploads/".$HTTP_POST_FILES['ufile']['name'][0];
                    $path2= "uploads/".$HTTP_POST_FILES['ufile']['name'][1];
                    $path3= "uploads/".$HTTP_POST_FILES['ufile']['name'][2];
                    $path4= "uploads/".$HTTP_POST_FILES['ufile']['name'][3];
                    $path5= "uploads/".$HTTP_POST_FILES['ufile']['name'][4];
                    
                    copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
                    copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
                    copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
                    copy($HTTP_POST_FILES['ufile']['tmp_name'][3], $path4);
                    copy($HTTP_POST_FILES['ufile']['tmp_name'][4], $path5);
                    
                    $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
                    $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
                    $filesize3=$HTTP_POST_FILES['ufile']['size'][2];
                    $filesize3=$HTTP_POST_FILES['ufile']['size'][3];
                    $filesize3=$HTTP_POST_FILES['ufile']['size'][4];
                    
                    if($filesize1 && $filesize2 && $filesize3 && $filesize4 && $filesize5 == 0) {
                    	echo "<br>ERROR.....<br>";                    
                        if($filesize1==0) {echo "There was something wrong with the first file &nbsp;";}
                        if($filesize2==0) {echo "and the second file &nbsp;";}
                        if($filesize3==0) {echo "and the third file &nbsp;";}
                        if($filesize4==0) {echo "and the fourth file &nbsp;";}
                        if($filesize5==0) {echo "and the fifth file &nbsp;";}
                    	}
                    
                    else {
                        echo "<h2>Thank you! We have recieved the images listed below.</h2>";
                        echo "<p>Pending a review, we will post this on the photos page.<br><span style='font-size:.85em;'><em>Please note that we may not include every image sent to us.</em></span></p><br><br>";
                    	}
                    
                    echo "<div class='images'>";
                    
                    if($filesize1!=0) {
                    echo "<p>";
                    echo "<b>".$HTTP_POST_FILES['ufile']['name'][0]."</b><br>";
                    echo "<img src=\"$path1\">";
                    echo "</p>";
                    }
                    
                    if($filesize2!=0) {
                    echo "<p>";
                    echo "<b>".$HTTP_POST_FILES['ufile']['name'][1]."</b><br>";
                    echo "<img src=\"$path2\">";
                    echo "</p>";
                    }
                    
                    if($filesize3!=0) {
                    echo "<p>";
                    echo "<b>".$HTTP_POST_FILES['ufile']['name'][2]."</b><br>";
                    echo "<img src=\"$path3\">";
                    echo "</p>";
                    }
                    
                    if($filesize4!=0) {
                    echo "<p>";
                    echo "<b>".$HTTP_POST_FILES['ufile']['name'][3]."</b><br>";
                    echo "<img src=\"$path3\">";
                    echo "</p>";
                    }
                    
                    if($filesize5!=0) {
                    echo "<p>";
                    echo "<b>".$HTTP_POST_FILES['ufile']['name'][4]."</b><br>";
                    echo "<img src=\"$path3\">";
                    echo "</p>";
                    }
                    
                    echo "</div>";
                    
                  ?>
    But the validation isn't perfect (in case of upload errors, etc)...

    So what I'd like to do is implement something like:

    Code:
    if($path1.length > 0 && $filesize1 == 0) {
      echo "<p>The first file had an error.</p>";
    }
    else {
      echo "<p>";
      echo "<b>".$HTTP_POST_FILES['ufile']['name'][0]."</b><br>";
      echo "<img src=\"$path1\">";
      echo "</p>";
    }
    Does that make sense?

    I just don't think that $path1.length is correct syntax. How can I check if it's empty or not (how can i see if that input box has text in it?)

    Thanks so much for any help.

  2. #2
    Join Date
    Mar 2008
    Posts
    55
    PHP doesn't actually know what was in that box, all it receives in the header are the data, the name, and the type.

    If they didn't upload anything, tmp_name will be empty:

    PHP Code:
    $HTTP_POST_FILES['ufile']['tmp_name'][0] == '' 
    Or, just try it yourself. Make a page that print_r("$_FILES") and then submit the form without a file. See what you get.

  3. #3
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779
    Oh - gotcha... I'll play around with this, I think I'll be able to get it working.

    Thanks a lot, Carth.

  4. #4
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779
    Hey, that worked perfectly - thanks a lot!

    If you're willing to help a little more...
    I've been playing around with this - and I'm very new to PHP... so this coding is a little ridiculous. I'm sure that it could be a lot more simple, but I'm bad at this - I'm surprised it's working out as well as it is.

    The code below gives all the correct error messages (file is not an image, file size is zero, or the field was left blank, so it skips over that image - if that makes sense).

    Anyway, all of this is working except for the fact that the file still uploads even if it's not an image... I want only images to be allowed to copy over.

    Here's the code, and my solution (that's not working):

    PHP Code:
    <?php
                    $path1
    "uploads/".$HTTP_POST_FILES['ufile']['name'][0];
                    
    $path2"uploads/".$HTTP_POST_FILES['ufile']['name'][1];
                    
    $path3"uploads/".$HTTP_POST_FILES['ufile']['name'][2];
                    
    $path4"uploads/".$HTTP_POST_FILES['ufile']['name'][3];
                    
    $path5"uploads/".$HTTP_POST_FILES['ufile']['name'][4];
                    
                    
    $isimage1=getimagesize($path1);
                    
    $isimage2=getimagesize($path2);
                    
    $isimage3=getimagesize($path3);
                    
    $isimage4=getimagesize($path4);
                    
    $isimage5=getimagesize($path5);
                    
                    
    copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
                    
    copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
                    
    copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
                    
    copy($HTTP_POST_FILES['ufile']['tmp_name'][3], $path4);
                    
    copy($HTTP_POST_FILES['ufile']['tmp_name'][4], $path5);
                    
                    
    $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
                    
    $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
                    
    $filesize3=$HTTP_POST_FILES['ufile']['size'][2];
                    
    $filesize4=$HTTP_POST_FILES['ufile']['size'][3];
                    
    $filesize5=$HTTP_POST_FILES['ufile']['size'][4];
                    
                    if (
    $filesize1 $filesize2 $filesize3 $filesize4 $filesize5 == 0) {
                        echo 
    "Error: no files attached";
                        exit();
                    }
                    else {
                        echo 
    "<h2>Thank you! We have recieved the images listed below.</h2>";
                        echo 
    "<p>Pending a review, we will post this on the photos page.<br><span style='font-size:.85em;'><em>Please note that we may not include every image sent to us.</em></span></p><br>";
                        
                        if (
    $HTTP_POST_FILES['ufile']['tmp_name'][0] != '') {
                            if (
    $isimage1 && $filesize1 0) {
                                echo 
    "<p>";
                                echo 
    "<b>".$HTTP_POST_FILES['ufile']['name'][0]."</b><br>";
                                echo 
    "<img src=\"$path1\">";
                                echo 
    "</p>";
                            }
                            else {
                                if (!
    $filesize1) {echo "There was an error with the first image. Please try it again.";}
                                else {echo 
    "Error with the first upload: only images are allowed";}
                            }
                        }
                        
                        if (
    $HTTP_POST_FILES['ufile']['tmp_name'][1] != '') {
                            if (
    $isimage2 && $filesize2 0) {
                                echo 
    "<p>";
                                echo 
    "<b>".$HTTP_POST_FILES['ufile']['name'][1]."</b><br>";
                                echo 
    "<img src=\"$path2\">";
                                echo 
    "</p>";
                            }
                            else {
                                if (!
    $filesize2) {echo "There was an error with the second image. Please try it again.";}
                                else {echo 
    "Error with the second upload: only images are allowed";}
                            }
                        }
                        
                        if (
    $HTTP_POST_FILES['ufile']['tmp_name'][2] != '') {
                            if (
    $isimage3 && $filesize3 0) {
                                echo 
    "<p>";
                                echo 
    "<b>".$HTTP_POST_FILES['ufile']['name'][2]."</b><br>";
                                echo 
    "<img src=\"$path3\">";
                                echo 
    "</p>";
                            }
                            else {
                                if (!
    $filesize3) {echo "There was an error with the third image. Please try it again.";}
                                else {echo 
    "Error with the third upload: only images are allowed";}
                            }
                        }
                        
                        if (
    $HTTP_POST_FILES['ufile']['tmp_name'][3] != '') {
                            if (
    $isimage4 && $filesize4 0) {
                                echo 
    "<p>";
                                echo 
    "<b>".$HTTP_POST_FILES['ufile']['name'][3]."</b><br>";
                                echo 
    "<img src=\"$path4\">";
                                echo 
    "</p>";
                            }
                            else {
                                if (!
    $filesize4) {echo "There was an error with the fourth image. Please try it again.";}
                                else {echo 
    "Error with the fourth upload: only images are allowed";}
                            }
                        }
                        
                        if (
    $HTTP_POST_FILES['ufile']['tmp_name'][4] != '') {
                            if (
    $isimage5 && $filesize5 0) {
                                echo 
    "<p>";
                                echo 
    "<b>".$HTTP_POST_FILES['ufile']['name'][4]."</b><br>";
                                echo 
    "<img src=\"$path5\">";
                                echo 
    "</p>";
                            }
                            else {
                                if (!
    $filesize5) {echo "There was an error with the fifth image. Please try it again.";}
                                else {echo 
    "Error with the fifth upload: only images are allowed";}
                            }
                        }

                    }
                  
    ?>
    Sorry - the coding is a little crazy (and probably really stupid)

    Anyway, I thought that doing this would work:

    if ($isimage1) {copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);}
    if ($isimage2) {copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);}
    if ($isimage3) {copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);}
    if ($isimage4) {copy($HTTP_POST_FILES['ufile']['tmp_name'][3], $path4);}
    if ($isimage5) {copy($HTTP_POST_FILES['ufile']['tmp_name'][4], $path5);}

    But it only produces errors... why would this not work? (I know that again, it's probably a dumb question, but I'm trying to learn)

    Thanks again for any help. I really appreciate it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles