Jump to content

PHP code replaces same named file in the server


sourav.network

Recommended Posts

Hi Guys, I am new to this forum. I have tried other forums but with no success. I hope you can answer me.

 

Project: Job Application Form along with CV upload.

Backend: MySQL.

Problem: When the form is submitted, it replaces the same named file in my server.

Example: When I upload a file named "Example.doc" using this form and if there is already a file named "Example.doc" in the same directory (Server), the new file (example.doc) will replace the old one.

Solution Required: May be,

                              a) When I upload a file, the file name gets renamed with say the personsname+DOB+timestamp.

                              b) Any other solution which will not delete the old files present.

 

I am pasting the PHP code that I used .... for your kind perusal. Please help:

 

<?php

 

 

// Receiving variables

@$pfw_ip= $_SERVER['REMOTE_ADDR'];

@$Name = addslashes($_POST['Name']);

@$Telephone = addslashes($_POST['Telephone']);

@$Email = addslashes($_POST['Email']);

@$Mobile = addslashes($_POST['Mobile']);

@$CITY = addslashes($_POST['CITY']);

@$OtherLocation = addslashes($_POST['OtherLocation']);

@$PostalAddress = addslashes($_POST['PostalAddress']);

@$Years = addslashes($_POST['Years']);

@$Months = addslashes($_POST['Months']);

@$Lacs = addslashes($_POST['Lacs']);

@$Thousands = addslashes($_POST['Thousands']);

@$FunctionalArea = addslashes($_POST['FunctionalArea']);

@$CurrIndustry = addslashes($_POST['CurrIndustry']);

@$KeySkills = addslashes($_POST['KeySkills']);

@$ResumeTitle = addslashes($_POST['ResumeTitle']);

@$JobID = addslashes($_POST['JobID']);

@$TenthUniv = addslashes($_POST['TenthUniv']);

@$TenthPer = addslashes($_POST['TenthPer']);

@$TwlUniv = addslashes($_POST['TwlUniv']);

@$TwlPer = addslashes($_POST['TwlPer']);

@$UGCOURSE = addslashes($_POST['UGCOURSE']);

@$GradPer = addslashes($_POST['GradPer']);

@$PGCOURSE = addslashes($_POST['PGCOURSE']);

@$PPGCOURSE = addslashes($_POST['PPGCOURSE']);

@$course1 = addslashes($_POST['course1']);

@$course2 = addslashes($_POST['course2']);

@$course3 = addslashes($_POST['course3']);

@$Gender = addslashes($_POST['Gender']);

@$DOB = addslashes($_POST['DOB']);

@$Nationality = addslashes($_POST['Nationality']);

@$select2 = addslashes($_POST['select2']);

@$file_Name = $_FILES['file']['name'];

@$file_Size = $_FILES['file']['size'];

@$file_Temp = $_FILES['file']['tmp_name'];

@$file_Mime_Type = $_FILES['file']['type'];

 

function RecursiveMkdir($path)

{

  if (!file_exists($path))

  {

      RecursiveMkdir(dirname($path));

      mkdir($path, 0777);

    }

  }

 

 

// Validation

if( $file_Size == 0)

{

die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid file</font></p>");

}

if( $file_Size >50000000)

{

//delete file

unlink($file_Temp);

die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid file</font></p>");

}

if( $file_Mime_Type != "application/msword" AND $file_Mime_Type != "application/pdf" AND $file_Mime_Type != "application/rtf" )

{

unlink($file_Temp);

die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid file</font></p>");

}

$uploadFile =  $file_Name ;

if (!is_dir(dirname($uploadFile)))

  {

    @RecursiveMkdir(dirname($uploadFile));

  }

else

  {

  @chmod(dirname($uploadFile), 0777);

  }

@move_uploaded_file( $file_Temp , $uploadFile);

chmod($uploadFile, 0644);

$file_URL = "http://www.myserver.com/resume/".$file_Name ;

 

//saving record to MySQL database

 

 

@$pfw_strQuery = "INSERT INTO `Candidate_Test`(`Name`,`tel`,`email`,`mob`,`city`,`othr`,`add`,`yrs`,`mon`,`lacs`,`thnd`,`func`,`curr`,`skills`,`title`,`Jobid`,`tenb`,`tenp`,`twlb`,`twlp`,`ugb`,`ugp`,`pg`,`ppg`,`c1`,`c2`,`c3`,`gen`,`dob`,`nation`,`pref`,`file`)VALUES (\"$Name\",\"$Telephone\",\"$Email\",\"$Mobile\",\"$CITY\",\"$OtherLocation\",\"$PostalAddress\",\"$Years\",\"$Months\",\"$Lacs\",\"$Thousands\",\"$FunctionalArea\",\"$CurrIndustry\",\"$KeySkills\",\"$ResumeTitle\",\"$JobID\",\"$TenthUniv\",\"$TenthPer\",\"$TwlUniv\",\"$TwlPer\",\"$UGCOURSE\",\"$GradPer\",\"$PGCOURSE\",\"$PPGCOURSE\",\"$course1\",\"$course2\",\"$course3\",\"$Gender\",\"$DOB\",\"$Nationality\",\"$select2\",\"$file_Name\")" ;

@$pfw_host = "localhost";

@$pfw_user = "testuser";

@$pfw_pw = "ultimate09";

@$pfw_db = "Resumebank";

$pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw);

if (!$pfw_link) {

die('Could not connect: ' . mysql_error());

}

$pfw_db_selected = mysql_select_db($pfw_db, $pfw_link);

if (!$pfw_db_selected) {

die ('Can not use $pfw_db : ' . mysql_error());

}

 

//insert new record

$pfw_result = mysql_query($pfw_strQuery);

if (!$pfw_result) {

die('Invalid query: ' . mysql_error());

}

mysql_close($pfw_link);

 

echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Successful</font></p>");

?>

 

--------

PLEASE HELP. URGENTLY REQUIRED!!!!

 

Sourav Sengupta

Link to comment
Share on other sites

add the current time stamp to the end of the files before they are moved from the tmp directory to your upload area:

 

$currentTime = time():
@move_uploaded_file( $file_Temp , $uploadFile . "_" . $currentTime); 

 

Its comming as error.

Do I have to add something in the html form? I am sorry. I am new. please help

Thanks for your reply.

 

Link to comment
Share on other sites

add the current time stamp to the end of the files before they are moved from the tmp directory to your upload area:

 

$currentTime = time():
@move_uploaded_file( $file_Temp , $uploadFile . "_" . $currentTime); 

 

Its comming as error.

Do I have to add something in the html form? I am sorry. I am new. please help

Thanks for your reply.

 

Thanks ..... ITS WORKING!!!!!!!

THANKS GUYS

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.