Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - Uploading Files to Web Servers

By: FYICenter.com

Part:   1  2  3  

(Continued from previous part...)

How To Uploaded Files to a Table?

To store uploaded files to MySQL database, you can use the normal SELECT statement as shown in the modified processing_uploaded_files.php listed below:

<?php
  $con = mysql_connect("localhost", "", "");
  mysql_select_db("fyi");
  $error = $_FILES['fyicenter_logo']['error'];
  $tmp_name = $_FILES['fyicenter_logo']['tmp_name'];
  $size = $_FILES['fyicenter_logo']['size'];
  $name = $_FILES['fyicenter_logo']['name'];
  $type = $_FILES['fyicenter_logo']['type'];
  print("
\n");
  if ($error == UPLOAD_ERR_OK && $size > 0) {
    $fp = fopen($tmp_name, 'r');
    $content = fread($fp, $size);
    fclose($fp);     
    $content = addslashes($content);
    $sql = "INSERT INTO fyi_files (name, type, size, content)"
      . " VALUES ('$name', '$type', $size, '$content')";
    mysql_query($sql, $con);
    print("File stored.\n");
  } else {
    print("Upload faield.\n");
  }
  print("
\n"); mysql_close($con); ?>

Note that addslashes() is used to add backslashes to special characters that need to be protected in SQL statements.

What Are the File Upload Settings in Configuration File?

There are several settings in the PHP configuration file related to file uploading:

  • file_uploads = On/Off - Whether or not to allow HTTP file uploads.
  • upload_tmp_dir = directory - The temporary directory used for storing files when doing file upload.
  • upload_max_filesize = size - The maximum size of an uploaded file.

How To Get the Technical Specifications for File Upload?

File upload technical specifications is provided in "Form-based File Upload in HTML - RFC 1867". You can get a copy from http://www.ietf.org/rfc/rfc1867.txt.

Part:   1  2  3  


Selected Developer Jobs:

More...