0

I upload a file to a server by filling out a form. The file should be uploaded and a record should be created with many data in the mysql table. This is how I specify the upload path:

$upload_path = "upload/";

In the public_html folder I have all the necessary .php files and the precreated upload folder as well. The upload is successful, mysql table has one new record, everything seems to be fine, except I cannot see the file in the public_html/upload folder.

I know this must be a rookie question, I need help.

   $allowed_filetypes = array('.jpg','.gif','.bmp','.png','.tif','.doc','.docx','.xls','.xlsx','.pdf','.psd');
     $filename = $_FILES['file']['name'];
     $max_filesize = 524288; //0.5MB
     $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
     //$upload_path = "C:/wamp/www/upload/";    //'./files/';
     $upload_path = "upload/";
      if(!in_array($ext,$allowed_filetypes))
      {
        die('The file you attempted to upload is not allowed or you haven not selected any files!');
      }
      if(filesize($_FILES['file']['tmp_name']) > $max_filesize)
      {
        die('The file you attempted to upload is too large.');
      }
      if(!is_writable($upload_path))
      {
        die('Invalid or non-writable folder');
      }


      if (file_exists($upload_path . $_FILES["file"]["name"]))
      {
        //echo $_FILES["file"]["name"] . " already exists. ";
    ?>
          <script type="text/javascript">
          alert('File with this name already exists!');
          </script>
    <?php

      }
      else
      {
        if(move_uploaded_file($_FILES['file']['tmp_name'],$upload_path . $filename))
        {
            ?>
          <script type="text/javascript">
            alert('Successful upload!');
          </script>
    <?php   
    }
//getting variables, insert into statement -> record added to table, this part is fine
    }
    ?>
1
  • Show your code. You may not have moved the uploaded file. Commented Feb 3, 2012 at 17:46

2 Answers 2

1

could also be that the permissions on the folder are not set so that the file can be created. This is done one of 2 ways.

1) If you have root access you can let the user who runs the web server own that directory with a chown command.

chown username dirpath

2) If you do not have root access, you must make the directory world writable.

chmod 777 dirpath

Try this code....

$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads';

if(is_dir($upload_dir) && is_writable($upload_dir))
{
  // put your code to handle uploaded file here
}
else
{
    echo "Sorry the upload file does not exist or I can not save the file there due to directory permissions";
}
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry but I have no idea what you are telling me. I created a domain at a company(000webhost.com), created a db and table. Now I am stuck
As the other commenter pointed out when you upload a file it goes into a temporary directory. After it's uploaded you use move_uploaded_file to put it in the directory you want it in. You need to use the full directory path so it might look like "/home/username/public_html/uploads" using "uploads/" will get you nothing. Lastly the uploads directory has to writable by the web server. If it is not writable then php will fail to move the file.
You mention that the database record is updated, so it seems you have 2 problems 1) file not being moved, 2) validation of the move is not happening and the database is getting updated anyway. I suggest that user move_uploaded_file that you first use is_writable() on the uploads directory path to make sure you can move the file to begin with.
Hm. I checked the file manager of the domain and i see the files. But how come I do not see them in total commander?? I have rw-r--r-- perms
rw-r-r means read/write for you the owner, read for the group you belong too, and read for everyone. Apache rarely runs as your user, so the permissions need to be rw-rw-rw in order to save the files there. therefore you need to change the permissions.
|
0

When PHP uploads, it uploads into the tmp directory (usually set by php.ini), then you use move_uploaded_file (http://php.net/manual/en/function.move-uploaded-file.php) to rename and move the file to where you want it.

Are you missing this step? It is hard to tell from the info in your question.

1 Comment

My line is if(move_uploaded_file($_FILES['file']['tmp_name'],$upload_path . $filename)) -> success

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.