0

画像をアップロードするPHPスクリプト(以下にリストされています)があります。ただし、画像をアップロードすると、デフォルトで640 9になり、Webブラウザでは禁止されます)、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 に答える