Jump to content

Uploading files via php


alphamoment

Recommended Posts

I'm trying to upload .doc .docx and .txt files to my server but it keeps failing. I get a white screen and nothing in my uploads folder. This is my index.php

<body>
  <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="pic" />
    <button type="submit" name="btn-upload">upload</button>
  </form>
</body>

Then this is my upload.php 

<?php

if ($_POST['Upload'] == 'Upload')
{
    $goodExtensions = array(
        '.doc',
        '.docx',
        '.txt',
    );
    $error = '';
    $uploaddir = './uploads ';
    $name = $_FILES['filename']['name'];
    $min_filesize = 10;
    $stem = substr($name, 0, strpos($name, '.'));
    $extension = substr($name, strpos($name, '.') , strlen($name) - 1);
    if (!in_array($extension, $goodExtensions)) $error.= 'Extension not allowed<br />';
    echo "<span> </span>";
    if (filesize($_FILES['filename']['tmp_name']) < $min_filesize) $error.= 'File size too small<br />' . "\n";
    $uploadfile = $uploaddir . $stem . $extension;
    $filename = $stem . $extension;
    if ($error == 'wtf')
    {
        if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile))
        {
            echo 'File Uploaded. Thank You.';
        }
    }
    else echo $error;
}

?>

I'm running a DebianOS. I have the /uploads folder chmod at 777. My logs say: 

Warning: move_uploaded_file(./uploads uploadtest.txt): failed to open
stream: Permission denied in /var/www/upload.php

What am I missing? thank you in advance

Also my files are 

/www/index.php
/www/upload.php
/www/uploads/

EDIT: Script is working fine, I just had to restart apache2 to pick up the path. If it's okay I'll leave this here if anyone needs to use this.

Link to comment
Share on other sites

- Look at the value of $uploaddir. Look at it. Carefully. Really you should have spotted this problem by reading the error message.

- Your $stem and $extension assume the file doesn't have multiple periods in it. Like "bad.doc.exe".

- $error will never be the value "wtf" so I don't know how you managed to get the move_uploaded_file() to (attempt to) execute in the first place.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.