Page 1 of 1

file upload modification

Posted: Fri May 31, 2002 2:25 pm
by abionifade
Hello,

This is a script for handling file uploads to server side storage. At the moment, the uploaded file filename is manipulated. When i look at my hard disk the file which i have uploaded has no extension, however, im trying to modify this script to retain the physical file extension.

Can anybody help?



Code: Select all

<?php

## Set up database connections

include("dbconnect.php");
require("redirectuser.php");

## Handle if data is submitted
## OK, lets get the uploaded data and insert it into the database
## Where $upFile is an array


if (isset($submit)) &#123;
  if (isset($fileupload) && $fileupload != "none") &#123;

    ## Get variables for new filename in file store
    ## Daves vars, if you dont want them delete from here down:
    $timestamp = time();
    $filedir = "/OpenSA/Apache/htdocs/final/files/work/";
 
	## is getting the length of the temp file name of the uploaded file. 
	$len = strlen($HTTP_POST_FILES&#1111;'userfile']&#1111;'name']);

	## Here I cut from the end of the filename the last 3 chars which would be the extention for windows programs. 

	$ext = substr($HTTP_POST_FILES&#1111;'userfile']&#1111;'name'],$len - 3,3);

	$filenamenew = strtolower(str_replace(" ", "", $module_title)) . "-" . $timestamp . "." . $ext;

    $filestore = $filedir . $filenamenew;

    ## Copy function is used to copy the file from temp store before placing into permanent store

    if (!@copy($fileupload, $filestore)) &#123;
      echo "There was a problem copying the temporary file to a permenant store.";
      exit;
    &#125;

    ## n12br Returns string with '<br />' (HTML line breaks) inserted before all newlines.

    $filedesc = addslashes(nl2br($filedesc));

    $sql = "INSERT INTO coursework_table";
    $sql .= "(coursework_id, description, fileloc, filename, filesize, filetype, user_id, submission_time, module_title) ";
    $sql .= "VALUES ('$courswork_id', '$filedesc', '$filestore', '$filenamenew', ";
    $sql .= "'$fileupload_size', '$fileupload_type', '$mycookie', NOW(), '$module_title')";

Posted: Fri May 31, 2002 2:41 pm
by hob_goblin
$ext = substr($HTTP_POST_FILES['userfile']['name'], -3);

should work...

Posted: Fri May 31, 2002 2:58 pm
by abionifade
hiya,

really appreciate you getting back to me.

tried it out and unfortunately im still experiencing the same problem.

-Abi