Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Uploading Files

Codes here !

Moderators: macek, egami, gesf

Uploading Files

Postby constance » Tue Dec 17, 2013 8:43 am

Hi,I am intending to create a page with upload function where user can upload documents such as Mircrosoft word, PDF, Microsoft Excel etc. I am using phpmyadmin MYSQL database. However I not sure on how to go about uploading documents, storing it in database and showing it in the website. I have a sample of fileupload php codes where it restrict the types to images, do you have any idea how to modify it so that it can accept other file types like word,excel and pdf?

Code: Select all
<?php


// list of possible upload error codes
// copied from http://www.php.net/manual/en/features.file-upload.errors.php
$uploadErrors = array(
    UPLOAD_ERR_OK => "No errors.",
    UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
    UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
    UPLOAD_ERR_PARTIAL => "Partial upload.",
    UPLOAD_ERR_NO_FILE => "No file.",
    UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
    UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
    UPLOAD_ERR_EXTENSION => "File upload stopped by extension.",
     
   );

   // list of possible file types accepted
   // here we only allow jpg and png and gif
   $fileTypes = array(
   'image/pjpeg',
   'image/jpeg',
   'image/png',
    'application/pdf',
   'image/gif'

   );
 
   // default value for unsuccessful move file
   $successfullyMoveFile = false;
 
// the name of the input type
$fileInputName = 'file';

// an array to store all the possible errors related to uploading a file
$fileErrorMessages = array();
 
$uploadFile = !empty($_FILES);

if ($uploadFile) {
$fileUploaded = $_FILES[$fileInputName];

// if we have errors while uploading!!
if ($fileUploaded['error'] != UPLOAD_ERR_OK) {
$errorCode = $fileUploaded['error']; // this could be 1, 2, 3, 4, 5, 6, or 7.
$fileErrorMessages['file'] = $uploadErrors[$errorCode];
}

// now we check for file type
$fileTypeUploaded = $fileUploaded['type'];

$fileTypeNotAllowed = !in_array($fileTypeUploaded, $fileTypes);
if ($fileTypeNotAllowed) {
$fileErrorMessages['file'] = 'You should upload a .jpg, .png or .gif file';
}

// if successful, we want to copy the file to our images folder
if ($fileUploaded['error'] == UPLOAD_ERR_OK) {

$successfullyMoveFile = move_uploaded_file($fileUploaded["tmp_name"], $imagesDirectory . $newFileName);

}
}
?>
constance
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Dec 17, 2013 8:37 am

Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron