[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

change name file before record in server

Posted on 2016-10-27
5
Medium Priority
?
68 Views
Last Modified: 2016-11-01
I need change name of file before record file in my server. How change this in my code?

$arquivo_curriculo=$_FILES['upload2']['name'];

if(!empty($arquivo_curriculo)) {


$pasta_upload = 'pasta_curriculo_enviado/';


$caminho_completo = $pasta_upload . basename($_FILES['upload2']['name']);


    if (move_uploaded_file($_FILES['upload2']['tmp_name'], $caminho_completo)) {


        $file_curriculo =$caminho_completo; 

    }


 }

 $arquivo_perfil=$_FILES['upload1']['name'];


 if(!empty($arquivo_perfil)) {

$pasta_upload = 'pasta_foto_candidato/';


$caminho_completo = $pasta_upload . basename($_FILES['upload1']['name']);


    if (move_uploaded_file($_FILES['upload1']['tmp_name'], $caminho_completo)) {

        echo "arquivo enviado com sucesso";

        $image = new Resize_Image; 

        $image->new_width = 140;

        $image->new_height = 140; 

        $image->image_to_resize = $caminho_completo; 

        $image->ratio = true; 

        $image->new_image_name = $arquivo_perfil;

        $image->save_folder = $pasta_upload;

        $process = $image->resize();

        unlink($caminho_completo);

        $file_perfil= $process['new_file_path'];


   }


 } 

Open in new window

0
Comment
Question by:eduardo12fox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41862269
Uhh, OK.  What do you want to change the name to?  Put that name string into $caminho_completo.  Or maybe into $arquivo_perfil.  Both look like file names to me.

If you're new to PHP and you're looking to learn the language, this might be a good starting point.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
0
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41862313
I am assuming this is what you want to change
$caminho_completo = $pasta_upload . basename($_FILES['upload2']['name']);
    if (move_uploaded_file($_FILES['upload2']['tmp_name'], $caminho_completo)) {
        $file_curriculo =$caminho_completo; 
    }
 }

Open in new window

$caminho_completo is being set to the path pasta_curriculo_enviado/ pre-fixed to the original name of the file that was uploaded.
I am assuming you want to save this as another name.

You will need to decide how you are going to come up with the name - for this example I will just use the date.
// GET THE NEW FILENAME BASED ON THE 
// DESTINATION FOLDER
// GET THE EXTENSION
$pi = pathinfo($_FILES['upload2']['name']);
$filename = getFilename($pasta_upload,$pi['extension']);

// ADD TO THE PATH
$caminho_completo = $pasta_upload . $filename;
if (move_uploaded_file($_FILES['upload2']['tmp_name'], $caminho_completo)) {
  $file_curriculo =$caminho_completo; 
}

// getFilename TAKES A path AND ext
// IT USES THESE IN CONJUNCTION WITH THE DATE
// TO FIND A UNIQUE FILENAME IN THE SPECIFIED
// FOLDER. IF A FILE WITH THAT NAME ALREADY EXISTS
// A SEQUENCE NUMBER IS ADDED UNTIL A UNIQUE
// NAME IS FOUND
function getFilename($path, $ext) 
{
   // CREATE THE BASE TO USE IF WE HAVE TO
   // LOOP. BASED ON CURRENT DATE
   $base = date('Ymd');

   // INITIAL FILE NAME TO LOOK FOR
   $filename = $base . ".{$ext}";

   // START SEQ AT 1
   $count=1;

   // LOOK FOR FILENAMES UNTIL NOT FOUND
   // ADD SEQ NUMBER ON EACH ITERATION
   // $path IS PREPENDED TO $filename TO
   // GIVE FULL SEARCH
   while(file_exists($path . $filename)) {
       // IF WE GET HERE FILENAME IS IN USE SO
       // CREATE A NEW FILENAME WITH INCREASED
       // SEQUENCE NUMBER
       $filename = $base . '.' . ($count++) . ".{$ext}";
   }

   // WHEN WE GET HERE FILENAME IS UNIQUE
   return $filename;
}

Open in new window

0
 

Author Comment

by:eduardo12fox
ID: 41862618
Perfect Julian Hansen Thanks man
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41862891
You are welcome.
0
 

Author Closing Comment

by:eduardo12fox
ID: 41869509
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question