Visualizzazione risultati 1 fino 7 di 7

Discussione: Script per caricare e cancellare files sul server

  1. #1
    Guest

    Question Script per caricare e cancellare files sul server

    Salve a tutti,

    in rete ho trovato questo script che permette di caricare e cancellare files in una specifica cartella del sito.
    Il nome della cartella di default risulta essere files. Il programmatore "promette" che è possibile cambiare in nome della cartella, ma a quanto pare una volta cambiato il nome non è più possibile cancellare i files, ma solo caricarli.

    E' possibile cambiare il nome della cartella qui:
    Codice PHP:
    public $upload_dir = 'files/'; // Name of the upload directory - create it yourself and chmod it to 777
    A metà script circa, c'è questa riga:
    Codice PHP:
    $upload_url = $url_dir."/files/";
    Vorrei mettere 2 demo (una con la cartella files e uno con un altro nome della cartella) ma temo sia un'operazione poco sicura.

    Il mio livello di conoscenza di php è nullo, spero possiate darmi una mano.

    Codice PHP:
    <?
    ###############################
    ## Sava's Simple Upload v2.0 ##
    ###############################

    // v2.0 Changes
    // Re-wrote code to use a class
    // No more notices when register globals is turned off


    class Upload {

    ## User edited variables ##

    public $upload_dir = 'files/'; // Name of the upload directory - create it yourself and chmod it to 777
    public $deletable = true; // Are files deletable ? true or false
    public $file_extensions = array('.zip','.jpg','jpeg','.png','.gif','.doc'); // File extensions allowed to be uploaded
    public $max_size = 200000000; // Maximum file size

    ## Stop editing passed this point unless you know what you are doing ##

    function index() {
    if (isset(
    $_REQUEST['delete'])) {
    $this->delete($_REQUEST['delete']);
    }
    if (isset(
    $_REQUEST['action']) && $_REQUEST['action'] == 'upload') {
    $this->do_upload();
    } else {
    echo
    '<!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" dir="ltr" lang="en-US">
    <head>
    <title>Sava'
    s Place - Simple Upload</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <
    style type="text/css">body {margin: 0 auto;padding: 0;color: #3C475B;font-family: "segoe ui", tahoma, sans-serif;font-size: small;}a:link { color: #0a4e96; }a:visited { color: #0a4e96; }a:hover { color: #333; }a:active { color: #000; }#content { margin: 0 auto; padding: 1em 2em 1em 2em;}#content table { border:1px solid #ccc; min-width:600px; text-align:center;}#header h1 { margin: 0; padding: 25px 0 0 0;}#header p { font-weight: bold; margin: 0; padding: 0 0 80px 0;}#footer { border-top: 1px dashed #ccc; font-size: 11px; color: #999; align:center;}</style>
    </head>
    <
    body>
    <
    div id="content">
    <
    div id="header">
    <
    h1><a href="http://savasplace.com/2009/01/savas-single-file-upload-script/">Sava's Simple Upload</a></h1>
    <p>Single File Upload Script</p>
    </div>'
    ;
    $this->show_message();
    $this->show_upload_form();
    $this->show_files();
    if(isset(
    $_GET['message'])) {echo '<meta http-equiv="refresh" content="5; '.$_SERVER['PHP_SELF'].'"';}
    echo
    '<p id="footer">© 2009 Sava\'s Place. All rights reserved</p></div></body></html>';
    }
    }

    function
    do_upload() {
    $site_name = $_SERVER['HTTP_HOST'];
    $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    $upload_dir = $this->upload_dir;
    $upload_url = $url_dir.$this->upload_dir;
    $temp_name = $_FILES['userfile']['tmp_name'];
    $file_name = $_FILES['userfile']['name'];
    $file_name = str_replace("\\","",$file_name);
    $file_name = str_replace("'","",$file_name);
    $file_path = $this->upload_dir.$file_name;
    $file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
    $file_type = $_FILES['userfile']['type'];
    if (
    $file_name == "") {
    $message = "Invalid File Name Specified";
    header("Location: ".$url_this.'?message='.$message);
    }
    elseif (
    $_FILES['userfile']['size'] > $this->max_size) {
    $message = "The file size is over 2MB.";
    header("Location: ".$url_this.'?message='.$message);
    }
    elseif (!
    in_array($file_ext, $this->file_extensions)) {
    $message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
    header("Location: ".$url_this.'?message='.$message);
    }
    else {
    $result = move_uploaded_file($temp_name, $file_path);
    if (!
    chmod($file_path,0777)) $message = "change permission to 777 failed."; else $message = ($result)?"$file_name was uploaded successfully." : "Something is wrong with uploading the file.";
    header("Location: ".$url_this.'?message='.$message);
    }
    }

    function
    show_message() {
    if(isset(
    $_REQUEST['message'])) {
    echo
    $_REQUEST['message'];
    }
    }

    function
    show_upload_form() {
    echo
    '<form action="'.$_SERVER['PHP_SELF'].'?action=upload" name="upload" id="upload" enctype="multipart/form-data" method="post">
    Upload File <input type="file" id="userfile" name="userfile" />
    <input type="submit" name="upload" value="Upload" />
    </form>'
    ;
    }

    function
    show_files() {
    $handle=opendir($this->upload_dir);
    $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $upload_url = $url_dir."/files/";
    $filelist = "<h3>Uploaded files</h3>";
    $filelist .= '<table class="table"><tr><th>Name</th><th>Added at</th><th>Actions</th></tr>';
    while (
    $file = readdir($handle)) {
    if(!
    is_dir($file) && !is_link($file)) {
    $filelist .= '<tr><td>'.$file.'</td>';
    $filelist .= '<td>'.date("d-m-Y H:i", filemtime($this->upload_dir.$file))."</td>"."";
    $filelist .= '<td><a style="text-decoration:none; font-weight:bold" href="'.$upload_url.$file.'" target="_blank">View</a> ';
    if (
    $this->deletable) {
    $filelist .= "<a style='text-decoration:none; font-weight:bold' href='?delete=$this->upload_dir".urlencode($file)."' title='delete'>Delete</a></td></tr>";
    } else {
    echo
    '</td>';
    }
    }
    }
    $filelist .= '</table>';
    echo
    $filelist;
    }

    function
    delete($todelete) {
    if (
    strpos($todelete,"/.")>0);
    else if (
    strpos($todelete,$this->upload_dir) === false);
    else if (
    substr($todelete,0,6)==$this->upload_dir) {
    if(
    unlink($todelete)) {
    $message = 'Deleted succesfully';
    header('Location: '.$_SERVER['PHP_SELF'].'?message='.$message);
    }
    }
    }
    }

    // Start your engines
    $upload = new Upload();

    // Go go go
    $upload->index();
    ?>
    Ultima modifica di saitfainder : 04-02-2011 alle ore 12.03.27

  2. #2
    L'avatar di saitfainder
    saitfainder non è connesso Sëniör Stäff
    Data registrazione
    06-12-2002
    Residenza
    Torino
    Messaggi
    8,715

    Predefinito

    Che script difettoso. Ho dato una sistemata:

    Codice PHP:
    <?
    ###############################
    ## Sava's Simple Upload v2.0 ##
    ###############################

    // v2.0 Changes
    // Re-wrote code to use a class
    // No more notices when register globals is turned off


    class Upload {

    ## User edited variables ##

    public $upload_dir = 'files/'; // Name of the upload directory - create it yourself and chmod it to 777
    public $deletable = true; // Are files deletable ? true or false
    public $file_extensions = array('.zip','.jpg','jpeg','.png','.gif','.doc'); // File extensions allowed to be uploaded
    public $max_size = 200000000; // Maximum file size

    ## Stop editing passed this point unless you know what you are doing ##

    function index() {
    if (isset(
    $_REQUEST['delete'])) {
    $this->delete($_REQUEST['delete']);
    }
    if (isset(
    $_REQUEST['action']) && $_REQUEST['action'] == 'upload') {
    $this->do_upload();
    } else {
    echo
    '<!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" dir="ltr" lang="en-US">
    <head>
    <title>Sava\'s Place - Simple Upload</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">body {margin: 0 auto;padding: 0;color: #3C475B;font-family: "segoe ui", tahoma, sans-serif;font-size: small;}a:link { color: #0a4e96; }a:visited { color: #0a4e96; }a:hover { color: #333; }a:active { color: #000; }#content { margin: 0 auto; padding: 1em 2em 1em 2em;}#content table { border:1px solid #ccc; min-width:600px; text-align:center;}#header h1 { margin: 0; padding: 25px 0 0 0;}#header p { font-weight: bold; margin: 0; padding: 0 0 80px 0;}#footer { border-top: 1px dashed #ccc; font-size: 11px; color: #999; align:center;}</style>
    </head>
    <body>
    <div id="content">
    <div id="header">
    <h1><a href="http://savasplace.com/2009/01/savas-single-file-upload-script/">Sava\'s Simple Upload</a></h1>
    <p>Single File Upload Script</p>
    </div>'
    ;
    $this->show_message();
    $this->show_upload_form();
    $this->show_files();
    if(isset(
    $_GET['message'])) {echo '<meta http-equiv="refresh" content="5; '.$_SERVER['PHP_SELF'].'"';}
    echo
    '<p id="footer">© 2009 Sava\'s Place. All rights reserved</p></div></body></html>';
    }
    }

    function
    do_upload() {
    $site_name = $_SERVER['HTTP_HOST'];
    $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    $upload_dir = $this->upload_dir;
    $upload_url = $url_dir.$this->upload_dir;
    $temp_name = $_FILES['userfile']['tmp_name'];
    $file_name = $_FILES['userfile']['name'];
    $file_name = str_replace("\\","",$file_name);
    $file_name = str_replace("'","",$file_name);
    $file_path = $this->upload_dir.$file_name;
    $file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
    $file_type = $_FILES['userfile']['type'];
    if (
    $file_name == "") {
    $message = "Invalid File Name Specified";
    header("Location: ".$url_this.'?message='.$message);
    }
    elseif (
    $_FILES['userfile']['size'] > $this->max_size) {
    $message = "The file size is over 2MB.";
    header("Location: ".$url_this.'?message='.$message);
    }
    elseif (!
    in_array($file_ext, $this->file_extensions)) {
    $message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
    header("Location: ".$url_this.'?message='.$message);
    }
    else {
    $result = move_uploaded_file($temp_name, $file_path);
    if (!
    chmod($file_path,0777)) $message = "change permission to 777 failed."; else $message = ($result)?"$file_name was uploaded successfully." : "Something is wrong with uploading the file.";
    header("Location: ".$url_this.'?message='.$message);
    }
    }

    function
    show_message() {
    if(isset(
    $_REQUEST['message'])) {
    echo
    $_REQUEST['message'];
    }
    }

    function
    show_upload_form() {
    echo
    '<form action="'.$_SERVER['PHP_SELF'].'?action=upload" name="upload" id="upload" enctype="multipart/form-data" method="post">
    Upload File <input type="file" id="userfile" name="userfile" />
    <input type="submit" name="upload" value="Upload" />
    </form>'
    ;
    }

    function
    show_files() {
    $handle=opendir($this->upload_dir);
    $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $upload_url = $url_dir."/".$this->upload_dir;
    $filelist = "<h3>Uploaded files</h3>";
    $filelist .= '<table class="table"><tr><th>Name</th><th>Added at</th><th>Actions</th></tr>';
    while (
    $file = readdir($handle)) {
    if(!
    is_dir($file) && !is_link($file)) {
    $filelist .= '<tr><td>'.$file.'</td>';
    $filelist .= '<td>'.date("d-m-Y H:i", filemtime($this->upload_dir.$file))."</td>"."";
    $filelist .= '<td><a style="text-decoration:none; font-weight:bold" href="'.$upload_url.$file.'" target="_blank">View</a> ';
    if (
    $this->deletable) {
    $filelist .= "<a style='text-decoration:none; font-weight:bold' href='?delete=$this->upload_dir".urlencode($file)."' title='delete'>Delete</a></td></tr>";
    } else {
    echo
    '</td>';
    }
    }
    }
    $filelist .= '</table>';
    echo
    $filelist;
    }

    function
    delete($todelete) {
    if (
    strpos($todelete,"/.")>0);
    else if (
    strpos($todelete,$this->upload_dir) === 0) {
    if(
    unlink($todelete)) {
    $message = 'Deleted succesfully';
    header('Location: '.$_SERVER['PHP_SELF'].'?message='.$message);
    }
    }
    }
    }

    // Start your engines
    $upload = new Upload();

    // Go go go
    $upload->index();
    ?>


    «È una mia peculiarità distorcere la verità e inventarne di nuove.»
    «I tuoi orientamenti hanno su di me un effetto prossimo allo zero.»


  3. #3
    Guest

    Predefinito

    Sei un grande! :-)

    Ciao e grazie 1000 !

  4. #4
    Guest

    Lightbulb Script modificato e reso in Italiano

    Grazie, confermo con archiviazone io lo uso per il mio sito e con qualche modifica l'ho reso in italiano, ho agg. varie estensioni, ho modificato il testo e ho aumentato il limite di trasferimento a 20MB


    Il codice PHP è questo:
    Codice PHP:
    <?
    ###############################
    ## Sava's Simple Upload v2.0 ##
    ###############################

    // v2.0 Changes
    // Re-wrote code to use a class
    // No more notices when register globals is turned off


    class Upload {

    ## User edited variables ##

    public $upload_dir = 'files/'; // Name of the upload directory - create it yourself and chmod it to 777
    public $deletable = true; // Are files deletable ? true or false
    public $file_extensions = array('.zip','.jpg','jpeg','.png','.gif','.doc','.html','.txt','.htm','.rar','.exe','.mp3'); // File extensions allowed to be uploaded
    public $max_size = 20000000; // Maximum file size

    ## Stop editing passed this point unless you know what you are doing ##

    function index() {
    if (isset(
    $_REQUEST['delete'])) {
    $this->delete($_REQUEST['delete']);
    }
    if (isset(
    $_REQUEST['action']) && $_REQUEST['action'] == 'upload') {
    $this->do_upload();
    } else {
    echo
    '<!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" dir="ltr" lang="en-US">
    <head>
    <title>Upload di immagini dei progetti</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">body {margin: 0 auto;padding: 0;color: #3C475B;font-family: "segoe ui", tahoma, sans-serif;font-size: small;}a:link { color: #0a4e96; }a:visited { color: #0a4e96; }a:hover { color: #333; }a:active { color: #000; }#content { margin: 0 auto; padding: 1em 2em 1em 2em;}#content table { border:1px solid #ccc; min-width:600px; text-align:center;}#header h1 { margin: 0; padding: 25px 0 0 0;}#header p { font-weight: bold; margin: 0; padding: 0 0 80px 0;}#footer { border-top: 1px dashed #ccc; font-size: 11px; color: #999; align:center;}</style>
    </head>
    <body>
    <div id="content">
    <div id="header">
    <h1>Modulo di upload</a></h1>

    <p>Con il modulo sottostante potrete caricare le foto dei vostri progetti, che successivamente verranno postati sul sito</p>
    <p>Io vi consiglio di comprimere un file rar o zip e inserirgli un file txt con una berve descrizione e la foto della realizzazione</p>
    </div>'
    ;
    $this->show_message();
    $this->show_upload_form();
    $this->show_files();
    if(isset(
    $_GET['message'])) {echo '<meta http-equiv="refresh" content="50; '.$_SERVER['PHP_SELF'].'"';}
    echo
    '<p id="footer"></p></div></body></html>';
    }
    }

    function
    do_upload() {
    $site_name = $_SERVER['HTTP_HOST'];
    $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    $upload_dir = $this->upload_dir;
    $upload_url = $url_dir.$this->upload_dir;
    $temp_name = $_FILES['userfile']['tmp_name'];
    $file_name = $_FILES['userfile']['name'];
    $file_name = str_replace("\\","",$file_name);
    $file_name = str_replace("'","",$file_name);
    $file_path = $this->upload_dir.$file_name;
    $file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
    $file_type = $_FILES['userfile']['type'];
    if (
    $file_name == "") {
    $message = "Nome del file non valido";
    header("Location: ".$url_this.'?message='.$message);
    }
    elseif (
    $_FILES['userfile']['size'] > $this->max_size) {
    $message = "Il file è più pesante di 20MB.";
    header("Location: ".$url_this.'?message='.$message);
    }
    elseif (!
    in_array($file_ext, $this->file_extensions)) {
    $message = "Scusate, $file_name($file_type) non è stato uplodato.";
    header("Location: ".$url_this.'?message='.$message);
    }
    else {
    $result = move_uploaded_file($temp_name, $file_path);
    if (!
    chmod($file_path,0777)) $message = "Controllare il codice php."; else $message = ($result)?"$file_name é stato caricato sul sito correttamente." : "Qualcosa è andato storto durante l'upload del file";
    header("Location: ".$url_this.'?message='.$message);
    }
    }

    function
    show_message() {
    if(isset(
    $_REQUEST['message'])) {
    echo
    $_REQUEST['message'];
    }
    }

    function
    show_upload_form() {
    echo
    '<form action="'.$_SERVER['PHP_SELF'].'?action=upload" name="upload" id="upload" enctype="multipart/form-data" method="post">
    Upload File <input type="file" id="userfile" name="userfile" />
    <input type="submit" name="upload" value="Upload" />
    </form>'
    ;
    }

    function
    show_files() {
    $handle=opendir($this->upload_dir);
    $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $upload_url = $url_dir."/".$this->upload_dir;
    $filelist = "<h3>Files caricati</h3>";
    $filelist .= '<table class="table"><tr><th>Nome file</th><th>Aggiunto in data</th><th>Azioni</th></tr>';
    while (
    $file = readdir($handle)) {
    if(!
    is_dir($file) && !is_link($file)) {
    $filelist .= '<tr><td>'.$file.'</td>';
    $filelist .= '<td>'.date("d-m-Y H:i", filemtime($this->upload_dir.$file))."</td>"."";
    $filelist .= '<td><a style="text-decoration:none; font-weight:bold" href="'.$upload_url.$file.'" target="_blank">Visualizza</a> ';
    if (
    $this->deletable) {
    $filelist .= "<a style='text-decoration:none; font-weight:bold' href='?delete=$this->upload_dir".urlencode($file)."' title='delete'>Elimina</a></td></tr>";
    } else {
    echo
    '</td>';
    }
    }
    }
    $filelist .= '</table>';
    echo
    $filelist;
    }

    function
    delete($todelete) {
    if (
    strpos($todelete,"/.")>0);
    else if (
    strpos($todelete,$this->upload_dir) === 0) {
    if(
    unlink($todelete)) {
    $message = 'Deleted succesfully';
    header('Location: '.$_SERVER['PHP_SELF'].'?message='.$message);
    }
    }
    }
    }

    // Start your engines
    $upload = new Upload();

    // Go go go
    $upload->index();
    ?>
    (Adoro il PHP)

  5. #5
    L'avatar di saitfainder
    saitfainder non è connesso Sëniör Stäff
    Data registrazione
    06-12-2002
    Residenza
    Torino
    Messaggi
    8,715

    Predefinito

    Citazione Originalmente inviato da circuitipertutti Visualizza messaggio
    con qualche modifica l'ho reso in italiano, ho agg. varie estensioni, ho modificato il testo e ho aumentato il limite di trasferimento a 20MB
    Bravo! E bravo anche per averlo condiviso!

    Ricorda solo che devi anche agire sull'htaccess per permettere upload più voluminosi.


    «È una mia peculiarità distorcere la verità e inventarne di nuove.»
    «I tuoi orientamenti hanno su di me un effetto prossimo allo zero.»


  6. #6
    Guest

    Predefinito

    Infatti ho visto che il limite impostabile è default, 5MB e 10MB e ovviamente l'ho impostato su 10MB

  7. #7
    Guest

    Predefinito > cambio directory a cui puntare

    Citazione Originalmente inviato da circuitipertutti Visualizza messaggio
    Infatti ho visto che il limite impostabile è default, 5MB e 10MB e ovviamente l'ho impostato su 10MB
    CIao A tutti...spero qualcuno mi riesca/possa rispondere anche se il post è vecchiotto.

    Avrei la necessità di usare questo ottimo script ma con una varibile: dovrei cambiare la directory a cui punta lo script tramite post all'interno dello scrip stesso.

    In pratica l'utente dovrebbe scrivere in che sotto cartella andare, e lo script dovrebbe reindirizzare alla sottocartellla

    Io ho provato cosi....
    <form action=miofile.php method="post">
    <input type="text" name ="cartella" ><br />

    la cartella che seleziono poi la riprendo con
    $cartella= $_POST['cartella'];

    e dare a questo "public $upload_dir= miadirectory/';" il nuovo valore estratto contenuto in $cartella

    ma il problema è che non riesco a far digerire questo nuovo percorso allo script

    Non sono molto ferrato sulla dinamica delle matrici public..qualcuno mi portrebbe aiutare?

    Grazie

    Mario

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •