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

    [script upload immagini] se il file esiste->rinomina

    Vorrei che quando uppo un file che già è presente nel server con lo stesso nome, invece di bloccarsi dicendo che già esiste un file con lo stesso nome, rinomini il nuovo file aggiungendo prima dell'estensione un codice casuale, e così se provo ancora una terza volta a riuploadare un file con lo stesso nome la possibilità che crei lo stesso codice sono quasi nulle...
    Codice PHP:
    <?
    //user defined variables
    $abpath "/home/web/public_html/upload"//Absolute path to where images are uploaded. No trailing slash
    $sizelim "yes"//Do you want size limit, yes or no
    $size "50000000"//What do you want size limited to be if there is one

    //all image types to upload
    $cert1 "image/pjpeg"//Jpeg type 1
    $cert2 "image/jpeg"//Jpeg type 2

    $log "";

    //begin upload 1

    //checks if file exists
    if ($img1_name == "") {
    $log .= "No file selected for upload 1
    "
    ;
    }
    if (
    $img1_name != "") {
    //checks if file exists
    if (file_exists("$abpath/$img1_name")) {
    $log .= "File 1 already existed
    "
    ;
    } else {

    //checks if files to big
    if ($sizelim == "yes") {
    if (
    $img1_size $size) {
    $log .= "File 1 was too big
    "
    ;
    }
    }

    //Checks if file is an image
    if (($img1_type == $cert1) or ($img1_type == $cert2) or ($img1_type == $cert3) or ($img1_type == $cert4) or ($img1_type == $cert5) or ($img1_type == $cert6) or ($img1_type == $cert7) or ($img1_type == $cert8) or ($img1_type == $cert9) or ($img1_type == $cert10) or ($img1_type == $cert11) or ($img1_type == $cert12) or ($img1_type == $cert13) or ($img1_type == $cert14) or ($img1_type == $cert15) or ($img1_type == $cert16) or ($img1_type == $cert17)) {
    @
    copy($img1"$abpath/$img1_name") or $log .= "Couldn't copy image 1 to server
    "
    ;
    if (
    file_exists("$abpath/$img1_name")) {
    $log .= "File 1 $img1_name was uploaded
    "
    ;
    }
    } else {
    $log .= "File 1 is not an image
    "
    ;
    }
    }
    }
    //checks if file exists
    if ($img2_name == "") {
    $log .= "No file selected for upload 2
    "
    ;
    }
    if (
    $img2_name != "") {

    //checks if file exists
    if (file_exists("$abpath/$img2_name")) {
    $log .= "File 2 already existed
    "
    ;
    } else {

    //checks if files to big
    if ($sizelim == "yes") {
    if (
    $img2_size $size) {
    $log .= "File 2 was too big]
    "
    ;
    }
    }

    //Checks if file is an image
    if (($img2_type == $cert1) or ($img2_type == $cert2) or ($img2_type == $cert3) or ($img2_type == $cert4) or ($img2_type == $cert5) or ($img2_type == $cert6) or ($img2_type == $cert7) or ($img2_type == $cert8) or ($img2_type == $cert9) or ($img2_type == $cert10) or ($img2_type == $cert11) or ($img2_type == $cert12) or ($img2_type == $cert13) or ($img2_type == $cert14) or ($img2_type == $cert15) or ($img2_type == $cert16) or ($img2_type == $cert17)) {
    @
    copy($img2"$abpath/$img2_name") or $log .= "Couldn't copy image 2 to server
    "
    ;
    if (
    file_exists("$abpath/$img2_name")) {
    $log .= "File 2 was uploaded
    "
    ;
    }
    } else {
    $log .= "File 2 is not an image
    "
    ;
    }
    }
    }
    if (
    $img3_name == "") {
    $log .= "No file selected for upload 3
    "
    ;
    }
    if (
    $img3_name != "") {

    //checks if file exists
    if (file_exists("$abpath/$img3_name")) {
    $log .= "File 3 already existed
    "
    ;
    } else {

    //checks if files to big
    if ($sizelim == "yes") {
    if (
    $img3_size $size) {
    $log .= "File 3 was too big
    "
    ;
    }
    }

    //Checks if file is an image
    if (($img3_type == $cert1) or ($img3_type == $cert2) or ($img3_type == $cert3) or ($img3_type == $cert4) or ($img3_type == $cert5) or ($img3_type == $cert6) or ($img3_type == $cert7) or ($img3_type == $cert8) or ($img3_type == $cert9) or ($img3_type == $cert10) or ($img3_type == $cert11) or ($img3_type == $cert12) or ($img3_type == $cert13) or ($img3_type == $cert14) or ($img3_type == $cert15) or ($img3_type == $cert16) or ($img3_type == $cert17)) {
    @
    copy($img3"$abpath/$img3_name") or $log .= "Couldn't copy image 3 to server
    "
    ;
    if (
    file_exists("$abpath/$img3_name")) {
    $log .= "File 3 was uploaded
    "
    ;
    }
    } else {
    $log .= "File 3 is not an image
    "
    ;
    }
    }
    }
    if (
    $img4_name == "") {
    $log .= "No file selected for upload 4
    "
    ;
    }
    if (
    $img4_name != "") {

    //checks if file exists
    if (file_exists("$abpath/$img4_name")) {
    $log .= "File 4 already existed
    "
    ;
    } else {

    //checks if files to big
    if ($sizelim == "yes") {
    if (
    $img4_size $size) {
    $log .= "File 4 was too big
    "
    ;
    }
    }

    //Checks if file is an image
    if (($img4_type == $cert1) or ($img4_type == $cert2) or ($img4_type == $cert3) or ($img4_type == $cert4) or ($img4_type == $cert5) or ($img4_type == $cert6) or ($img4_type == $cert7) or ($img4_type == $cert8) or ($img4_type == $cert9) or ($img4_type == $cert10) or ($img4_type == $cert11) or ($img4_type == $cert12) or ($img4_type == $cert13) or ($img4_type == $cert14) or ($img4_type == $cert15) or ($img4_type == $cert16) or ($img4_type == $cert17)) {
    @
    copy($img4"$abpath/$img4_name") or $log .= "Couldn't copy image 4 to server
    "
    ;
    if (
    file_exists("$abpath/$img4_name")) {
    $log .= "File 4 was uploaded
    "
    ;
    }
    } else {
    $log .= "File 4 is not an image
    "
    ;
    }
    }
    }

    if (
    $img5_name == "") {
    $log .= "No file selected for upload 5
    "
    ;
    }
    if (
    $img5_name != "") {
    //checks if file exists
    if (file_exists("$abpath/$img5_name")) {
    $log .= "File 5 already existed
    "
    ;
    } else {

    //checks if files to big
    if ($sizelim == "yes") {
    if (
    $img5_size $size) {
    $log .= "File 5 was too big
    "
    ;
    }
    }

    //Checks if file is an image
    if (($img5_type == $cert1) or ($img5_type == $cert2) or ($img5_type == $cert3) or ($img5_type == $cert4) or ($img5_type == $cert5) or ($img5_type == $cert6) or ($img5_type == $cert7) or ($img5_type == $cert8) or ($img5_type == $cert9) or ($img5_type == $cert10) or ($img5_type == $cert11) or ($img5_type == $cert12) or ($img5_type == $cert13) or ($img5_type == $cert14) or ($img5_type == $cert15) or ($img5_type == $cert16) or ($img5_type == $cert17)) {
    @
    copy($img5"$abpath/$img5_name") or $log .= "Couldn't copy image 5 to server
    "
    ;
    if (
    file_exists("$abpath/$img5_name")) {
    $log .= "File 5 was uploaded
    "
    ;
    }

    } else {
    $log .= "File 5 is not an image
    "
    ;
    }
    }
    }
    ?>
    <html>
    <head>


    Log:

    <?

    echo "$log";

    ?>
    Inoltre (l'ho fatto per il primo solamente) voglio che dica "file 1 [nome file] uploaded", però voglio che stampi il nuovo nome del file rinominato quando lo rinomina!
    Grazie in anticipo.

  2. #2
    prima di copiare il file caricato nella sua posizione definitiva fai un controllo con file_exist() passandogli il percorso+nome del file che stai per copiare, se il file esiste già allora prima di copiare generi il nuovo nome del file aggiungendo md5(microtime());

    Non ho guardato il codice postato perché troppo incasinato (molto codice ripetuto più volte), potresti certamente fare qualcosa di migliore...

  3. #3
    Spoon25 ha ragione!

    Io per esempio ho fatto così:

    Codice PHP:
    //tralascio un pò di codice..
    if(file_exists("upload/".$_FILES['file']['name'])) {
    copy($_FILES['file']['tmp_name'],"upload/"."$data".$_FILES['file']['name']);
    echo 
    "File : [url]http://link.org/upload/[/url]"."$data".$_FILES['file']['name']; 
    $data = date("His");

    PER CASO ti serve un guestbook, completamente personalizzabile?e PER CASO lo vuoi anche gratis e senza pop-up o dialer?E allora prova il nuovo NewAge GuestBook..Niente per caso, tutto perfetto

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.