Jump to content

.htaccess and file uploads


netpumber

Recommended Posts

Hello!

 

I have a problem with uploading some files from my web app, when i m running it through Apache's document root.

 

Let's take it from the beginning.

 

I have create a vhost where all the files of my web app are there.

Here is the vhost directive of apache's config

 

<VirtualHost 127.0.0.1:80>
    DocumentRoot "/home/n3t/dev/http/geneBank/"
    ServerName dev.geneBank
    ServerAlias dev.geneBank
    ErrorLog "/var/log/httpd/VHost-geneBank/error_log"
    CustomLog "/var/log/httpd/VHost-geneBank/access_log" common
    <Directory /home/n3t/dev/http/geneBank/>
                    DirectoryIndex index.htm index.html index.php
                    Options ExecCGI Indexes FollowSymLinks MultiViews Includes
                    AllowOverride All
                    Order allow,deny
                    allow from all
                    Require all granted
    </Directory>
</VirtualHost>

and here is the php file that uploads files.

 

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif');

// Check if a rec id has sent and creates a directory with its name.
if($_POST['uplRecID'] != ''){

        //Check if the given RecId is valid by begining with 'PL'.
        $str = substr($_POST['uplRecID'], 0, 2);
        if($str == 'PL'){

                // Check if the directory allready exists.
                if(!file_exists("uploads/".$_POST['uplRecID'])){


                        echo " Dir didn't exist and we create it now..";
                        if(!mkdir("uploads/".$_POST['uplRecID'], 0777)){
                                echo '{"status":"error creating the dir"}';
                        }
                }
        }else{
                echo '{"status":"Unaccepted recID"}';
                exit;
        }
}


if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){


        $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);


        if(!in_array(strtolower($extension), $allowed)){
                echo '{"status":"Unaccepted extension"}';
                exit;
        }




        if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_POST['uplRecID'].'/'.$_FILES['upl']['name'])){
                echo '{"status":"success"}';
                exit;
        }
}else{

echo $_FILES['upl']['error'];
echo '{"status":"error"}';
exit;


}
?>
So, when im running this code from dev.geneBank (Vhost) everything works like a charm. But when i move that code into apache's document root (/var/www/httpdocs/) directory and run it through the web (not locally) , $_FILES['upl']['error'] returns error with number 3.
 
So i thought that maybe the fault is that document root isn't configured for file uploading.
 
If i am correct, is there any way to configure the document root using .htaccess file ? I don't want to change the main httpd.conf file.
 
Thank you.
 
Link to comment
Share on other sites

Document root has nothing to do with the file upload. When the file is being uploaded it is stored in a temporary location on the server somewhere. It is the down to your PHP code move the uploaded file to its intended location.

 

 

 

$_FILES['upl']['error'] returns error with number 3.

 

 

From the PHP manual on error code 3

UPLOAD_ERR_PARTIAL

Value: 3; The uploaded file was only partially uploaded.

 

 

Which means for some reason the client (the users browser) is not uploading file properly. Maybe the connection between the server and client is being timeout/lost? 

Link to comment
Share on other sites

From the PHP manual on error code 3

 

Which means for some reason the client (the users browser) is not uploading file properly. Maybe the connection between the server and client is being timeout/lost? 

 

 

Yes i know what error code 3  means but the weird thing is that the server and the client is the same PC in both cases. Either if i'm running the application locally (127.0.0.1) or running it through the web (e.g www.blahblah.com). I cannot find a reason for the connection to timeout/lost , every time i'm trying it.

Link to comment
Share on other sites

Finally with ZAP tool found that the upload.php file returns this :

 

Warning: move_uploaded_file(uploads/PL-17534/1.jpg): failed to open stream: Permission denied in /var/www/httpdocs/geneBank/mupload/upload.php on line 47

Warning: move_uploaded_file(): Unable to move '/tmp/phpn7XUJH' to 'uploads/PL-17534/1.jpg' in /var/www/httpdocs/geneBank/mupload/upload.php on line 47

 

 

upload directory has 777 permissions and chowned to http user , but the message id still produced .

Edited by netpumber
Link to comment
Share on other sites

I changed the path but im still getting this error :

 

Warning: move_uploaded_file(/var/www/httpdocs/geneBank/mupload/uploads/PL-17534/1.jpg): failed to open stream: Permission denied in/var/www/httpdocs/geneBank/mupload/upload.php on line 47

Warning: move_uploaded_file(): Unable to move '/tmp/php2HcnFL' to '/var/www/httpdocs/geneBank/mupload/uploads/PL-17534/1.jpg' in /var/www/httpdocs/geneBank/mupload/upload.phpon line 47

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.