Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Rinominare file

  1. #1
    Utente di HTML.it L'avatar di Mister
    Registrato dal
    Jan 2001
    Messaggi
    251

    Rinominare file

    Sono alla prime armi,

    avendo una form con diversi campi: nome, cognome e upload allegato, è possibile che il file in upload venga rinominato con i risultati dei campi nome e cognome?

    ES: nome_cognome_fileallegato.pdf?

    Sotto la parte di codice dell'upload.

    Grazie.



    Codice PHP:
    /**
     * Uploads a file for a particular form submission field.
     *
     * This is to be called AFTER the submission has already been added to the database - it uploads the
     * file to the specified folder then updates the database record.
     *
     * @param integer $form_id The unique form ID.
     * @param integer $submission_id A unique submission ID.
     * @param integer $field_id A unique field ID.
     * @param array $fileinfo An index from the $_FILES array (containing all data about the file)
     * @return array Returns array with indexes:

     *               [0]: true/false (success / failure)

     *               [1]: message string

     *               [2]: If success, the filename of the uploaded file
     */
    function ft_upload_file($form_id$submission_id$field_id$fileinfo)
    {
        global 
    $g_table_prefix$LANG;

        
    // get the column name and upload folder for this field
        
    $field_info ft_get_form_field($field_id);
        
    $col_name    $field_info['col_name'];
        
    $file_upload_dir $field_info['file_upload_dir'];
        
    $file_upload_url $field_info['file_upload_url'];
        
    $file_upload_max_size $field_info['file_upload_max_size'];
        
    $file_upload_types $field_info['file_upload_types'];

        
    // if the column name wasn't found, the $field_id passed in was invalid. Return false.
        
    if (empty($col_name))
            return array(
    false$LANG["notify_submission_no_field_id"]);

        
    // convert any whitespace chars in filename to underscores [list may be expanded later]
        
    $filename     preg_replace("/\s+/""_"$fileinfo["name"]);
        
    $tmp_filename $fileinfo["tmp_name"];
        
    $filesize     $fileinfo["size"];

        
    // check file size
        
    if ($filesize $file_upload_max_size)
            return array(
    false$LANG["notify_file_too_large"]);

        
    // check upload folder is valid and writable
        
    if (!is_dir($file_upload_dir) || !is_writable($file_upload_dir))
            return array(
    false$LANG["notify_invalid_field_upload_folder"]);

        
    // check file extension is valid. Note: this is "dumb" - it just tests for the file extension, not
        // the actual file type based on it's header info [this is done because I want to allow users to permit
        // uploading of any file types, and I can't know the headers of all of them]
        
    $is_valid_extension true;
        if (!empty(
    $file_upload_types))
        {
            
    $is_valid_extension false;
            
    $raw_extensions explode(","$file_upload_types);

            foreach (
    $raw_extensions as $ext)
            {
                
    // remove whitespace and periods
                
    $clean_extension str_replace("."""trim($ext));

                if (
    preg_match("/$clean_extension$/"$filename))
                    
    $is_valid_extension true;
            }
        }

        
    // all checks out!
        
    if ($is_valid_extension)
        {
            
    // check for duplicate filenames and get a unique name
            
    $unique_filename ft_check_duplicate_filename($file_upload_dir$filename);

            
    // copy file to uploads folder and remove temporary file
            
    if (rename($tmp_filename"$file_upload_dir/$unique_filename"))
            {
                @
    chmod("$file_upload_dir/$unique_filename"0777);

                
    // update the database
                
    $query "
                    UPDATE 
    {$g_table_prefix}form_{$form_id}
                    SET    
    $col_name = '$unique_filename'
                    WHERE  submission_id = 
    $submission_id
                                 "
    ;

                
    $link ft_db_connect();
                
    mysql_query($query);
                
    ft_db_disconnect($link);

                return array(
    true$LANG["notify_file_uploaded"], $unique_filename);
            }
            else
                return array(
    false$LANG["notify_file_not_uploaded"]);
        }

        
    // not a valid extension. Inform the user
        
    else
            return array(
    false$LANG["notify_unsupported_file_extension"]);


  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2008
    Messaggi
    270
    esiste una funziona apposta in php e si chiama rename() quindi

    codice:
    rename('path/nome_file', 'path/$nome_$cognome');

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.