lost and found ( for me ? )

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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.