Jump to content

I'm making a primitive version control script for my own use. I'm trying to upload a file through PHP and then rename the uploaded file with a DateTime stamp. This is what I have below but it keeps giving me a parse error after the brackets. Not sure why.

<?php $timestamp = date('Y/n/j h:i:s'); $uploaddir   = '/repository'; $uploadfile  = $uploaddir . basename($_FILES['file']['name']); move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile); # Our date... $Date2 = date('YmdHis'); $Parts = explode('.', $uploadfile); $Extension = '.' . $Parts[(count($Parts)- 1] ; //problem code hererename($uploadfile, $uploaddir . $Date2 . $Extension); ?>
Link to comment
https://linustechtips.com/topic/389622-datestamp-file-after-upload/
Share on other sites

Link to post
Share on other sites

 

I'm making a primitive version control script for my own use. I'm trying to upload a file through PHP and then rename the uploaded file with a DateTime stamp. This is what I have below but it keeps giving me a parse error after the brackets. Not sure why.

<?php $timestamp = date('Y/n/j h:i:s'); $uploaddir   = '/repository'; $uploadfile  = $uploaddir . basename($_FILES['file']['name']); move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile); # Our date... $Date2 = date('YmdHis'); $Parts = explode('.', $uploadfile); $Extension = '.' . $Parts[>>>(<<<count($Parts)- 1] ; //problem code hererename($uploadfile, $uploaddir . $Date2 . $Extension); ?>

 

Unbalanced parenthesis.

Link to post
Share on other sites

I redid the code like this but I've had no luck renaming the file after upload. 

<?php $target_path = "repository/";$timestamp = date('Y-m-d H:i:s');$target_path = "repository/" .$timestamp .basename( $_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {    echo "The file ".  basename( $_FILES['file']['name']).     " has been uploaded";} else{    echo "There was an error uploading the file, please try again!";}?>
Link to post
Share on other sites

I got it figured it out by using Directory Seperator which breaks up the path, filename, and extension.

$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $upload_dir . DIRECTORY_SEPARATOR;	// Adding timestamp with image's name so that files with same name can be uploaded easily.	$mainFile = $targetPath.date("Y-m-d H-i-s").'-'. $_FILES['file']['name'];

Now I'm trying to plan how to create a Major or Minor Version functionality. I'm trying to figure out how to automatically version the uploaded files as Version 1.0 or 0.1 within the database while maintaining archiving.

 

My thoughts is copy the file, timestamp it after it's copied, and update the version number in the database depending on whether the use selects the file as a major or minor version. Trying to avoid using git or subversion since this is for personal use and don't need the advanced features of those tools.

Link to post
Share on other sites

Trying to avoid using git or subversion since this is for personal use and don't need the advanced features of those tools.

 

And the penalties for not using those "advanced features" are? Git has been undesirable for "personal use" since when? Git is a breeze compared to what you're trying to do.

Link to post
Share on other sites

And the penalties for not using those "advanced features" are? Git has been undesirable for "personal use" since when? Git is a breeze compared to what you're trying to do.

 

This code will eventually make it's way into a commercial Document Management System I'm planning on developing which avoids the technicalities of code versioning systems like Git.

Link to post
Share on other sites

What technicalitites?

 

For example, things like remembering to commit files, branching, etc. For a DMS, users don't need that ability. They'd be more akin to using something like Subversion with a simpler system of tracking changes. Although, from what I've seen from testing many other commercial systems a lock/unlock document feature is used most often. 

 

I'd rather just see a filename labeled Version 1.1 so I could get an idea of what changes were done and perhaps a user who last initialized changes. This system isn't intended to be used by multiple people at the same time and certainly not for professional programmers. It's a way for me to manage my scripts in a way I find best for me. 

 

Anyway, speaking of which I can't figure out why I go into an infinite while loop when I try to get 0.2 as the version (adding 0.01 to the filename). The original 0.1 value sets no problem. It's only when the file is uploaded twice as a minor version that I have issues. 

 

Here's my code:

if ( isset( $_POST['addfile'] ) ) {// variablesdefine('UPLOAD_DIR', 'repository/'); $fileName = $_FILES['fileToUpload'];if($_POST['rev_type'] == 'Minor') {    function update_file_name_minor($file)     {      $pos = strrpos($file,'.');      $ext = substr($file,$pos);       $dir = strrpos($file,'/');      $dr  = substr($file,0,($dir+1));       $arr = explode('/',$file);      $fName = substr($arr[(count($arr) - 1)], 0, -strlen($ext));      $exist = FALSE;      $i = 0.01;      while(!$exist)      {        $file = $dr.$fName.'_'.'Ver '.$i.$ext;        if(!file_exists($file))          $exist = TRUE;        $i + 0.01;      }      return $file;    }    // check for which action should be taken if file already exist    if(file_exists(UPLOAD_DIR . $fileName['name']))     {      $updatedFileName = update_file_name_minor(UPLOAD_DIR.$fileName['name']);      move_uploaded_file($fileName['tmp_name'], $updatedFileName);      echo "You have successfully uploaded and renamed the file as a minor revision.";    }    else    {      move_uploaded_file($fileName['tmp_name'], UPLOAD_DIR.$fileName['name']);     echo "You have successfully uploaded the file.";    }}elseif($_POST['rev_type'] == 'Major') {    function update_file_name_major($file)     {      $pos = strrpos($file,'.');      $ext = substr($file,$pos);       $dir = strrpos($file,'/');      $dr  = substr($file,0,($dir+1));       $arr = explode('/',$file);      $fName = substr($arr[(count($arr) - 1)], 0, -strlen($ext));      $exist = FALSE;      $i = 2;      while(!$exist)      {        $file = $dr.$fName.'_'.'Ver '.$i.$ext;        if(!file_exists($file))          $exist = TRUE;        $i++;      }      return $file;    }    // check for which action should be taken if file already exist    if(file_exists(UPLOAD_DIR . $fileName['name']))     {      $updatedFileName = update_file_name_major(UPLOAD_DIR.$fileName['name']);      move_uploaded_file($fileName['tmp_name'], $updatedFileName);      echo "You have successfully uploaded and renamed the file as a major revision.";    }    else    {      move_uploaded_file($fileName['tmp_name'], UPLOAD_DIR.$fileName['name']);     echo "You have successfully uploaded the file.";    }             }         } //main if
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×