netpumber Posted May 6, 2014 Share Posted May 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/ Share on other sites More sharing options...
Ch0cu3r Posted May 6, 2014 Share Posted May 6, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478408 Share on other sites More sharing options...
netpumber Posted May 6, 2014 Author Share Posted May 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478434 Share on other sites More sharing options...
netpumber Posted May 6, 2014 Author Share Posted May 6, 2014 (edited) 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 47Warning: 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 May 6, 2014 by netpumber Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478437 Share on other sites More sharing options...
gizmola Posted May 6, 2014 Share Posted May 6, 2014 That is a relative path: 'uploads/PL-17534/1.jpg' What is the absolute path of the .../uploads directory? Should be '/somedir/.../uploads' Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478491 Share on other sites More sharing options...
netpumber Posted May 7, 2014 Author Share Posted May 7, 2014 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 47Warning: 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 Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478651 Share on other sites More sharing options...
jazzman1 Posted May 8, 2014 Share Posted May 8, 2014 Post the output of: ls -la /var/www/httpdocs/geneBank/mupload/uploads/PL-17534/ //or ls -ld /var/www/httpdocs/geneBank/mupload/uploads/PL-17534/ What linux distro is that? Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478668 Share on other sites More sharing options...
netpumber Posted May 8, 2014 Author Share Posted May 8, 2014 total 8 drwxr-xr-x 2 root root 4096 May 6 08:09 . drwxr-xr-x 3 http http 4096 May 6 08:09 .. the output of ls -la. and i'm using Arch linux. Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478776 Share on other sites More sharing options...
netpumber Posted May 8, 2014 Author Share Posted May 8, 2014 Ok! i chown and chgrp it for user http and now seems to work. Quote Link to comment https://forums.phpfreaks.com/topic/288283-htaccess-and-file-uploads/#findComment-1478790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.