Question

<?php

class File_Streamer {

private $fileName;
private $contentLength;
private $path;

    public function __construct()
    {
        if (array_key_exists('HTTP_X_FILE_NAME', $_SERVER) && array_key_exists('CONTENT_LENGTH', $_SERVER)) {
            $this->fileName = $_SERVER['HTTP_X_FILE_NAME'];
            $this->contentLength = $_SERVER['CONTENT_LENGTH'];
        } else throw new Exception("Error retrieving headers");
    }

    public function setDestination($p)
    {
        $this->path = $p;

    }

    public function receive()
    {
        if (!$this->contentLength > 0) {
            throw new Exception('No file uploaded!');
        }

        file_put_contents(
            $this->path . $this->fileName, 
            file_get_contents("php://input")
        )
        ;
        return true;
    }
}
?>

I have this code to upload my files, but i want them to upload with CHMOD 755, need some help, please.

Was it helpful?

Solution

Two options:

  1. First upload file completely, then chmod it.
  2. Create empty file, chmod it, then put your data into it.

Doing a chmod on a file is rather simple in PHP:

chmod("/directory/file", 0755);  

Just remember that you need to use octal and not decimal to get the correct value of mode; so prepend your "755" with a "0" as shown in the example above!

For your code, I would do it like this:

public function receive()
{
    ...

    chmod($this->path . $this->fileName, 0755); 
    return true;
}

More details related to the CHMOD command in php can be found at http://php.net/manual/de/function.chmod.php.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top