Solved

change name file before record in server

Posted on 2016-10-27
5
49 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 110

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 56

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 56

Expert Comment

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

Author Closing Comment

by:eduardo12fox
ID: 41869509
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
$_SERVER Variable question 31 56
php error 27 58
Echo'd values in dropdowns 6 30
how to use jquery variable in mysql query 20 43
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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