0

我有一个上传图片的 PHP 脚本(如下所列)。但是,当上传图像时,它默认为 640 9so 在网络浏览器中被禁止),我需要将其上传为 644。有没有人对如何实现这一点有任何建议?

非常感谢,达伦

<?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;
}

}

?>

4

2 回答 2

1

尝试编辑此部分

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;
}

对此:

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;
}

可能是您没有更改文件权限的权限,或者 PHP 配置限制了chmod().

于 2012-08-28T11:46:35.587 回答
1

您可以使用chmod

<?php
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);

// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);

// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
?>
于 2012-08-28T11:46:51.313 回答