erdomester
erdomester

Reputation: 11829

Upload file to server with php mysql

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
    }
    ?>

Upvotes: 0

Views: 1532

Answers (2)

thenetimp
thenetimp

Reputation: 9820

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";
}

Upvotes: 1

TrippyD
TrippyD

Reputation: 735

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.

Upvotes: 0

Related Questions