?
Solved

change name file before record in server

Posted on 2016-10-27
5
Medium Priority
?
61 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 58

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 58

Expert Comment

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

Author Closing Comment

by:eduardo12fox
ID: 41869509
Thanks
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses

777 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