Jump to content

uploading images to a server


kikilahooch

Recommended Posts

I'm trying to upload images to a server using code that was given in a tutorial on this site: [url=http://www.phpfreaks.com/tutorials/85/0.php]http://www.phpfreaks.com/tutorials/85/0.php[/url]

I'm very new to php so I am not sure if followed it correctly. I basically copied and pasted it all and modified the path where images are stored and the log directory and saved it as Upload_Class.php

[code] <?php


class Upload_Files {

    var $temp_file_name;
    var $file_name;
    var $upload_dir;
    var $upload_log_dir;
    var $max_file_size;
    var $banned_array;
    var $ext_array;



function validate_extension() {
    //SECTION #1
    $file_name = trim($this->file_name);
    $extension = strtolower(strrchr($file_name,"."));
    $ext_array = $this->ext_array;
    $ext_count = count($ext_array);

    //SECTION #2
    if (!$file_name) {
        return false;
    } else {
        if (!$ext_array) {
            return true;
        } else {
            foreach ($ext_array as $value) {
                $first_char = substr($value,0,1);
                    if ($first_char <> ".") {
                        $extensions[] = ".".strtolower($value);
                    } else {
                        $extensions[] = strtolower($value);
                    }
            }

            //SECTION #3
            foreach ($extensions as $value) {
                if ($value == $extension) {
                    $valid_extension = "TRUE";
                }               
            }

            //SECTION #4
            if ($valid_extension) {
                return true;
            } else {
                return false;
            }
        }
    }
}




function validate_size() {
    $temp_file_name = trim($this->temp_file_name);
    $max_file_size = trim($this->max_file_size);

    if (!$temp_file_name) {
        $size = filesize($temp_file_name);
            if ($size > $max_file_size) {
                return false;                                                       
            } else {
                return true;
            }
    } else {
        return false;
    }   
}

function existing_file() {
    $file_name = trim($this->file_name);
    $upload_dir = $this->get_upload_directory();

    if ($upload_dir == "ERROR") {
        return true;
    } else {
        $file = $upload_dir . $file_name;
        if (file_exists($file)) {
            return true;
        } else {
            return false;
        }
    }   
}

function get_file_size() {
    //SECTION #1
    $temp_file_name = trim($this->temp_file_name);
    $kb = 1024;
    $mb = 1024 * $kb;
    $gb = 1024 * $mb;
    $tb = 1024 * $gb;

        //SECTION #2
        if ($temp_file_name) {
            $size = filesize($temp_file_name);
            if ($size < $kb) {
                $file_size = "$size Bytes";
            }
            elseif ($size < $mb) {
                $final = round($size/$kb,2);
                $file_size = "$final KB";
            }
            elseif ($size < $gb) {
                $final = round($size/$mb,2);
                $file_size = "$final MB";
            }
            elseif($size < $tb) {
                $final = round($size/$gb,2);
                $file_size = "$final GB";
            } else {
                $final = round($size/$tb,2);
                $file_size = "$final TB";
            }
        } else {
            $file_size = "ERROR: NO FILE PASSED TO get_file_size()";
        }
        return $file_size;
}


function get_max_size() {
    $max_file_size = trim($this->max_file_size);
    $kb = 1024;
    $mb = 1024 * $kb;
    $gb = 1024 * $mb;
    $tb = 1024 * $gb;

    if ($max_file_size) {
        if ($max_file_size < $kb) {
            $max_file_size = "max_file_size Bytes";
        }
        elseif ($max_file_size < $mb) {
            $final = round($max_file_size/$kb,2);
            $max_file_size = "$final KB";
        }
        elseif ($max_file_size < $gb) {
            $final = round($max_file_size/$mb,2);
            $max_file_size = "$final MB";
        }
        elseif($max_file_size < $tb) {
            $final = round($max_file_size/$gb,2);
                $max_file_size = "$final GB";
        } else {
            $final = round($max_file_size/$tb,2);
            $max_file_size = "$final TB";
        }
    } else {
        $max_file_size = "ERROR: NO SIZE PARAMETER PASSED TO  get_max_size()";
    }
        return $max_file_size;
}

function validate_user() {
    //SECTION #1
    $banned_array = $this->banned_array;
    $ip = trim($_SERVER['REMOTE_ADDR']);
    $cpu = gethostbyaddr($ip);
    $count = count($banned_array);

    //SECTION #2
    if ($count < 1) {
        return true;
    } else {
        foreach($banned_array as $key => $value) {
            if ($value == $ip ."-". $cpu) {
                return false;
            } else {
                return true;
            }
        }
    }
}


function get_upload_directory() {
    //SECTION #1
 
    $upload_dir = trim('$this->clothes/');
    //SECTION #2
    if ($upload_dir) {
        $ud_len = strlen($upload_dir);
        $last_slash = substr($upload_dir,$ud_len-1,1);
            if ($last_slash <> "/") {
                $upload_dir = $upload_dir."/";

            } else {
                    $upload_dir = $upload_dir;
            }

        //SECTION #3
        $handle = @opendir($upload_dir);
            if ($handle) {
                $upload_dir = $upload_dir;
                closedir($handle);
            } else {
                $upload_dir = "ERROR";
            }
    } else {
        $upload_dir = "ERROR";
    }
    return $upload_dir;
}


function get_upload_log_directory() {
      $upload_dir = trim('$this->log_dir/');
    //$upload_log_dir = trim($this->log_dir/);
    if ($upload_log_dir) {
        $ud_len = strlen($upload_log_dir);
        $last_slash = substr($upload_log_dir,$ud_len-1,1);
            if ($last_slash <> "/") {
                $upload_log_dir = $upload_log_dir."/";
            } else {
                $upload_log_dir = $upload_log_dir;
            }
            $handle = @opendir($upload_log_dir);
                if ($handle) {
                    $upload_log_dir = $upload_log_dir;
                    closedir($handle);
                } else {
                    $upload_log_dir = "ERROR";
                }
    } else {
        $upload_log_dir = "ERROR";
    }
    return $upload_log_dir;
}


function upload_file_no_validation() {
    //SECTION #1
    $temp_file_name = trim($this->temp_file_name);
    $file_name = trim(strtolower($this->file_name));
    $upload_dir = $this->get_upload_directory();
    $upload_log_dir = $this->get_upload_log_directory();
    $file_size = $this->get_file_size();
    $ip = trim($_SERVER['REMOTE_ADDR']);
    $cpu = gethostbyaddr($ip);
    $m = date("m");
    $d = date("d");
    $y = date("Y");
    $date = date("m/d/Y");
    $time = date("h:i:s A");

    //SECTION #2
    if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR")) {
        return false;
    } else {
        if (is_uploaded_file($temp_file_name)) {
            if (move_uploaded_file($temp_file_name,$upload_dir . $file_name)) {
                $log = $upload_log_dir.$y."_".$m."_".$d.".txt";
                $fp = fopen($log,"a+");
                fwrite($fp,"
$ip-$cpu | $file_name | $file_size | $date | $time");
                fclose($fp);
                return true;
            } else {
                return false;   
            }
        } else {
            return false;
        }
    }
}


function upload_file_with_validation() {
    //SECTION #1
    $temp_file_name = trim($this->temp_file_name);
    $file_name = trim(strtolower($this->file_name));
    $upload_dir = $this->get_upload_directory();
    $upload_log_dir = $this->get_upload_log_directory();
    $file_size = $this->get_file_size();
    $ip = trim($_SERVER['REMOTE_ADDR']);
    $cpu = gethostbyaddr($ip);
    $m = date("m");
    $d = date("d");
    $y = date("Y");
    $date = date("m/d/Y");
    $time = date("h:i:s A");
    $existing_file = $this->existing_file();    //<-Add On
    $valid_user = $this->validate_user();        //<-Add On
    $valid_size = $this->validate_size();        //<-Add On
    $valid_ext = $this->validate_extension();    //<-Add On

    //SECTION #2
    if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR")) {
        return false;
    }
    elseif ((((!$valid_user) OR (!$valid_size) OR (!$valid_ext) OR ($existing_file)))) {
        return false;
    } else {
        if (is_uploaded_file($temp_file_name)) {
            if (move_uploaded_file($temp_file_name,$upload_dir . $file_name)) {
                $log = $upload_log_dir.$y."_".$m."_".$d.".txt";
                $fp = fopen($log,"a+");
                fwrite($fp,"
$ip-$cpu | $file_name | $file_size | $date | $time");
                fclose($fp);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}

}
?>
[/code]

Then I called the class using a using a file called test_upload.php

[code]

<?php
//SECTION #1

include("Upload_Class.php");

$upload_class = new Upload_File;  //line 7
$upload_class->temp_file_name = trim($_FILES['upload']['tmp_name']);
$upload_class->file_name = trim(strtolower($_FILES['upload']['name']));
$upload_class->upload_dir = "uploads/";
$upload_class->upload_log_dir = "uploads/upload_logs/";
$upload_class->max_file_size = 524288;
$upload_class->banned_array = array("");
$upload_class->ext_array = array(".jpg",".gif",".jpeg",".png");

//SECTION #2

$upload_file = $upload_class->upload_file_with_no_validation();


?> [/code]

When I try to upload an image then I get the error message:

Fatal error: Cannot instantiate non-existent class: upload_file in /home/c/ciaracousins/public_html/test_upload.php on line 7

Would really appreciate if someone could point out where I am going wrong
Link to comment
Share on other sites

Thanks! realised what I had done, had misspelt one of the class names. Now though when I go to upload it just brings me from my fileupload.html page, to a blank page (test_upload.php), it doesnt even give me any errors and the file is not uploading. I changed the code for calling the class to this then to see if it would tell me why its not uploading but this too just brings me to a blank page (test_upload.php) and doesnt work.

[code]
<?php

include("Upload_Class.php");
$upload_class = new Upload_Files;
$upload_class->temp_file_name = trim($_FILES['upload']['tmp_name']);
$upload_class->file_name = trim(strtolower($_FILES['upload']['name']));
$upload_class->upload_dir = "clothes/";
$upload_class->upload_log_dir = "log_dir/";
$upload_class->max_file_size = 5242880;
$upload_class->banned_array = array("");
$upload_class->ext_array = array(".zip",".rar",".ace",".tar");

$valid_ext = $upload_class->validate_extension();
$valid_size = $upload_class->validate_size();
$valid_user = $upload_class->validate_user();
$max_size = $upload_class->get_max_size();
$file_size = $upload_class->get_file_size();
$file_exists = $upload_class->existing_file();

   if (!$valid_ext) {
       $result = "The file extension is invalid, please try again!";
   }
   elseif (!$valid_size) {
       $result = "The file size is invalid, please try again! The maximum file size is: $max_size and your file was: $file_size";
   }
   elseif (!$valid_user) {
       $result = "You have been banned from uploading to this server.";
   }
   elseif ($file_exists) {
       $result = "This file already exists on the server, please try again.";
   } else {
       $upload_file = $upload_class->upload_file_with_validation();
       if (!$upload_file) {
           $result = "Your file could not be uploaded!";
       } else {
           $result = "Your file has been successfully uploaded to the server.";
       }
   }
?> [/code]

Any idea why its doing this??

Here is the code to my fileupload.html page as well if it helps

[code]<html>
<head>
<title>A simple file upload form</title>
</head>
<body>
<form action="test_upload.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200">
<p><strong>File to Upload:</strong> <input type="file" name="fileupload"></p>
<p><input type="submit" value="upload!"></p>
</form>
</body>
</html>
[/code]
Link to comment
Share on other sites

Thanks ToonMariner, I changed the line to

<p><strong>File to Upload:</strong> <input type="file" name="upload"></p>

Its still not working though, It keeps going to a blank test_upload.php page. It goes straight to the page as well, so its not even trying to upload anything. Any other advice??
Link to comment
Share on other sites

Thanks onlyican! Now it is telling me why it wont upload. It keeps saying :

The file size is invalid, please try again! The maximum file size is: 5 MB and your file was: 1.35 KB

Can you point out where I have gone wrong with this?? Heres the validate size code

[code]
function validate_size() {
    $temp_file_name = trim($this->temp_file_name);
    $max_file_size = trim($this->max_file_size);

    if (!$temp_file_name) {
        $size = filesize($temp_file_name);
            if ($size > $max_file_size) {
                return false;                                                       
            } else {
                return true;
            }
    } else {
        return false;
    }   
} [/code]

I'm really new to this and got the code from a website so sorry if I'm missing really obvious stuff here!
Link to comment
Share on other sites

Thanks! Ok now it is saying "This file already exists on the server, please try again" but the file is new and is not on the server. I removed all code then that checks to see if the file already exists because its not really important in my case but then it just says:

"Your file could not be uploaded!"

Can you see anything that is stopping it from uploading??

Link to comment
Share on other sites

ok ok
How do you upload the files to your website?
Right click on the folder where the uploaded files go
Choose Properties / File Attributes / Settings (Something)
and you should be able to change the Attributes, the CHMOD
Try 755, if it fails, try 777, if it fails, let us know
Link to comment
Share on other sites

Ok I did that, but I am using a hosting site and when I choose properties it just brings up one page (common), there are no tabs to choose file attributes, but on that page it says "permission", here it has:

owner:  R  W  X  Set UID  //with check boxes beside each of them, all the R,W,X's are checked                            except 2 of the W's, when I put 0777 these were also checked
Group:  R  W  X  Set GID  // and the Set UID,GID and sticky bit are unchecked
Others: R  W  X  Sticky bit

Octal: 0755

I tried changing the octal to 0777 if that is the same as what you are talking about but there was no change
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.