Jump to content

rename file upload in php


ainbila
 Share

Recommended Posts

i try this code to rename file when upload , the file name is rename in the folder or physical file .But in the database , it display 'array' not the name of file .How to save file name in the database ?

 

$targetDirg= "folder/pda-semakan/gambar/";
 if($_FILES['gambar']['size'] == 0 && $_FILES['gambar']['name'] == ""){
   $fileNameg = $gambar;
 }else{
$fileNameg =explode(".",$_FILES['gambar']['name']);
$newfilename= $stIC.'.'.end($fileNameg);

 
 $moveg =move_uploaded_file($_FILES["gambar"]["tmp_name"], $targetDirg.$newfilename);
 }

 

Link to comment
Share on other sites

    $file_name = $_FILES['gambar']['name']; // Original File Name:
    $file_size = $_FILES['gambar']['size'];
    $file_tmp = $_FILES['gambar']['tmp_name']; // Temporary file for the directory:
    $file_type = $_FILES['gambar']['type'];
    $file_ext = strtolower(pathinfo($_FILES['gambar']['name'], PATHINFO_EXTENSION));

    /*
     * Set the path to the correct folder
     */
    $dir_path = 'folder/pda-semakan/gambar/';

    /*
     * Create unique name for file.
     */
    $new_file_name = $dir_path . 'file-example-' . time() . '.' . $file_ext;

    move_uploaded_file($file_tmp, $new_file_name);

I didn't test this out, but you would do something like the above. Note $new_file_name would also store the path in the database table, so if you just want the just file then just out the $dir_path after saving the file to the directory.

Edited by Strider64
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.