Adam Fisher Adam Fisher - 7 months ago 25
PHP Question

Folder writable error with php file upload

I have been working on a php file upload and I run into a error

/uploaded must be a valid, writable folder.
Then I went into properties and unchecked read-only but it does'nt work. It keep going back to read-only.

Right Before i unchecked and applied the changes

After i appiled the changes and open it up again

The code for the file-upload:

<?php
use foundationphp\UploadFile;

$max = 50 * 1024;
$result = array();
if (isset($_POST['upload'])) {
require_once 'src/foundationphp/UploadFile.php';
$destination = '/uploaded';
try {
$upload = new UploadFile($destination);
//$upload->setMaxSize($max);
$upload->upload();
$result = $upload->getMessages();
} catch (Exception $e) {
$result[] = $e->getMessage();
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Uploads</title>
<link href="styles/form.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Uploading Files</h1>
<?php if ($result) { ?>
<ul class="result">
<?php foreach ($result as $message) {
echo "<li>$message</li>";
}?>
</ul>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max;?>">
<label for="filename">Select File:</label>
<input type="file" name="filename" id="filename">
</p>
<p>
<input type="submit" name="upload" value="Upload File">
</p>
</form>
</body>
</html>


CLASS:

<?php
namespace foundationphp;

class UploadFile
{
protected $destination;
protected $messages = array();
protected $maxSize = 51200;

public function __construct($uploadFolder)
{
if (!is_dir($uploadFolder) || !is_writable($uploadFolder)) {
throw new \Exception("$uploadFolder must be a valid, writable folder.");
}
if ($uploadFolder[strlen($uploadFolder)-1] != '/') {
$uploadFolder .= '/';
}
$this->destination = $uploadFolder;
}

public function setMaxSize($bytes)
{
if (is_numeric($bytes) && $bytes > 0) {
$this->maxSize = $bytes;
}
}

public function upload()
{
$uploaded = current($_FILES);
if ($this->checkFile($uploaded)) {
$this->moveFile($uploaded);
}
}

public function getMessages()
{
return $this->messages;
}

protected function checkFile($file)
{
if ($file['error'] != 0) {
$this->getErrorMessage($file);
return false;
}
if (!$this->checkSize($file)) {
return false;
}
return true;
}

protected function getErrorMessage($file)
{
switch($file['error']) {
case 1:
case 2:
$this->messages[] = $file['name'] . ' is too big.';
break;
case 3:
$this->messages[] = $file['name'] . ' was only partially uploaded.';
break;
case 4:
$this->messages[] = 'No file submitted.';
break;
default:
$this->messages[] = 'Sorry, there was a problem uploading ' . $file['name'];
break;
}
}

protected function checkSize($file)
{
if ($file['size'] == 0) {
$this->messages[] = $file['name'] . ' is empty.';
return false;
} elseif ($file['size'] > $this->maxSize) {
$this->messages[] = $file['name'] . ' exceeds the maximum size for a file.';
return false;
} else {
return true;
}
}

protected function moveFile($file)
{
$this->messages[] = $file['name'] . ' was uploaded successfully.';
}
}


The error has to be in the construct method where it says:
if (!is_dir($uploadFolder) || !is_writable($uploadFolder)) {


Because in the error it says:
/uploaded must be a valid, writable folder.
In the code it says !is_dir($uploadFolder) || !is_writeable($uploadFolder)

Answer

Just like Marc B said:

PHP runs at the filesystem level. /uploaded is the root of the server's file 
system, not your site's document root. e.g it's the equivalent of c:\uploaded, 
not c:\home\sites\example.com\html\uploaded

Have you debugged your problem Yet ?

What results did you get back if you debugged it you would of found out what the problem is?