lost and found ( for me ? )

Showing posts with label file upload. Show all posts
Showing posts with label file upload. Show all posts

php : upload large files ( Ubuntu 12.04 )

there are file size limitation options you can upload in /etc/php5/apache2/php.ini  file.


In case of ubuntu apache with php5, you can change allowable file size by modifying /etc/php5/apache2/php.ini
; Maximum size of POST data that PHP will accept.
; http://php.net/post-max-size
#post_max_size = 8M
post_max_size = 500M


; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
#upload_max_filesize = 2M
upload_max_filesize = 500M


restart apache2
# /etc/init.d/apache2 restart


If you upload larger files without modifying above options, you will see the following error.
[error] [client 192.168.122.1] PHP Warning:  POST Content-Length of 10486037 bytes exceeds the limit of 8388608 bytes in Unknown on line 0, referer: http://192.168.122.216/upload.html

apache : file uploads with php

many thanks.

root@ubuntu-vm2:~# tail -1 /etc/lsb-release ;uname -ri
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
3.2.0-49-virtual x86_64

root@ubuntu-vm2:~# apache2ctl -v
Server version: Apache/2.2.22 (Ubuntu)
Server built:   Jul 12 2013 13:37:10

prepare an html file
root@ubuntu-vm2:~# cat /var/www/upload.html
<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

this html file executes “upload_file.php”

upload_file.php
root@ubuntu-vm2:~# cat /var/www/upload_file.php
<?php
if ($_FILES["file"]["error"] > 0)
 {
 echo "Error: " . $_FILES["file"]["error"] . "<br>";
 }
else
 {
 echo "Upload: " . $_FILES["file"]["name"] . "<br>";
 echo "Type: " . $_FILES["file"]["type"] . "<br>";
 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
 echo "Stored in: " . $_FILES["file"]["tmp_name"];
 }
?>

put upload.html and uploader_file.php under /var/www
root@ubuntu-vm2:/var/www# pwd
/var/www

root@ubuntu-vm2:/var/www# ls upload*
upload.html  upload_file.php



access to http://apache’s IP/upload.html

select a file you would like to upload and click “Submit”

with above scripts, the uploaded file will not be copied to apache server’s directory.

with the following script,  uploaded files will be copied to the apache server’s directory. ( /var/www/upload )

edit upload_file.php
this php copies *.gif, *.jpeg, *.jpg, *.png files which are under 100KB to upload directory.
root@ubuntu-vm2:/var/www# cat upload_file.php
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 100000)
&& in_array($extension, $allowedExts))
 {
 if ($_FILES["file"]["error"] > 0)
   {
   echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
 else
   {
   echo "Upload: " . $_FILES["file"]["name"] . "<br>";
   echo "Type: " . $_FILES["file"]["type"] . "<br>";
   echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
   echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

   if (file_exists("upload/" . $_FILES["file"]["name"]))
     {
     echo $_FILES["file"]["name"] . " already exists. ";
     }
   else
     {
     move_uploaded_file($_FILES["file"]["tmp_name"],
     "upload/" . $_FILES["file"]["name"]);
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
     }
   }
 }
else
 {
 echo "Invalid file";
 }
?>
root@ubuntu-vm2:/var/www#



make upload directory.
root@ubuntu-vm2:~# mkdir /var/www/upload

access to http://apache IP/upload.html and upload a file.

nn ,the file is not copied to /war/www/upload
root@ubuntu-vm2:/var/www/upload# ls /var/www/upload
root@ubuntu-vm2:/var/www/upload#

check error logs.
root@ubuntu-vm2:/var/www/upload# cat /var/log/apache2/error.log
[Sun Jul 21 13:43:28 2013] [error] [client 192.168.122.1] PHP Warning:  move_uploaded_file(upload/Screenshot_from_2013-07-21 18:42:50.png): failed to open stream: Permission denied in /var/www/upload_file.php on line 32, referer: http://192.168.122.216/upload.html
[Sun Jul 21 13:43:28 2013] [error] [client 192.168.122.1] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpksn2Yy' to 'upload/Screenshot_from_2013-07-21 18:42:50.png' in /var/www/upload_file.php on line 32, referer: http://192.168.122.216/upload.html

how about changing permission.
root@ubuntu-vm2:~# chmod 777 /var/www/upload

try again.

okay. I was able to upload the file.
root@ubuntu-vm2:~# ls /var/www/upload
Screenshot_from_2013-07-21 18:42:50.png