Page 2 of 2

Re: FILES upload

Posted: Fri Feb 19, 2010 3:07 am
by Darhazer

Code: Select all

if ($_FILES["file"]["error"] > 0)
DO not use numbers. Use the constants instead
Additionally, you have this check twise in your code
I would recommend you to use absolute paths
instead of:

Code: Select all

'uploads/' . $_FILES['file']['name']
Use:

Code: Select all

dirname(__FILE__) . '/uploads' . basename($_FILES['file']['name']);
Note: basename is there for security reasons ;)

Re: FILES upload

Posted: Mon Mar 01, 2010 12:20 pm
by lubber123
I places an htaccess file in the root to define sizes. I have been trying to implement your code but have so many errors. I am not sure where it is getting the file name from - it is coming up with a u0v0.jpg file name - not sure where it comes from.

Here is my code - This thing has taken me so many hours. My old code worked for small images but would not upload large ones. My host says we can upload up to 1gig. So I am dead in the water.

Code: Select all

            if(isset($_REQUEST['btnuploadatt'])){
                    //  $img_base = base directory structure for thumbnail images
                    //  $w_dst = maximum width of thumbnail
                    //  $h_dst = maximum height of thumbnail
                    //  $n_img = new thumbnail name
                    //  $o_img = old thumbnail name
                    function convertPic($img_base, $w_dst, $h_dst, $n_img, $o_img)
                      {ini_set('memory_limit', '100M');   //  handle large images
                       unlink($img_base.$n_img);         //  remove old images if present
                       unlink($img_base.$o_img);
                       $new_img = $img_base.$n_img;
                        
                       $file_src = $img_base."img.jpg";  //  temporary safe image storage
                       unlink($file_src);
                       move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $file_src);
                                 
                       list($w_src, $h_src, $type) = getimagesize($file_src);     // create new dimensions, keeping aspect ratio
                       $ratio = $w_src/$h_src;
                       if ($w_dst/$h_dst > $ratio) {$w_dst = floor($h_dst*$ratio);} else {$h_dst = floor($w_dst/$ratio);}
                    
                       switch ($type)
                         {case 1:   //   gif -> jpg
                            $img_src = imagecreatefromgif($file_src);
                            break;
                          case 2:   //   jpeg -> jpg
                            $img_src = imagecreatefromjpeg($file_src);
                            break;
                          case 3:  //   png -> jpg
                            $img_src = imagecreatefrompng($file_src);
                            break;
                         }
                       $img_dst = imagecreatetruecolor($w_dst, $h_dst);  //  resample
                      
                       imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst, $h_dst, $w_src, $h_src);
                       imagejpeg($img_dst, $new_img);    //  save new image
                    
                       unlink($file_src);  //  clean up image storage
                       imagedestroy($img_src);       
                       imagedestroy($img_dst);
                      }
                    
                    $p_id = (Integer) $_POST[uid];
                    $ver = (Integer) $_POST[ver];
                    $delver = (Integer) $_POST[delver];
                    convertPic("attachments/", 150, 150, "u".$p_id."v".$ver.".jpg", "u".$p_id."v".$delver.".jpg");
            }   
 

Re: FILES upload

Posted: Tue Mar 02, 2010 11:28 am
by lubber123
Okay, I uploaded the htaccess file to my root directory. I added the code (below) but I have so many errors (below) I don't quite know where to start. It is providing as file called u0v0.jpg - not sure where that came from - I know the directory exists. Can you explain a little more?

Code: Select all

// Where the uploadedfile is going to be placed 
                
                    //  $img_base = base directory structure for thumbnail images
                    //  $w_dst = maximum width of thumbnail
                    //  $h_dst = maximum height of thumbnail
                    //  $n_img = new thumbnail name
                    //  $o_img = old thumbnail name
                    function convertPic($img_base, $w_dst, $h_dst, $n_img, $o_img)
                      {ini_set('memory_limit', '100M');   //  handle large images
                       unlink($img_base.$n_img);         //  remove old images if present
                       unlink($img_base.$o_img);
                       $new_img = $img_base.$n_img;
                        
                       $file_src = $img_base."img.jpg";  //  temporary safe image storage
                       unlink($file_src);
                       move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src);
                                 
                       list($w_src, $h_src, $type) = getimagesize($file_src);     // create new dimensions, keeping aspect ratio
                       $ratio = $w_src/$h_src;
                       if ($w_dst/$h_dst > $ratio) {$w_dst = floor($h_dst*$ratio);} else {$h_dst = floor($w_dst/$ratio);}
                    
                       switch ($type)
                         {case 1:   //   gif -> jpg
                            $img_src = imagecreatefromgif($file_src);
                            break;
                          case 2:   //   jpeg -> jpg
                            $img_src = imagecreatefromjpeg($file_src);
                            break;
                          case 3:  //   png -> jpg
                            $img_src = imagecreatefrompng($file_src);
                            break;
                         }
                       $img_dst = imagecreatetruecolor($w_dst, $h_dst);  //  resample
                      
                       imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst, $h_dst, $w_src, $h_src);
                       imagejpeg($img_dst, $new_img);    //  save new image
                    
                       unlink($file_src);  //  clean up image storage
                       imagedestroy($img_src);       
                       imagedestroy($img_dst);
                      }
                    
                    $p_id = (Integer) $_POST[uid];
                    $ver = (Integer) $_POST[ver];
                    $delver = (Integer) $_POST[delver];
                    convertPic("attachments/", 175, 230, "u".$p_id."v".$ver.".jpg", "u".$p_id."v".$delver.".jpg");
 

Errors: Warning: unlink(attachments/u0v0.jpg): No such file or directory in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 48

Warning: unlink(attachments/u0v0.jpg): No such file or directory in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 49

Warning: unlink(attachments/img.jpg): No such file or directory in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 53

Warning: getimagesize(attachments/img.jpg): failed to open stream: No such file or directory in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 56

Warning: Division by zero in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 57

Warning: imagecreatetruecolor(): Invalid image dimensions in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 71

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 73

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 74

Warning: unlink(attachments/img.jpg): No such file or directory in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 76

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 77

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/lfchur5/public_html/trainingcenter/mtcStudentPage.php on line 78

Re: FILES upload

Posted: Tue Mar 02, 2010 11:46 am
by brentwientjes
Hi lubber123

1. It looks like you copied my example code and wrapped it with a conditional statement. You included the function block within the conditional block. I do not think the php interpreter will like that. Place the function block at the base level of code and put the conditional block around the calling sequence of the function. The calling sequence can be anything you like. My version gets data from the client and then calls the routine.

$p_id = (Integer) $_POST[uid];
$ver = (Integer) $_POST[ver];
$delver = (Integer) $_POST[delver];
$new_img_name = "the new thumbnail to be created";
$old_img_name = "the old thumbnail to be discarded";
convertPic($img_base, $w_dst, $h_dst, $new_img_name, $old_img_name);

2. The new file name can be anything you want. In my case, I have a Flex client side program that supplies an index for the picture name and a picture version number to force the client browser to always show the updated image. You can create any name desired in its place. So, replace ["u".$p_id."v".$ver.".jpg"] with the name of your choice.

3. Be aware that each system may have a different name for the move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src); command. "Filedata" should be whatever the image upload structure name is. In my case, "Filedata" is the default value given by the Flex enviroment. It looks like you have a calling structure that has the name 'uploadedfile'.

Hope this helps. The function calls works great in my system so try the above to resolve. Let me know if you continue having problems.

Re: FILES upload

Posted: Tue Mar 02, 2010 12:06 pm
by brentwientjes
Hi again,

Your post arrived as I was composing my response above. The list of warnings and errors looks like

1. Function placement is wrong as I mentioned above
2. if that does not fix everything, then do you have GD lib as part of php environment. And it seems like the environment does not recognize your "attchment" directory.

If your original routine worked as expected with the exception of loading files larger than 2MB, then just add the .htaccess file size changes and the ini_set('memory_limit', '100M'); either with in line code or just add to the .htaccess file php_value memory_limit 100M. That is the actual secret sauce of how this all works with large images. Any of the image upload routines should then work with fixing the php envrionment varliables.

Let me know if there are any more questions.

Re: FILES upload

Posted: Wed Mar 03, 2010 10:03 am
by lubber123
Hi again,

Here is what I have. I have some original code that I have been slowly checking line by line with different files. This is what I have found: I have been attempting to upload 2 files cliffy-small.jpg and cliffy-medium.jpg. Small is 12.5kb and medium is 80.4kb. Small works its way through the code fine and uploads. The medium gets caught on the line

Code: Select all

 
if($result = getimagesize($_FILES['uploadedfile']['tmp_name'])){
 
and catches the code:

Code: Select all

 
}else{
echo "NO!"; exit;
}
I have the htaccess file you recommended in the root of the site - should I move it to the attachments directory?
I also put the ini_set('memory_limit', '100M'); line in my code: full code below.
It seems like it just does not want to send any medium or large sized files up. The directory is set to 777. My host says I can upload up to 1gig.

I have no more ideas and no more time to stay on this issue. Any last suggestions would be greatly appreciated.

Code: Select all

 
if(isset($_REQUEST['btnuploadatt'])){
                     
                     //**************************************************************************
    //**************************************************************************
    //  UPLOAD ATTACHMENTS FROM INSTRUCTORS
    //**************************************************************************
    //**************************************************************************
        // Where the uploadedfile is going to be placed 
                    ini_set('memory_limit', '100M');
                    $target_path = "attachments/";
                    
                
                /* Add the original uploadedfilename to our target path.  
                Result is "attachments/uploadedfilename.extension" */
                
                
                $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);   
                $last = substr($_FILES['uploadedfile']['name'], -4, 4); 
                
                if(($_FILES['uploadedfile']['error'] == UPLOAD_ERR_OK) || ($last == '.JPG') || ($last == '.jpg') || ($last == '.pdf') || ($last == '.doc') || ($last == '.xls')) {  
                    if($result = getimagesize($_FILES['uploadedfile']['tmp_name'])){
                        echo $_FILES['uploadedfile']['tmp_name'] . " " . $_FILES['uploadedfile']['name']; exit;
                                if($_FILES["uploadedfile"]["size"] > 3000000){  ///FILE SIZE
                                  $teachermessage = "Sorry, the file is too large.  Please reduce the size and re-upload."; 
                                    header('LOCATION: mtcStudentPage.php?class=' . trim($_SESSION['year']) . '&teaching=' . trim($_SESSION['class_id']) . '&teachermessage=' . $teachermessage);
                                    exit;
                                }
                                
                                //Changing the file name
                                $varString = implode(",", $_FILES['uploadedfile']);
                                $varFile = substr($varString, 0, (strpos($varString, ",")));        
                            
                                if(file_exists("attachments/" . $varFile)){
                                    unlink("attachments/" . $varFile);
                                }
                                
                                
                              if ($varString["error"] > 0){
                                echo "Return Code: " . $varString["error"] . "<br />";
                              }else{
                                //UPLOAD FILE
                                  if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],"attachments/" . $_FILES["uploadedfile"]["name"])){
                                        //INSERT INTO DB
                                        $queryattachment = "UPDATE mtc_classes SET attachments = '" . $varFile . "' WHERE id = " . $_SESSION['id'];
                                        $Resultattachment = mysql_query($queryattachment, $connection) or die("Error" . mysql_error());
                                        $_SESSION['attachments'] = $varFile;
                                        $teachermessage = "Thanks, the file was uploaded."; 
                                        header('LOCATION: mtcStudentPage.php?class=' . trim($_SESSION['year']) . '&teaching=' . trim($_SESSION['class_id']) . '&teachermessage=' . $teachermessage);
                                        exit;
                                  }else{
                                        $teachermessage = "Sorry, the file was not uploaded.  Please contact LFC IT."; 
                                        header('LOCATION: mtcStudentPage.php?class=' . trim($_SESSION['year']) . '&teaching=' . trim($_SESSION['class_id']) . '&teachermessage=' . $teachermessage);
                                        exit;
                                  }                             
                            }
                    }else{
                        echo "NO!"; exit;
                    }
                }else{
                        $teachermessage = "Sorry, this is the wrong file type."; 
                        header('LOCATION: mtcStudentPage.php?class=' . trim($_SESSION['year']) . '&teaching=' . trim($_SESSION['class_id']) . '&teachermessage=' . $teachermessage);
                        exit;
                }   
 

Re: FILES upload

Posted: Wed Mar 03, 2010 1:53 pm
by brentwientjes
getimagesize returns an array with 7 subvalues. I am not sure what an if test will do on an array. Go to the php documentation on getimagesize and look at the command details (google php documentation, insert getimagesize into search bar -> will show details).

It seems on line 22 you are just asking if the upload file is present. You just need to test is_uploaded_file($_FILES['uploadedfile']['tmp_name']) to make sure there is a file transferred.

Also, your code seems to mix 2 concepts together. You are allowing jpg, doc, pdf and xls files to be included but you are testing all types of files with the getimagesize command. The non jpg files have nothing to do with images and I suspect will bomb on the getimagesize command. I do not see where you do anything with the image after loading so I am not sure why you use the getimagesize command at all. It seems you should just treat all files alike and just ask if present, then test/tell user.

Hope this helps. Good luck.

Re: FILES upload

Posted: Wed Mar 03, 2010 8:20 pm
by lubber123
Well there is a section that adds the file name to a db table and things happen after that. But that is pointless if large files cannot be uploaded. Thanks for your time.

Re: FILES upload

Posted: Thu Mar 04, 2010 3:51 am
by VladSun
Darhazer wrote:The file type in the $_FILES array is provided by the browser, and it's not reliable. If you want to allow only images to be uploaded, use getimagesize() - it will return false if the file is not an image.
http://ha.ckers.org/blog/20070604/passi ... imagesize/
;)