Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:31:40+00:00 2026-06-10T11:31:40+00:00

I have a PHP script (which is listed below) which uploads an image. However,

  • 0

I have a PHP script (which is listed below) which uploads an image. However, when an image is uploaded it defaults to 640 9so forbidden in a web browser), I need it to upload as 644. Has anyone got any suggestions on how to achieve this?

Many thanks,
Darren

<?php
class upload
{
/**
* the maximum file size allowed
*/
var $maxFileSize;

/**
* array of valid file extensions
*/
var $validExts;

/**
* directory to upload files to
*/
var $uploadDir;

/**
* name of the file uploaded
*/
var $fileName;

/**
* temporary file name
*/
var $tempFileName;

/**
* name of the file when uploaded
*/
var $uploadedFileName;

/**
* current file size
*/
var $fileSize;

/**
* current file extension
*/
var $fileExt;

/**
* last known error
*/
var $error;

/**
* class constructor
* @return NULL
*/
function upload () {
    $this->setValidExts = array(".jpg", ".gif");
}

/**
* set the valid extensions
* @return boolean
*/
function setValidExts ($exts) {
    $this->validExts = array();
    if (is_array($exts) && sizeof($exts) > 0) {
        $this->validExts = $exts;
    }
    return TRUE;
}

/**
* set a filename
* @return
*/
function setFileName ($name, $ufn = FALSE) {
    $this->fileName = "";
    if (strlen($name) > 0) {
        $this->fileName = $name;
        if ($ufn) {
            $this->setUploadFileName($name);
        }
    }
}

/**
* set the uploaded filename
* @return
*/
function setUploadedFileName ($name) {
    $this->uploadedFileName = "";
    if (strlen($name) > 0) {
        $this->uploadedFileName = $name;
    }
}

/**
* set the temporary file name
* @return
*/
function setTempFileName ($name) {
    $this->tempFileName = "";
    if (strlen($name) > 0) {
        $this->tempFileName = $name;
    }
}

/**
* set the maximum file size
* @return
*/
function setMaxFileSize ($size = 0) {
    $this->maxFileSize = intval($size);
}

/**
* set the upload directory
* @return boolean
*/
function setUploadDir ($dir) {
    $dir = $dir;
    if (is_dir($dir)) {
        $this->uploadDir = $dir;
        return TRUE;
    }
    return FALSE;
}

/**
* validate the extension of the uploaded file
* @return boolean
*/
function validateExt () {
    $this->fileExt = strtolower(strrchr($this->fileName, "."));
    if (sizeof($this->validExts) == 0) {
        return TRUE;
    }

    foreach ($this->validExts AS $key => $value) {
        if ($value == $this->fileExt) {
            return TRUE;
        }
    }
    return FALSE;
}

/**
* validate the size of the file
* @return boolean
*/
function validateSize () {
    $this->fileSize = filesize($this->tempFileName);
    if ($this->fileSize <= $this->maxFileSize || $this->fileSize == 0) {
        return TRUE;
    }
    return FALSE;
}

/**
* validate the upload directory
* @return boolean
*/
function validateUploadDir () {
    if (is_writable($this->uploadDir)) {
        return TRUE;
    }
    $this->error = $this->uploadDir. " is not writeable";
    return FALSE;
}

/**
* check the error code
* @return boolean
*/
function checkError () {
    switch ($_FILES['error'])
    {
        case UPLOAD_ERR_OK:

            return TRUE;
            break;

        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
        case UPLOAD_ERR_PARTIAL:
        case UPLOAD_ERR_NO_FILE:
        case UPLOAD_ERR_NO_TMP_DIR:
        default:

            return FALSE;
            break;
    }
}

/**
* the upload function
* @return boolean
*/

function uploadFile () {
    if (!$this->checkError()) {
        return FALSE;
    }

    if ($this->validateUploadDir() && $this->validateExt()) {
        if (is_uploaded_file($this->tempFileName)) {
            if (move_uploaded_file($this->tempFileName, $this->uploadDir . $this->uploadedFileName . $this->fileExt)) {
                return TRUE;
            }
        }
    }
    return FALSE;
}

/**
* resize an image
* @return
*/
function thumbnail ($filename, $new_w, $new_h) {
    $filename = $this->uploadDir . $filename . $this->fileExt;
    if (preg_match('/jpg|jpeg/', $this->fileExt)) {
        $src_img = @imagecreatefromjpeg($this->uploadDir . $this->uploadedFileName . $this->fileExt);
    } elseif (preg_match('/gif/', $this->fileExt)) {
        $src_img = @imagecreatefromgif($this->uploadDir . $this->uploadedFileName . $this->fileExt);
    }

    // get image details
    list($old_x, $old_y) = getimagesize($this->uploadDir . $this->uploadedFileName . $this->fileExt);

    // create new imge
    $dst_img = imagecreatetruecolor($new_w,$new_h);
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, $old_x, $old_y);

    // save the new image
    if (preg_match('/jpg|jpeg/', $this->fileExt)) {
        @imagejpeg($dst_img, $filename);
    } elseif (preg_match('/gif/', $this->fileExt)) {
        @imagegif($dst_img, $filename);
    }

    // memory saving
    imagedestroy($dst_img); 
    imagedestroy($src_img);
    return TRUE;
}

/**
* delete an image
* @access public
* @return boolean
*/
function deleteImage ($name) {
    for ($i = 0; $i < (sizeof($this->validExts) - 1); $i++) {
        @unlink($this->uploadDir.$name.$this->validExts[$i]);
    }
    return TRUE;
}

}

?>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:31:41+00:00Added an answer on June 10, 2026 at 11:31 am

    Try to edit this part

    function uploadFile () {
        if (!$this->checkError()) {
            return FALSE;
        }
    
        if ($this->validateUploadDir() && $this->validateExt()) {
            if (is_uploaded_file($this->tempFileName)) {
                if (move_uploaded_file($this->tempFileName, $this->uploadDir . $this->uploadedFileName . $this->fileExt)) {
                    return TRUE;
                }
            }
        }
        return FALSE;
    }
    

    to this:

    function uploadFile () {
        if (!$this->checkError()) {
            return FALSE;
        }
    
        if ($this->validateUploadDir() && $this->validateExt()) {
            if (is_uploaded_file($this->tempFileName)) {
                if (move_uploaded_file($this->tempFileName, $this->uploadDir . $this->uploadedFileName . $this->fileExt)) {
                    // Try to change permissions
                    if (chmod($this->uploadDir . $this->uploadedFileName . $this->fileExt, 0644)) {
                        return TRUE;
                    }
                }
            }
        }
        return FALSE;
    }
    

    It might be that you do not have the permissions to change file permissions or that the PHP configuration restricts the usage of chmod().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have programmed a proxy.php script (listed below), which would fetch an image specified
I have a php script which saves the original image, then resizes it -
I have a php script which outputs an image, how can I POST data
I have a (PHP) script which runs on a new row in MySQL. However,
I have a php script which requires no web hosting or disk space etc.
I have this PHP script which i'm grabbing images from a directory and displaying
I have a PHP script which creates and listens on a port for connections
I have a PHP script which is generating buttons to each content schedule. The
I have a PHP search script which I've altered slightly and works well for
Here I have a simple php script which displays some values from a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.