Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Rinominare un file PHP aggiungendo al suo nome "1"

    // Max size PER file in KB
    $max_file_size="70000";

    // Max size for all files COMBINED in KB
    $max_combined_size="204800";

    //Maximum file uploades at one time
    $file_uploads="1";

    //The name of your website
    $websitename="CTS CORP";

    // Full browser accessable URL to where files are accessed. With trailing slash.
    $full_url="www.nomesito.it";

    // Path to store files on your server If this fails use $fullpath below. With trailing slash.
    $folder="./uploads/";

    // Use random file names? true=yes (recommended), false=use original file name.
    // Random names will help prevent files being denied because a file with that name already exists.
    $random_name=true;

    // Types of files that are acceptiable for uploading. Keep the array structure.
    $allow_types=array("flv","gif","png","zip","rar"," txt","doc");

    // Only use this variable if you wish to use full server paths. Otherwise leave this empty. With trailing slash.
    $fullpath="";

    //Use this only if you want to password protect your upload form.
    $password="";

    /*
    //================================================== ==============================
    * ! ATTENTION !
    //================================================== ==============================
    : Don't edit below this line.
    */

    // Initialize variables
    $password_hash=md5($password);
    $error="";
    $success="";
    $display_message="";
    $file_ext=array();
    $password_form="";

    // Function to get the extension a file.
    function get_ext($key) {
    $key=strtolower(substr(strrchr($key, "."), 1));
    $key=str_replace("jpeg","jpg",$key);
    return $key;
    }

    // Filename security cleaning. Do not modify.
    function cln_file_name($string) {
    $cln_filename_find=array("/\.[^\.]+$/", "/[^\d\w\s-]/", "/\s\s+/", "/[-]+/", "/[_]+/");
    $cln_filename_repl=array("", ""," ", "-", "_");
    $string=preg_replace($cln_filename_find, $cln_filename_repl, $string);
    return trim($string);
    }

    // If a password is set, they must login to upload files.
    If($password) {

    //Verify the credentials.
    If($_POST['verify_password']==true) {
    If(md5($_POST['check_password'])==$password_hash) {
    setcookie("phUploader",$password_hash);
    sleep(1); //seems to help some people.
    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
    exit;
    }
    }

    //Show the authentication form
    If($_COOKIE['phUploader']!=$password_hash) {
    $password_form="<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">\n";
    $password_form.="<table align=\"center\" class=\"table\">\n";
    $password_form.="<tr>\n";
    $password_form.="<td width=\"100%\" class=\"table_header\" colspan=\"2\">Password Required</td>\n";
    $password_form.="</tr>\n";
    $password_form.="<tr>\n";
    $password_form.="<td width=\"35%\" class=\"table_body\">Enter Password:</td>\n";
    $password_form.="<td width=\"65%\" class=\"table_body\"><input type=\"password\" name=\"check_password\" /></td>\n";
    $password_form.="</tr>\n";
    $password_form.="<td colspan=\"2\" align=\"center\" class=\"table_body\">\n";
    $password_form.="<input type=\"hidden\" name=\"verify_password\" value=\"true\">\n";
    $password_form.="<input type=\"submit\" value=\" Verify Password \" />\n";
    $password_form.="</td>\n";
    $password_form.="</tr>\n";
    $password_form.="</table>\n";
    $password_form.="</form>\n";
    }

    } // If Password

    // Dont allow submit if $password_form has been populated
    If(($_POST['submit']==true) AND ($password_form=="")) {

    //Tally the size of all the files uploaded, check if it's over the ammount.
    If(array_sum($_FILES['file']['size']) > $max_combined_size*1024) {

    $error.="FAILED: All Files REASON: Combined file size is to large.
    ";

    // Loop though, verify and upload files.
    } Else {

    // Loop through all the files.
    For($i=0; $i <= $file_uploads-1; $i++) {

    // If a file actually exists in this key
    If($_FILES['file']['name'][$i]) {

    //Get the file extension
    $file_ext[$i]=get_ext($_FILES['file']['name'][$i]);

    //ASSEGNO IL NUMERO UNO AL FILE CARICATO




    If($random_name){
    $file_name[$i]=1;
    } Else {
    $file_name[$i]=cln_file_name($_FILES['file']['name'][$i]);
    }

    // Check for blank file name
    If(str_replace(" ", "", $file_name[$i])=="") {

    $error.= "FAILED: ".$_FILES['file']['name'][$i]." REASON: Blank file name detected.
    ";

    //Check if the file type uploaded is a valid file type.
    } ElseIf(!in_array($file_ext[$i], $allow_types)) {

    $error.= "FAILED: ".$_FILES['file']['name'][$i]." REASON: Invalide file type.
    ";

    //Check the size of each file
    } Elseif($_FILES['file']['size'][$i] > ($max_file_size*1024)) {

    $error.= "FAILED: ".$_FILES['file']['name'][$i]." REASON: File to large.
    ";

    // Check if the file already exists on the server..
    } Elseif(file_exists($folder.$file_name[$i].".".$file_ext[$i])) {

    int rename($file_name[$i], $file_name[$i]++);

    } Else {

    If(move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i].".".$file_ext[$i])) {

    $success.="SUCCESS: ".$_FILES['file']['name'][$i]."
    ";
    $success.="URL: <a href=\"".$full_url.$file_name[$i].".".$file_ext[$i]."\" target=\"_blank\">".$full_url.$file_name[$i].".".$file_ext[$i]."</a>

    ";

    } Else {
    $error.="FAILED: ".$_FILES['file']['name'][$i]." REASON: General upload failure.
    ";
    }

    }

    } // If Files

    } // For

    } // Else Total Size

    If(($error=="") AND ($success=="")) {
    $error.="FAILED: No files selected
    ";
    }

    $display_message=$success.$error;

    } // $_POST AND !$password_form

    /*
    //================================================== ==============================
    * Start the form layout
    //================================================== ==============================
    :- Please know what your doing before editing below. Sorry for the stop and start php.. people requested that I use only html for the form..
    */
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Language" content="en-us" />
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title><?php echo $websitename; ?> - Powered By phUploader</title>

    <style type="text/css">
    body{
    background-color:#FFFFFF;
    font-family: Verdana, Arial, sans-serif;
    font-size: 12pt;
    color: #000000;
    }

    .message {
    font-family: Verdana, Arial, sans-serif;
    font-size: 11pt;
    color: #000000;
    background-color:#EBEBEB;
    }

    a:link, a:visited {
    text-decoration:none;
    color: #000000;
    }

    a:hover {
    text-decoration:none;
    color: #000000;
    }

    .table {
    border-collapse:collapse;
    border:1px solid #000000;
    width:450px;
    }

    .table_header {
    border:1px solid #000000;
    background-color:#C03738;
    font-family: Verdana, Arial, sans-serif;
    font-size: 11pt;
    font-weight:bold;
    color: #FFFFFF;
    text-align:center;
    padding:2px;
    }

    .upload_info {
    border:1px solid #000000;
    background-color:#EBEBEB;
    font-family: Verdana, Arial, sans-serif;
    font-size: 8pt;
    color: #000000;
    padding:4px;
    }

    .table_body {
    border:1px solid #000000;
    background-color:#EBEBEB;
    font-family: Verdana, Arial, sans-serif;
    font-size: 10pt;
    color: #000000;
    padding:2px;
    }

    .table_footer {
    border:1px solid #000000;
    background-color:#C03738;
    text-align:center;
    padding:2px;
    }

    input,select,textarea {
    font-family: Verdana, Arial, sans-serif;
    font-size: 10pt;
    color: #000000;
    background-color:#AFAEAE;
    border:1px solid #000000;
    }

    .copyright {
    border:0px;
    font-family: Verdana, Arial, sans-serif;
    font-size: 9pt;
    color: #000000;
    text-align:right;
    }

    form {
    padding:0px;
    margin:0px;
    }
    </style>

    <?
    If($password_form) {

    Echo $password_form;

    } Else {
    ?>

    <form action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data" name="phuploader">
    <table align="center" class="table">
    <tr>
    <td class="table_header" colspan="2"><?=$websitename;?> </td>
    </tr>

    <?If($display_message){?>
    <tr>
    <td colspan="2" class="message">


    <?=$display_message;?>


    </td>
    </tr>
    <?}?>

    <tr>
    <td colspan="2" class="upload_info">
    Allowed Types: <?=implode($allow_types, ", ");?>

    Max size per file: <?=$max_file_size?>kb.

    Max size for all files combined: <?=$max_combined_size?>kb.

    </td>
    </tr>
    <?For($i=0;$i <= $file_uploads-1;$i++) {?>
    <tr>
    <td class="table_body" width="20%">Select File: </td>
    <td class="table_body" width="80%"><input type="file" name="file[]" size="30" /></td>
    </tr>
    <?}?>
    <tr>
    <td colspan="2" align="center" class="table_footer">
    <input type="hidden" name="submit" value="true" />
    <input type="submit" value=" Upload File(s) " />
    <input type="reset" name="reset" value=" Reset Form " onclick="window.location.reload(true);" />
    </td>
    </tr>
    </table>
    </form>

    <?}//Please leave this here.. it really dosen't make people hate you or make your site look bad.. ?>
    <table class="table" style="border:0px;" align="center">
    <tr>
    <td><div class="copyright">&copy;phUploader</div></td>
    </tr>
    </table>
    </body>


    Questo codice carica un file nella cartella uploads e gli assegna il numero 1. Adesso ho un problema. Se la cartella in cui viene caricato il file (uploads), contiene già il file chiamato 1, dovrebbe rinominarlo assegnando al file il numero immediatamente più grande di quello già esistente. (Es. se è presente 1 --> rinomina ->2 ; se è presente 2 rinomina 3 ecc)

    Come posso fare? Sto impazzendo letteralmente...
    Ho bisogno che i file caricati abbiano un numero crescente a partire da 1.. poi 2...poi 3...

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    3,709
    a parte che hai postato un po' troppo codice :) ...

    potersti fare così: crea un file "counter.txt" (anche nella stessa cartella degli altri file) con permessi di lettura/scrittura che contiene il solo carattere "0" (zero), poi quando assegni il nome anzichè:

    Codice PHP:
    If($random_name){
    $file_name[$i]=1;
    } Else {
    $file_name[$i]=cln_file_name($_FILES['file']['name'][$i]);

    fai:

    Codice PHP:
    $counterfile '........./counter.txt'// vedi un po' se indicare una cartella...
    If($random_name){
    $counter file_get_contents($counterfile);
    file_put_contents($counterfile, ++$counter);
    $file_name[$i]=$counter;
    } Else {
    $file_name[$i]=cln_file_name($_FILES['file']['name'][$i]);


  3. #3

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    3,709
    il "problema" è tener "traccia" del contatore per sapere il "prossimo" file come dev'essere denominato... naturalmente ci sono tante soluzioni...

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.