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:


if (!session_is_registered("SESSION"))
    $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.";
        echo "You must select a file to upload.";
<title>MyWebSite Administration</title>
<div align="center"><B><font size="+5">MyWebSite File Upload</font></B>
<form action="upload.php" enctype="multipart/form-data" method="POST">
  <div align="center">    <br />
    <table width="335" border="1">
        <td width="97">File Directory:</td>
        <td width="222"><input name="myfile" type="file" id="myfile"></td>
        <td colspan="2"><div align="center">
            <input type="submit" name="submit2" value="Submit">
    <input type="hidden" name="submit" value="Submit">
<p align="center"><a href="">Return to Admin Menu</a> | <a href="">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
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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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.


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.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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.


Is /var/www/html/files/ the root af your site?
Anyway, I will sugest you take a look at
The way you are handling an upload is'nt so good.
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?


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.


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!";
      $_SESSION["message"] = "Sorry, unable to upload file!";

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

<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">

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


      if(isset($_SESSION["message"])) {
      echo $_SESSION["message"];


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!


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:

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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.