Uploaded images file permissions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Uploaded images file permissions

Post by shiznatix »

When I upload an image I am trying to resize it and do fun things with it. Well the upload works fine but it uploads it with permissions 411 which does not let me change the picture sizes and stuff. I have tried to chmod the tmp file AND the uploaded file right after I do the upload but to no avail. I know this has to be something with my code as I have been able to do this in the past. Here is my code:

Code: Select all

foreach ($_FILES['Pictures']['error'] as $key => $error)
        {
            if (0 === $error)
            {
                $IM->Set('fkListingId', $ListingId);

                $tmp = $_FILES['Pictures']['tmp_name'][$key];
                $name = $_FILES['Pictures']['name'][$key];

                while (file_exists($UploadDir.$name))
                {
                    $name = str_replace(' ', '', microtime());
                    $name = str_replace('.', '', $name);
                }

                $DBPath = GetUserId().'/';

                move_uploaded_file($tmp, $UploadDir.$name);

                if (false === chmod($UploadDir.$name, '0777'))
                {
                    die('unable to chmod file on line: '.__LINE__);
                }

                if ($key == $MakeThumb && true !== $MakeThumb)
                {
                    $ImageResize->DoResize($UploadDir.$name, $UploadDir.'thumb_'.$name, 100, 100);

                    $IM->Set('Path', $DBPath.'thumb_'.$name);
                    $IM->Set('isThumb', 1);
                    $IM->Save();
                }
                else if (false === $MakeThumb)
                {
                    $ImageResize->DoResize($UploadDir.$name, $UploadDir.'thumb_'.$name, 100, 100);
                    $MakeThumb = true;

                    $IM->Set('Path', $DBPath.'thumb_'.$name);
                    $IM->Set('isThumb', 1);
                    $Im->Save();
                }

                $ImageResize->DoResize($UploadDir.$name, $UploadDir.$name, 400, 300);

                $IM->Set('Path', $DBPath.$name);
                $IM->Set('isThumb', 0);
                $IM->Save();
            }
        }
and my DoResize function:

Code: Select all

public function DoResize($src, $out, $outX, $outY)
    {
        $dim = @getimagesize($src);

        if ($dim === FALSE)
        {
            trigger_error('Failed to read the source image', E_USER_ERROR);
        }

        list($srcX, $srcY, $srcType) = $dim;

        switch ($srcType)
        {
            case 1:
                $imgSrc = @imagecreatefromgif($src);
                break;
            case 2:
                $imgSrc = @imagecreatefromjpeg($src);
                break;
            case 3:
                $imgSrc = @imagecreatefrompng($src);
                break;
            default:
                trigger_error('Unsupported image type', E_USER_ERROR);
        }

        $imgOut = @imagecreatetruecolor($outX, $outY);

        if (false === $imgSrc || false === $imgOut)
        {
            trigger_error('Failed to create image', E_USER_ERROR);
        }

        if (false === @imagecopyresampled($imgOut, $imgSrc, 0, 0, 0, 0, $outX, $outY, $srcX, $srcY))
        {
            trigger_error('Failed to resize image', E_USER_ERROR);
        }

        switch ($srcType)
        {
            case 1:
                $saveres = @imagegif($imgOut,  $out);
                break;
            case 2:
                $saveres = @imagejpeg($imgOut, $out);
                break;
            case 3:
                $saveres = @imagepng($imgOut,  $out);
                break;
            default:
                trigger_error('Unsupported image type', E_USER_ERROR);
        }

        if (false === $saveres)
        {
            trigger_error('Failed to save image', E_USER_ERROR);
        }

        return true;
    }
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Re: Uploaded images file permissions

Post by Buddha443556 »

Code: Select all

if (false === chmod($UploadDir.$name, '0777'))
The mode should be an int not a string.

Code: Select all

if (false === chmod($UploadDir.$name, 0777))
Post Reply