¿Cómo puedo cambiar los archivos que se cargan en este script?

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

}

?>

preguntado el 28 de agosto de 12 a las 12:08

Could you please only show the ¿código? -

2 Respuestas

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

a esto:

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().

Respondido 28 ago 12, 12:08

puedes usar 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);
?>

Respondido 28 ago 12, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.