file upload modification

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

file upload modification

Post 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')";
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

$ext = substr($HTTP_POST_FILES['userfile']['name'], -3);

should work...
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Post by abionifade »

hiya,

really appreciate you getting back to me.

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

-Abi
Post Reply