Page 1 of 1

uploading files

Posted: Fri Jun 06, 2008 8:27 am
by highway81
hello,

i am making a simple cms which allows admin users to upload images to their database. i have used this particular file before but upon trying to implement it earlier i am getting the following error:

File Upload Failed, Debug Info: Array ( )

the code (which is quite weighty) is as follows:


<?php
if(isset($_POST['submit'])){
if(is_uploaded_file($_FILES['logo']['tmp_name'])){
if ($_FILES['logo']['type'] == "image/gif" ||
$_FILES['logo']['type'] == "image/jpeg" ||
$_FILES['logo']['type'] == "image/pjpeg"){
if ($_FILES['logo']['size'] < 1024 * 100){
list($width, $height, $type, $attr) = getimagesize($_FILES['logo']['tmp_name']);
if ($width < 200 || $height < 200){

//once all checks have been passed we make a directory using the followig format YYYYMMDD
//THE RELATIVE PATH WE USE FOR THIS SCRIPT
$dir_name = "../assets/images/portfolio_main/" . date("jmY");
//THE ABS PATH WE ALSO STORE FOR USING IN THE DB AND IMG TAGS
$img_path = "http://highway81design.com/10ant/assets ... folio_main" . date("jmY");
//we have to check if the directory already exists otherwise the script will error if it does
if(!is_dir($dir_name)){
mkdir($dir_name) or die("could not create directory " . $dir_name);
}

//so now we have the directory created, we must check for the file of the same name inside, and if it exists, we must add a number to our filename
//so we set an increment counter, and remember the original name of the file (otherwise we get 3_2_1_filename.jpg)
$i = 1;
$orig_filename = $_FILES['logo']['name'];
//check for the file in a loop
while(file_exists($dir_name . "/" . $_FILES['logo']['name'])){
//chnage the actual name of the file
//(here we add a number to the beginning, ideally we would break the filename apart and add the number at the end before the .extension, but i am trying to keep things simple here ...for now smile.gif
$_FILES['logo']['name'] = $i . "_" . $orig_filename;
//increment the counter just in case we have to come back through the loop again.
$i++;
}

//bingo, when the file no longer exists, we move the file, and give it the new name
move_uploaded_file($_FILES['logo']['tmp_name'], $dir_name . "/" . $_FILES['logo']['name']);
//$feedback = "File upload successful. The file has been saved as: " . $img_path . "/" . $_FILES['product_image']['name'];
$image_loc = $img_path . "/" . $_FILES['logo']['name'];

}else{
$image_feedback = "LOGO IMAGE SIZE ERROR: You have exceeded the maximum logo image dimensions of 200 x 200.";
}
}else{
$image_feedback = "FILESIZE ERROR: You have exceeded the maximum logo image file size of 100kb.";
}
}else{
$image_feedback = "FILETYPE ERROR: Wrong file type.";
}
}else{
echo "File Upload Failed, Debug Info: <br/>";
print_r($_FILES);
}
}
echo $image_feedback;
?>


any help would be greatly appreciated

thanks

Re: uploading files

Posted: Fri Jun 06, 2008 8:49 am
by vargadanis
Please put your code into [ php ] or [ code ] tags. It is very hard to read it like this.

Is your form type multipart/form-data?

Re: uploading files

Posted: Fri Jun 06, 2008 8:51 am
by highway81
apologies:

Code: Select all

 
 
<?php
if(isset($_POST['submit'])){
    if(is_uploaded_file($_FILES['image']['tmp_name'])){
        if ($_FILES['image']['type'] == "image/gif" || 
            $_FILES['image']['type'] == "image/jpeg" ||
            $_FILES['image']['type'] == "image/pjpeg"){
                if ($_FILES['logo']['size'] < 1024 * 100){
                    list($width, $height, $type, $attr) = getimagesize($_FILES['image']['tmp_name']);
                    if ($width < 551 || $height < 284){
                        
                        //once all checks have been passed we make a directory using the followig format YYYYMMDD
                        //THE RELATIVE PATH WE USE FOR THIS SCRIPT
                        $dir_name = "../assets/images/portfolio_main/" . date("jmY");
                        //THE ABS PATH WE ALSO STORE FOR USING IN THE DB AND IMG TAGS
                        $img_path = "http://highway81design.com/10ant/assets/images/portfolio_main" . date("jmY");
                        //we have to check if the directory already exists otherwise the script will error if it does
                        if(!is_dir($dir_name)){
                            mkdir($dir_name) or die("could not create directory " . $dir_name);
                        }
                        
                        //so now we have the directory created, we must check for the file of the same name inside, and if it exists, we must add a number to our filename
                        //so we set an increment counter, and remember the original name of the file (otherwise we get 3_2_1_filename.jpg)
                        $abc = 1;
                        $orig_filename = $_FILES['image']['name'];
                        //check for the file in a loop
                        while(file_exists($dir_name . "/" . $_FILES['image']['name'])){
                            //chnage the actual name of the file 
                            //(here we add a number to the beginning, ideally we would break the filename apart and add the number at the end before the .extension, but i am trying to keep things simple here ...for now :)
                            $_FILES['image']['name'] = $abc . "_" . $orig_filename;
                            //increment the counter just in case we have to come back through the loop again.
                            $abc++;
                        }
                        
                        //bingo, when the file no longer exists, we move the file, and give it the new name
                        move_uploaded_file($_FILES['image']['tmp_name'], $dir_name . "/" . $_FILES['image']['name']);
                        //$feedback =  "File upload successful. The file has been saved as: " . $img_path . "/" . $_FILES['product_image']['name'];
                        $image_loc = $img_path . "/" . $_FILES['image']['name'];
                        
                    }else{
                    $image_feedback = "IMAGE SIZE ERROR: You have exceeded the maximum property image dimensions of 550 x 283.";
                    }   
                }else{
                $image_feedback = "FILESIZE ERROR: You have exceeded the maximum property image file size of 100kb.";
                }       
        }else{
        $image_feedback = "FILETYPE ERROR: Wrong property image file type.";
        }   
    }else{
    echo "File Upload Failed, Debug Info: <br/>";
    print_r($_FILES);
    }   
} 
echo $image_feedback;
?>
 
 

Re: uploading files

Posted: Fri Jun 06, 2008 10:33 am
by dbemowsk
Your form tag should be in this form

Code: Select all

<form name="your_form_name" action="your_script.php" method="POST" enctype="multipart/form-data">
Note the attribute 'enctype="multipart/form-data">'. Your form tag MUST contain this for a file upload.