• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2045
  • Last Modified:

File Upload

I have writin a basic php script to upload files onto my web server but I am getting this error:

Warning: SAFE MODE Restriction in effect. The script whose uid is 519 is not allowed to access /var/www owned by uid 0 in /home/virtual/site8/fst/var/www/html/Admins/Admins/upload.php on line 14
Error: could not make directory.

Here is my code:

<?
session_start();

if (!session_is_registered("SESSION"))
{
      header("Location: http://www.mywebsite.com/Admins/index.php");
}
    $dirname = "files";
    define(PATH, "/var/www/");
    $filename = $myfile_name;
    if ($filename != "")
    {
        if (!$dir = @opendir(PATH.$dirname))
        mkdir(PATH.$dirname, 0700) or die("<b>Error:</b> could not make directory.");
        copy($myfile, PATH.$dirname."/".$filename) or die("<b>Error:</b> could not copy file.");
        unlink($myfile) or die("<b>Error:</b> could not delete uploaded file.");
        echo "Upload sucessful.";
    }
    else
    {
        echo "You must select a file to upload.";
    }
?>
<title>MyWebSite Administration</title>
<div align="center"><B><font size="+5">MyWebSite File Upload</font></B>
</div>
<form action="upload.php" enctype="multipart/form-data" method="POST">
  <div align="center">    <br />
    <table width="335" border="1">
      <tr>
        <td width="97">File Directory:</td>
        <td width="222"><input name="myfile" type="file" id="myfile"></td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
            <input type="submit" name="submit2" value="Submit">
        </div></td>
      </tr>
    </table>
    <input type="hidden" name="submit" value="Submit">
  </div>
</form>
<p align="center"><a href="http://www.mywebsite.com/Admins/Admins/main.php">Return to Admin Menu</a> | <a href="http://www.mywebsite.com/Admins/main.php">Return to Main Menu</a> </p>

There some more things I need added to the script.
1) Choose the directory to upload the file, but has the default path of http://www.mywebsite.com/files/
2) Have the php script print out all the files in that directory and have the option to delete and rename
3) When a file has sucessfuly uploaded, "Upload Complete" prints out under my title in red font.

I'm happy to give an A grade to anyone who can help me with my code.

Cheers

-OBCT
0
OBCT
Asked:
OBCT
1 Solution
 
outerJoinCommented:
Greetings, OBCT!

This is most likely a Unix file permissions problem.

In order to help with your question, I will need a little more information:

What do the directory permissions look like at "/var/www/"?  Also, what username does your instance of PHP run under?

===

By the way, may I make a recommendation?  Unless you are running on an intranet behind a firewall and never allowing access to your site from the outside, it is not very secure to provide access starting at /var/www/.  It is better to start in an isolated "safe room" subdirectory that has rwx permissions for the PHP system.  This saferoom subdirectory should be located at least 1 level down from /var/www/; or even better yet, in a separate directory tree.

You may also wish to reject filenames containing slashes or ".." or other unsafe constructs.

===

Looking forward to being able to help,
   -- outerJoin
0
 
OBCTAuthor Commented:
I think I typed in the wrong directory. The one I wanted to use was /var/www/html/files/.
I have givin total permissions to that directoy, but I can throw on a .htaccess file to protect it (if this upload script lets me)
This upload.php file is secured by session variables, and only administrators can access it.

Cheers

-OBCT
0
 
maninder123Commented:
This is happening because of open_basedir restriction in safe mode.
If you can change its value through ini_set method. then give it a value of the directory where you want to upload the file.
If you are not able to change ini variables because of safe mode restrictions you can try to change them in .htaccess file.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
OBCTAuthor Commented:
I have no idea how to change the file, I know very little about servers.
If you can post how I can fix the problem that would be great.

Cheers

-OBCT
0
 
show_sourceCommented:
Is /var/www/html/files/ the root af your site?
Anyway, I will sugest you take a look at http://dk.php.net/manual/en/features.file-upload.php
The way you are handling an upload is'nt so good.
0
 
OBCTAuthor Commented:
I'm having no luck what so ever. Every tutorial I try and every souce code I use, ends up giving me an error.
Am I aloud to ask for a example code?
Or is there anything else I should try?

Cheers

-OBCT
0
 
show_sourceCommented:
Shure you can ask for code! :O)

Try this first:
Be sure that $dist is the correct path to upload dir. Is it a dir outside "webscope", you will need to write the full path, e.x. /home/virtual/site8/fst/var/www/files/
And be sure there's write access to upload dor.

<?php
session_start();

if(isset($_FILES["singlefile"])) {

$dist = $_SERVER["DOCUMENT_ROOT"] ."/files/";

      if(is_uploaded_file($_FILES["singlefile"]["tmp_name"])) {

            if(move_uploaded_file($_FILES["singlefile"]["tmp_name"], $dist . $_FILES["singlefile"]["name"])) {
      $_SESSION["message"] = "File has been uploaded with succes!";
            }else{
      $_SESSION["message"] = "Sorry, unable to upload file!";
            }
      }

header("Location:". $_SERVER["PHP_SELF"] ."");
exit();

}
?>
<html><head></head>
<body>
<center>
<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table><tr>

      <td>Choose a file:<br>
<input name="singlefile" type="file" size="23"> <input type="submit" value="Upload"></td>

</tr></table></form>

<?php
      if(isset($_SESSION["message"])) {
      echo $_SESSION["message"];
      unset($_SESSION["message"]);
      }
?>
</center>

</body></html>
0
 
OBCTAuthor Commented:
Thank you show_source, YOU ARE A GENIUS! Or maybe I'm just newbie ;)
I really appriciate all your help and the fact you didn't give up on me. Hehe
You deserve everyone one of these points!

Cheers!

-OBCT
0
 
show_sourceCommented:
Ok, thanks!
For some more upload, (comments is in danish, but $dist is the same, and you should could figure out what other vars means) try this:

http://www.showsource.dk/php/download.php?id=1064975823

0
 
imstaffCommented:
i'm getting error when i try to upload a file when i'm logged in (session started), the file isn't send to the server!
when i'm logged off (session off) the file seems to be sent!
but i need to stay logged to see if the user is able to upload or not!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now