Solved

change name file before record in server

Posted on 2016-10-27
5
26 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
  • 2
  • 2
5 Comments
 
LVL 108

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 51

Accepted Solution

by:
Julian Hansen earned 500 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 51

Expert Comment

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

Author Closing Comment

by:eduardo12fox
ID: 41869509
Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now