File upload PHP5

Hi,
We are using Apache 2.52 with PHP5. The upload file script which worked under PHP4 is not working under PHP5. Here is php scripts to upload file


<?php
include("../ssi/header.html");
?>

<p class="main">
<center><h2 class="main">Upload Page</h2>
<br>
<P>

<form enctype="multipart/form-data" action="results.html" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="500000000">
Upload File: <input name="uploadfile" type="file" size=40>
<P>

File Date:
<select name="quarter">
<option value="" selected>Quarter</option>
<? for ($i=1; $i<=4; $i++) { echo "<option value=\"$i\">$i</option>\n"; } ?>
</select>

<select name="year">
<option value="" selected>Year</option>
<?php $Y = date("Y"); $Y1 = $Y+1; $Y0 = $Y-1;
echo "<option value=\"$Y0\">$Y0</option>\n";
echo "<option value=\"$Y\">$Y</option>\n";
echo "<option value=\"$Y1\">$Y1</option>\n";
?>
</select>

</P>
<P>
<input type="submit" value="Upload File">
</form>
</P>
</center>
<br>
<?
include("../ssi/footer.html");
?>


results.php


<?PHP
$quarter = $_POST['quarter'];
$year = $_POST['year'];
$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/test/';
//$uploaddir = '/tmp/';

echo (file_exists($uploaddir) && is_dir($uploaddir)) ? 'Directory path ok' : 'Directory does not exist';

if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
$name = $_FILES['uploadfile']['name'];
$uploaddir = $uploaddir . $name;
$tempname = $_FILES['uploadfile']['tmp_name'];

$result = move_uploaded_file($_FILES['uploadfile']['tmp_name'],$uploaddir);

if ($result == 1){ echo "<p>File uploaded.</p>";}
else { die("Error uploading file. Please contact an administrator");
}
}

?>



I tried $uploaddir to /tmp and it works fine.


if $uploaddir is set to /opt/www/domain.com/html/test/ it gives following error


[Thu Jul 20 15:17:14 2006] [error] [client 172.16.22.66] PHP Warning: move_uploaded_file(/opt/www/domain.com/html/test/test.txt) [<a href='http://www.phpbuilder.com/board/archive/index.php/function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: Permission denied in /opt/www/domain.com/html/upload/results.html on line 20, referer: <!-- m --><a class="postlink" href="http://192.168.19.66/upload/">http://192.168.19.66/upload/</a><!-- m -->


[Thu Jul 20 15:17:14 2006] [error] [client 172.16.22.66] PHP Warning: move_uploaded_file() [<a href='http://www.phpbuilder.com/board/archive/index.php/function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/phpOTS20J' to '/opt/www/domain.com/html/test/test.txt' in /opt/www/domain.com/html/upload/results.html on line 20, referer: <!-- m --><a class="postlink" href="http://192.168.19.66/upload/">http://192.168.19.66/upload/</a><!-- m -->



Directory persmission is set 777 to all the directory from /opt/www/domain.com/html/test. I was able to copy file to $uploaddir from command prompt as user apache.

Please help.

thanks.I found the solution at <!-- m --><a class="postlink" href="http://us3.php.net/function.move-uploaded-file">http://us3.php.net/function.move-uploaded-file</a><!-- m -->. Under notes i saw this
-----------------------------------------------------------------------------------------------------------
On the Fedora Core 3 Linux distribution, you may get a "failed to open stream: Permission denied in ..." message. I fact changing the permission of the directory will not work (even if you set to 0777). It is because of the new SELinux kernel that allow apache user to write only in /tmp dir (I think). In order to solve the problem you must to disable the SELinux (at least for apache service) to allow the server to write in other directories. To do that, run the system-config-securitylevel app and disable the SE to apache service. Reboot your system and continue your work. Hope it helps!
-----------------------------------------------------------------------------------------------------------

Guys,
I hope this would help somebody. I am on Redhat Enterprise linux 4. This below solution also holds good for Redhat Enterprise Linux. Execute the command
system-config-securitylevel under X and goto SElinux tab, in the window at the bottom look for HTTPD service and check disable for HTTPD and reboot the server to take effect of changes.

Cheers!!!!!!!!!!!!!
 
Back
Top