SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Form Problems

  1. #1
    Non-Member MrLister's Avatar
    Join Date
    Feb 2001
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Problems

    I need a form to be submitted to screenshots.php?gameid=1
    PHP Code:
    <?php
    require("./global.php");


    if (
    $gameid) {

     
    $screenshots mysql_query("SELECT review_name, review_id
                           FROM reviews_pc
                           WHERE review_id=
    $gameid");
      list(
    $review_name$review_id) = mysql_fetch_row($screenshots);
      
    $my_max_file_size     "54000"# in bytes
    $image_max_width    "30000";
    $image_max_height    "30000";
    $the_path            "C:\\Program Files\\Apache Group\\Apache\\htdocs\\upload\\";
    echo(
    "$the_path<br>");
    echo(
    "$review_id");
    $registered_types = array(
                        
    "image/gif"                         => ".gif, .jpeg"
                        
    );

    $allowed_types = array("image/gif");

    # --

    function form($error=false) {

    global 
    $PHP_SELF,$my_max_file_size;

        if (
    $error) print $error "<br><br>";
        
        print 
    "\n<form ENCTYPE=\"multipart/form-data\"  action=\"screenshots.php\" method=\"post\">";
        print 
    "<input type=\"hidden\" name=\"gameid\" value=\"1\">";
        print 
    "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" $my_max_file_size "\">";
        print 
    "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
        print 
    "\n<P>Upload a file";
        print 
    "\n<BR>NOTE: Max file size is " . ($my_max_file_size 1024) . "KB";
         print 
    "\n<br><INPUT NAME=\"the_file\" TYPE=\"file\" SIZE=\"35\"><br>";
        print 
    "\n<input type=\"submit\" Value=\"Upload\">";
        print 
    "\n</form>";

    # END form

    # --

    if (!ereg("^4",phpversion())) {
        function 
    in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
            
    for ($i=0$i count($haystack); $i++) {
                if (
    $haystack[$i] == $needle) {    
                    return 
    true;
                }
            }
        }
    }

    # --

    function validate_upload($the_file) {

    global 
    $my_max_file_size$image_max_width$image_max_height,$allowed_types,$the_file_type,$registered_types;
        
        
    $start_error "\n<b>Error:</b>\n<ul>";
        
        if (
    $the_file == "none") { # do we even have a file?
        
            
    $error .= "\n<li>You did not upload anything!</li>";
        
        } else { 
    # check if we are allowed to upload this file_type
            
            
    if (!in_array($the_file_type,$allowed_types)) {
                
    $error .= "\n<li>The file that you uploaded was of a type that is not allowed, you are only 
                            allowed to upload files of the type:\n<ul>"
    ;
                while (
    $type current($allowed_types)) {
                    
    $error .= "\n<li>" $registered_types[$type] . " (" $type ")</li>";
                    
    next($allowed_types);
                }
                
    $error .= "\n</ul>";
            }
        
            if (
    ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {
            
                
    $size GetImageSize($the_file);
                list(
    $foo,$width,$bar,$height) = explode("\"",$size[3]);
        
                if (
    $width $image_max_width) {
                    
    $error .= "\n<li>Your image should be no wider than " $image_max_width " Pixels</li>";
                }
                
                if (
    $height $image_max_height) {
                    
    $error .= "\n<li>Your image should be no higher than " $image_max_height " Pixels</li>";
                }
            
            }
            
            if (
    $error) {
                
    $error $start_error $error "\n</ul>";
                return 
    $error;
            } else {
                return 
    false;
            }
        }
    # END validate_upload

    # --


    function list_files() {

    global 
    $the_path;
        
        
    $handle dir($the_path);
        print 
    "\n<b>Uploaded files:</b><br>";
        while (
    $file $handle->read()) {
            if ((
    $file != ".") && ($file != "..")) {
                print 
    "\n" $file "<br>";
           }
        }
        print 
    "<hr>";
    }

    # --

    function upload($the_file) {

    global 
    $the_path,$the_file_name;
        
        
    $error validate_upload($the_file);
        if (
    $error) {
            
    form($error);
        } else { 
    # cool, we can continue
            
    if (!@copy($the_file$the_path "/" $the_file_name)) {
                
    form("\n<b>Something barfed, check the path to and the permissions for the upload directory</b>");
            } else {
                
    list_files();
                
    form();
            }
        }
    # END upload

    # --

    ############ Start page

    print "<html>\n<head>\n<title>Upload example</title>\n</head>\n<body>";

    switch(
    $task) {
        case 
    'upload':
            
    upload($the_file);
        break;
        default:
            
    form();
    }

    print 
    "\n</body>\n</html>";
    }
    ?>
    The error that i get is the one defined in the code. the one that says "Something barfed, check the path to and the permissions for the upload directory" but the directory and permissions are fine. without the hidden field and the if ($gameid) it works perfectly. where did i go wrong?

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your problem may be that you can't pass $the_file as a function parameter, you'll need to use global $the_file_size and $the_file_type or you can globalize $HTTP_POST_FILES and access the elements like $HTTP_POST_FILES['the_file']['size'] and $HTTP_POST_FILES['the_file']['type']
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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