Solved

change name file before record in server

Posted on 2016-10-27
5
36 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 109

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 54

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 54

Expert Comment

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

Author Closing Comment

by:eduardo12fox
ID: 41869509
Thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 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

14 Experts available now in Live!

Get 1:1 Help Now