Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15

Thread: Uploading files

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    Uploading files

    I am trying to upload images and the script is generally working. My problems lies when up loading anything over 7M the script appears to stops running and I get all sort of errors related to an "empty" file.

    I have my phpini set to- upload_max_filesize 12M - so shouldn't my file still up load?

    Here is my code:

    PHP Code:
    if ($one != NULL)  {
        
    $filename stripslashes($_FILES['photo_1']['name']);

        
    $extension getExtension($filename);
        
    $extension strtolower($extension);

        
    $image =$_FILES["photo_1"]["name"];
        
    $uploadedfile $_FILES['photo_1']['tmp_name'];

        if (
    $image) {     
            
    $filename stripslashes($_FILES['photo_1']['name']);
            
    $extension getExtension($filename);
            
    $extension strtolower($extension);
                    
            if ((
    $extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
                
    $errorMsg1="&errorMsg1=1";
            }
            
    // if there are not extension errors
            
    else    {
                
    // setting it 
                
    if($extension=="jpg" || $extension=="jpeg" )    {
                    
    $uploadedfile $_FILES['photo_1']['tmp_name'];
                    
    $src imagecreatefromjpeg($uploadedfile);
                }
                else if(
    $extension=="png")    {
                    
    $uploadedfile $_FILES['photo_1']['tmp_name'];
                    
    $src imagecreatefrompng($uploadedfile);
                }
                else if(
    $extension=="gif")    {
                    
    $uploadedfile $_FILES['photo_1']['tmp_name'];
                    
    $src imagecreatefromgif($uploadedfile);
                }

                
    // getting the width and height of the image
                
    list($width,$height)=getimagesize($uploadedfile);
                    
                if (
    $width $height) {
                    
    // changes width            
                    
    $newwidth=300;
                    
    $newheight=($height/$width)*$newwidth;
                    
    $tmp=imagecreatetruecolor($newwidth,$newheight);

                    
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                } 
    // END if ($width > $height)
                
    else {
                    
    // changes height                
                    
    $newHeight=300;
                    
    $newWidth=($width/$height)*$newHeight;
                    
    $tmp=imagecreatetruecolor($newWidth,$newHeight);
                    
    imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$width,$height);
                }

                
    $extension strrchr($_FILES['photo_1']['name'],'.');
                
    $extension strtolower($extension);

                
    // where it places the new images
                
    $photoNumber="_1";
                
    $finalName="$clientID$photoNumber";
                
    $save_path "uploads/";
                
    $target_path $save_path basename$_FILES['photo_1']['name']);
                
    $NewPhotoName $finalName;
                
    $withExt $NewPhotoName $extension;
                
    $filename $save_path $NewPhotoName $extension;

                if(
    move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename)) {
                    
    $stmt $mysqli->prepare("UPDATE photos SET photo_1=? WHERE userID=?");
                    
    $stmt->bind_param('ss',
                        
    $withExt,
                        
    $clientID);
                    
    $stmt->execute();
                } 
    // END if(move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename))

                // needed for the resizing
                
    imagejpeg($tmp,$filename,100);
                
    imagedestroy($src);
                
    imagedestroy($tmp);
            } 
    // END else
        
    // END if ($image)
            
        
    $qry "SELECT photo_1 FROM photos WHERE userID LIKE '$clientID'";
        
    $result $mysqli->query($qry);
                    
        while (
    $r=$result->fetch_assoc()) {
            
    $photo_1=$r['photo_1'];
        }
    // END else if ($one != NULL) 


    It works for smaller files and will work for the big ones if I take out everything but

    PHP Code:
    if ($one != NULL)  {
        
    // where it places the new images
            
    $photoNumber="_1";
            
    $finalName="$clientID$photoNumber";
            
    $save_path "uploads/";
            
    $target_path $save_path basename$_FILES['photo_1']['name']);
            
    $NewPhotoName $finalName;
            
    $withExt $NewPhotoName $extension;
            
    $filename $save_path $NewPhotoName $extension;
                
            if(
    move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename)) {
                
    $stmt $mysqli->prepare("UPDATE photos SET photo_1=? WHERE userID=?");
                
    $stmt->bind_param('ss',
                    
    $withExt,
                    
    $clientID);
                
    $stmt->execute();
            } 
    // END else if ($one != NULL)



    Could really use some help. It is most likely a simple issue that I cannot see

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Did you increase the post_max_size as well? This directive must be > upload_max_filesize if you want to be able to upload up to the specified upload filesize.

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I didn't but it is set at 33M. I read that it should be doulbe the upload_max_filesize


    Also, it takes that image and turns it black and small, 1.27 kb from a 7681kb file
    Last edited by harkly; 10-22-2012 at 08:58 PM.

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    maybe it's just timing out
    check this guide: http://www.radinks.com/upload/config.php

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by harkly View Post
    I didn't but it is set at 33M. I read that it should be doulbe the upload_max_filesize


    Also, it takes that image and turns it black and small, 1.27 kb from a 7681kb file
    What's in the associated 'error' offset for the $_FILES? The above looks to me that it's hitting condition 3, or partial file upload.

    Time could be of relevance as well, although typically I'd expect that you would lose out on most of the script handling with that. If the script fails, I'd expect that it will never move, unless it fails after the move (which is certainly possible as well). Still, it smells to me that its partial.

    BTW, you did restart your webserver software after changing the ini right? Or is it a local ini (if its local, it should work without restart assuming you are on a fcgi install).

  • #6
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    checked the possibily of timing out and I don't believe that is the issue.

    What's in the associated 'error' offset for the $_FILES? The above looks to me that it's hitting condition 3, or partial file upload.
    You went over my head with this question. Not sure what you mean by assocaisted 'error' offest for the $_FILES.

    I am working off of godaddy so not sure if it restarts, I know I had to wait a bit for the 12M to change from 8M
    Last edited by harkly; 10-22-2012 at 10:04 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What is in $_FILES['photo_1']['error']? You should always be checking this btw, only ever attempt a move if that results to UPLOAD_ERR_OK (or 0).

  • #8
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    The problem appears that the image uploading is not an actual image.

    So now I am testing that the uploads are images by checking the getimagesize. However that is not working for me either. Everything is coming back as "not an image"

    PHP Code:
        function getExtension($str) {
            
    $i strrpos($str,".");
            if (!
    $i) {
                return 
    "";
            }
            
    $l strlen($str) - $i;
            
    $ext substr($str,$i+1,$l);
            return 
    $ext;
        }

        if( isset(
    $_POST['submitPhoto'])) {
        
            
    $one getimagesize($_FILES['photo_1']['name']);

            if(empty(
    $one)) {// No Image?
                 
    echo "The uploaded file doesn't seem to be an image.";
            }
            else {
                echo 
    " an image";
            }
        }
    echo
    "
    <form enctype='multipart/form-data' action='' method='POST'>

        &nbsp;<input type='file' name='photo_1' class='zip'><br><br>
        
        <div id='button'><br>
            <input type='image' value='Upload File' src='img/button_update.gif' width='72' height='25'
                onmouseover=\"javascript:this.src='img/button_update2.gif';\" onmouseout=\"javascript:this.src='img/button_update.gif';\">
                <input type='hidden' name='submitPhoto' value='true'>
            </div>
    </form>"

    Error msg:
    Warning: getimagesize(ce1.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/content/01/6514501/html/singles/testImage.php on line 23
    The uploaded file doesn't seem to be an image.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You cannot pull a getimagesize on an uploaded file's name. You must pull it from the tmp_name.

  • #10
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I was attempting to follow this guys example


    PHP Code:
    $file $_FILES['image_file'];

    $file_name $file['name'];

    $error ''// Empty

    // Get File Extension (if any)

    $ext strtolower(substr(strrchr($file_name"."), 1));

    // Check for a correct extension. The image file hasn't an extension? Add one

       
    if($validation_type == 1)
       {
       
     
    $file_info getimagesize($_FILES['image_file']['tmp_name']); 

        { 
    So this is wrong as well?

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, that's correct. It uses the tmp_name: getimagesize($_FILES['image_file']['tmp_name']); you have used the name: getimagesize($_FILES['photo_1']['name']).

  • #12
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I believe I have it!

    I have a question on size.

    In the example data they have this line in the form
    Code:
    <input type="hidden" name="MAX_FILE_SIZE" value="768000" />
    If the image size if over the limit it gives the same error as using a non image. Is there a way to get a different error message when the file is to large? Not sure how that is deciphered.

    I was under the impress that the file had to be uploaded and then tested for size.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I guess this depends entirely on what you are checking for your error. If the files uploaded error message is UPLOAD_ERR_FORM_SIZE, then that indicates you've exceeded the max size allowed in the form. Since you do not have a file to work with (or potentially a partial file if obeyed by the client), there is no way to tell what it was. That said, UPLOAD_ERR_FORM_SIZE shouldn't be relied on either as its controlled from a client side. So yes, you still have to test the size after the upload; the MAX_FILE_SIZE is a client control. I don't know the communication between the use of this, but I'd presume that the webhost is instructed to "give up" on it at that point (the client developers may actually know if this is in any way, shape or form actually enforced). PHP wise, it simply issues that its exceeded, but I don't know if it gets the file completely, detects that its exceeded and abandons it, or what it does tbh.

    Beyond this, so long as UPLOAD_ERR_OK is valid, then you check to see what you have been given. There's no way until you've received the file to determine exactly what it was.

  • #14
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Not 100% sure where to place the UPLOAD_ERR_OK. I've place it after the upload, which makes sense to me but then...

    Can you tell me if I have used it correctly?

    PHP Code:
                    move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename);
                    
                if(
    $_FILES['photo_1']['error'] == UPLOAD_ERR_OK) {
                    
    $stmt $mysqli->prepare("UPDATE photos SET photo_1=? WHERE userID=?");
                    
    $stmt->bind_param('ss',
                        
    $withExt,
                        
    $clientID);
                    
    $stmt->execute();
                }
                else {
                    
    // error for when file didn't move
                


  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, you do absolutely nothing with the file until its been verified as uploaded successfully. You don't even bother checking the size, the name, the extension, not a single thing until it passes UPLOAD_ERR_OK.


  •  

    Posting Permissions

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