Forum Moderators: coopster

Message Too Old, No Replies

File Uploading - generating unique name

         

adammc

11:00 pm on Sep 10, 2005 (gmt 0)

10+ Year Member



Hi folks,

How can i change the name of the file that people upload?
I am running a job search site and am having problems as people are uploading their resume and all trying to call it resume.doc

Of course the other users file is being overwritten!

Any help would be GREATLY appreciated.


// Show errors, if any
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);


// Recieving variables from send-test.php
$CompanyEmail = $_POST['CompanyEmail'];
$CompanyName = $_POST['CompanyName'];
$job_id = $_POST['job_id'];
$position = $_POST['position'];
$ename = $_POST['ename'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$zip = $_POST['zip'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$phone2 = $_POST['phone2'];
$job_seeker_email = $_POST['job_seeker_email'];
$cover_letter = addslashes($_POST['cover_letter']);

$CV_name = addslashes($_FILES['CV']['name']);
$CV_tmp_name = addslashes($_FILES['CV']['tmp_name']);
$date = $_POST['date'];

// Validate form
if ((!$cover_letter))
{

Print "<br><br><font font size='2' face='Verdana' color='#000099'><b>Oops</b></font><font font size='2' face='Verdana' color='black'>, you forgot to write a cover letter.<br><br>This is an important step in the application process.<br>Please go back and try again.</font>";

}
else
{

// Notify the employer by email
$from = "From: $contactemail";
$to = "$CompanyEmail";

/* subject */
$subject = "New job applicant";

/* message */
$message =
"Hello $CompanyName,\n\nA JobSeeker has applied for your job vacancy on example.\n\nPosition - $position\nJob ID - $job_id\nApplicant Name - $fname $lname\n\nTo view the applicant`s resume, please login to www.example.com using your username/pass.\nOn the Employers Menu, click Manage Jobs.\nThen just click on the link \"View Most Recent Applicants\" for the relevant job offer.\n\nThank you for using example\nhttp://www.example.com
";

/* and now mail it */
mail($to, $subject, $message, $from);

/* the name of the folder - change to fit your needs */
$folder = "cvs/";

//First you move uploaded file into folder
move_uploaded_file($CV_tmp_name,$folder.$CV_name);

//Inserting data into the database
$q = "insert into applications set
date = '$date',
job_id = '$_POST[job_id]',
position = '$_POST[position]',
emp_uname = '$ename',
CompanyName = '$CompanyName',
CompanyEmail = '$CompanyEmail',
job_uname = '$uname',
first_name = '$fname',
last_name = '$lname',
address = '$address',
city = '$city',
state = '$state',
zip = '$zip',
country = '$country',
phone = '$phone',
phone2 = '$phone2',
job_seeker_email = '$job_seeker_email',
cover_letter = '$cover_letter',
cv = '$CV_name'
";
$r = mysql_query($q) or die(mysql_error());

echo "<br><br><font face='Trebuchet MS' size='2' color='#000099'>
<b>Thank you $fname</font></b>
<br><font face='Trebuchet MS' size='2' color='black'>
Your application has been successfully sent to $CompanyName.
<br><br><br>
<a href='http://www.example.com/jobseekers/index.php'><font face='Trebuchet MS' size='2' color='#000099'>Click Here</font></a> to go back to the home page.

<br><br>";

}

?>

[edited by: jatar_k at 3:59 pm (utc) on Sep. 12, 2005]
[edit reason] removed site specifics [/edit]

dreamcatcher

11:28 pm on Sep 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi adammc,

Change this line:

$CV_name = addslashes($_FILES['CV']['name']);

to this:

$CV_name = time() . '_' . addslashes($_FILES['CV']['name']);

This will add a timestamp to the name.

dc

adammc

11:44 pm on Sep 10, 2005 (gmt 0)

10+ Year Member



Dreamcatcher,

THANK YOU!
It works a treat :)

adammc

3:58 am on Sep 11, 2005 (gmt 0)

10+ Year Member



I have just found a BIG problem with the code :(

The users of my site do not have to upload a file, those that dont will automatically get a timestamp in the file field stored in the database :(

The file name that was uploaded or "Not Uploaded" is being shown on a page, available for download.

Can i use an if else or something like that with the timestamp? So that if they dont upload a file nothing goes into the file field in the database?


// Recieving variables from send.php
$CompanyEmail = $_POST['CompanyEmail'];
$CompanyName = $_POST['CompanyName'];
$job_id = $_POST['job_id'];
$position = $_POST['position'];
$ename = $_POST['ename'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$zip = $_POST['zip'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$phone2 = $_POST['phone2'];
$job_seeker_email = $_POST['job_seeker_email'];
$cover_letter = addslashes($_POST['cover_letter']);
$CV_name = time() . '_' . addslashes($_FILES['CV']['name']);
$CV_tmp_name = addslashes($_FILES['CV']['tmp_name']);
$date = $_POST['date'];

// Validate form
if ((!$cover_letter))
{

Print "<br><br><font font size='2' face='Verdana' color='#000099'><b>Oops</b></font><font font size='2' face='Verdana' color='black'>, you forgot to write a cover letter.<br><br>This is an important step in the application process.<br>Please go back and try again.</font>";

}
else
{

// Notify the employer by email
$from = "From: $contactemail";
$to = "$CompanyEmail";

/* subject */
$subject = "New job applicant";

/* message */
$message =
"Hello $CompanyName,\n\nA JobSeeker has applied for your job vacancy on I have just found a BIG problem with the code :(

The users of my site do not have to upload a file, those that dont will automatically get a timestamp in the file field stored in the database :(

The file name that was uploaded or "Not Uploaded" is being shown on a page, available for download.

Can i use an if else or something like that with the timestamp? So that if they dont upload a file nothing goes into the file field in the database?

[code]
// Recieving variables from send.php
$CompanyEmail = $_POST['CompanyEmail'];
$CompanyName = $_POST['CompanyName'];
$job_id = $_POST['job_id'];
$position = $_POST['position'];
$ename = $_POST['ename'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$zip = $_POST['zip'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$phone2 = $_POST['phone2'];
$job_seeker_email = $_POST['job_seeker_email'];
$cover_letter = addslashes($_POST['cover_letter']);
$CV_name = time() . '_' . addslashes($_FILES['CV']['name']);
$CV_tmp_name = addslashes($_FILES['CV']['tmp_name']);
$date = $_POST['date'];

// Validate form
if ((!$cover_letter))
{

Print "<br><br><font font size='2' face='Verdana' color='#000099'><b>Oops</b></font><font font size='2' face='Verdana' color='black'>, you forgot to write a cover letter.<br><br>This is an important step in the application process.<br>Please go back and try again.</font>";

}
else
{

// Notify the employer by email
$from = "From: $contactemail";
$to = "$CompanyEmail";

/* subject */
$subject = "New job applicant from Work In Cairns";

/* message */
$message =
"Hello $CompanyName,\n\nA JobSeeker has applied for your job vacancy on Work In Cairns.\n\nPosition - $position\nJob ID - $job_id\nApplicant Name - $fname $lname\n\nTo view the applicant`s resume, please login to www.workincairns.com using your username/pass.\nOn the Employers Menu, click Manage Jobs.\nThen just click on the link \"View Most Recent Applicants\" for the relevant job offer.\n\nThank you for using Work In Cairns\nhttp://www.workincairns.com
";

/* and now mail it */
mail($to, $subject, $message, $from);

/* the name of the folder - change to fit your needs */
$folder = "cvs/";

//First you move uploaded file into folder
move_uploaded_file($CV_tmp_name,$folder.$CV_name);

//Inserting data into the database
$q = "insert into applications set
date = '$date',
job_id = '$_POST[job_id]',
position = '$_POST[position]',
emp_uname = '$ename',
CompanyName = '$CompanyName',
CompanyEmail = '$CompanyEmail',
job_uname = '$uname',
first_name = '$fname',
last_name = '$lname',
address = '$address',
city = '$city',
state = '$state',
zip = '$zip',
country = '$country',
phone = '$phone',
phone2 = '$phone2',
job_seeker_email = '$job_seeker_email',
cover_letter = '$cover_letter',
cv = '$CV_name'
";
$r = mysql_query($q) or die(mysql_error());

.\n\nPosition - $position\nJob ID - $job_id\nApplicant Name - $fname $lname\n\nTo view the applicant`s resume, please login to www.example.com using your username/pass.\nOn the Employers Menu, click Manage Jobs.\nThen just click on the link \"View Most Recent Applicants\" for the relevant job offer.\n\nThank you for using \nhttp://www.example.com
";

/* and now mail it */
mail($to, $subject, $message, $from);

/* the name of the folder - change to fit your needs */
$folder = "cvs/";

//First you move uploaded file into folder
move_uploaded_file($CV_tmp_name,$folder.$CV_name);

//Inserting data into the database
$q = "insert into applications set
date = '$date',
job_id = '$_POST[job_id]',
position = '$_POST[position]',
emp_uname = '$ename',
CompanyName = '$CompanyName',
CompanyEmail = '$CompanyEmail',
job_uname = '$uname',
first_name = '$fname',
last_name = '$lname',
address = '$address',
city = '$city',
state = '$state',
zip = '$zip',
country = '$country',
phone = '$phone',
phone2 = '$phone2',
job_seeker_email = '$job_seeker_email',
cover_letter = '$cover_letter',
cv = '$CV_name'
";
$r = mysql_query($q) or die(mysql_error());

[/code]

[edited by: jatar_k at 4:01 pm (utc) on Sep. 12, 2005]
[edit reason] removed site specifics [/edit]

dreamcatcher

8:20 am on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem.

Change this:

cv = '$CV_name'

to this:

cv = '".($_FILES['CV']['name']? $CV_name : '')."'

dc

adammc

12:47 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



dreamcatcher,
Thank you sooooo much! :)

dreamcatcher

5:44 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. :)

dc

adammc

11:08 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



How would I cut out using the filename in the naming process and instead use timestamp_date (get server date)

Any help would be greatly appreciated :)