Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11

Discussione: Sistema di Upload

  1. #1

    Sistema di Upload

    Ciao ragazzi, Volevo creare un sistema di upload di immagini proprio come tinypic.com, magari meno complesso. Il problema è uno, ho cercato di farmelo e cercarlo sul web, ma non sono riuscito.. avete dei suggerimenti? delle guide? oppure potete darmi voi dei codici con cui partire? Grazie.

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2010
    Messaggi
    272
    Prova questo

    e dimmi se funziona!

  3. #3
    Il sistema di upload va bene xD ho creato la cartella public ma non si vede l'immagine..

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2010
    Messaggi
    272
    posta qualche codice!

  5. #5
    codice:
    <? $dimensione_massima=51200; //dimensione massima consentita per file in byte -> 1024 byte = 1 Kb $dimensione_massima_Kb=$dimensione_massima/1024; $cartella_upload="public/"; //cartella in cui eseguire l'upload (controllare permessi scrittura) // percorso cartella relativo $cartella_upload="../public/"; $filtrare=1; //filtrare x estensioni ammesse? 1=si 0=no $array_estensioni_ammesse=array('.jpg','.jpeg','.gif','.png'); //estensioni ammesse if(!isset($_FILES['file1']) || $_FILES['file1']['size']==0){ echo "Nessun file selezionato per l'upload"; }elseif($_FILES['file1']['size']>$dimensione_massima){ echo "Il file selezionato per l'upload supera dimensione massima di $dimensione_massima_Kb Kb"; }else{ $nome_file=$_FILES['file1']['name']; $errore=""; if($filtrare==1){ $estensione = strtolower(substr($nome_file, strrpos($nome_file, "."), strlen($nome_file)-strrpos($nome_file, "."))); if(!in_array($estensione,$array_estensioni_ammesse)){ $errore.="Upload file non ammesso. Estensioni ammesse: ".implode(", ",$array_estensioni_ammesse)."
    "; } } if(!file_exists($cartella_upload)){ $errore.="La cartella di destinazione non esiste</br>"; } if($errore==""){ if(move_uploaded_file($_FILES['file1']['tmp_name'], $cartella_upload.$_FILES['file1']['name'])){ chmod($cartella_upload.$_FILES['file1']['name'],0777); //permessi per poterci sovrascrivere/scaricare echo "Operazione eseguita con successo. Upload riuscito."; }else{ echo "Impossibile effettuare l'upload del file"; } }else{ echo $errore; } } ?>

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2010
    Messaggi
    272
    Originariamente inviato da Igna49
    code
    oddio sistema questo codice ... non metterno nel prompt che ti esce .... scrivi tu
    Codice PHP:
    codice 

  7. #7
    <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
    <?php
    // upload the file
    if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {

    // file needs to be jpg,gif,bmp,x-png and 4 MB max
    if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
    {


    // some settings
    $max_upload_width = 2592;
    $max_upload_height = 1944;

    // if user chosed properly then scale down the image according to user preferances
    if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
    $max_upload_width = $_REQUEST['max_width_box'];
    }
    if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
    $max_upload_height = $_REQUEST['max_height_box'];
    }


    // if uploaded image was JPG/JPEG
    if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
    $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
    }
    // if uploaded image was GIF
    if($_FILES["image_upload_box"]["type"] == "image/gif"){
    $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
    }
    // if uploaded image was PNG
    if($_FILES["image_upload_box"]["type"] == "image/png"){
    $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
    }
    // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
    // if uploaded image was BMP
    if($_FILES["image_upload_box"]["type"] == "image/bmp"){
    $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
    }


    $remote_file = "image_files/".$_FILES["image_upload_box"]["name"];
    imagejpeg($image_source,$remote_file,100);
    chmod($remote_file,0644);



    // get width and height of original image
    list($image_width, $image_height) = getimagesize($remote_file);

    if($image_width>$max_upload_width || $image_height >$max_upload_height){
    $proportions = $image_width/$image_height;

    if($image_width>$image_height){
    $new_width = $max_upload_width;
    $new_height = round($max_upload_width/$proportions);
    }
    else{
    $new_height = $max_upload_height;
    $new_width = round($max_upload_height*$proportions);
    }


    $new_image = imagecreatetruecolor($new_width , $new_height);
    $image_source = imagecreatefromjpeg($remote_file);

    imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
    imagejpeg($new_image,$remote_file,100);

    imagedestroy($new_image);
    }

    imagedestroy($image_source);


    header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=". $_FILES["image_upload_box"]["name"]);
    exit;
    }
    else{
    header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
    exit;
    }
    }
    ?>

    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Image Upload with resize</title>
    <style type="text/css">
    <!--
    body,td,th {
    font-family: Arial, Helvetica, sans-serif;
    color: #333333;
    font-size: 12px;
    }

    .upload_message_success {
    padding:4px;
    background-color:#009900;
    border:1px solid #006600;
    color:#FFFFFF;
    margin-top:10px;
    margin-bottom:10px;
    }

    .upload_message_error {
    padding:4px;
    background-color:#CE0000;
    border:1px solid #990000;
    color:#FFFFFF;
    margin-top:10px;
    margin-bottom:10px;
    }

    -->
    </style></head>

    <body>

    <h1 style="margin-bottom: 0px">Submit an image</h1>


    <?php if(isset($_REQUEST['upload_message'])){?>
    <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
    <?php echo htmlentities($_REQUEST['upload_message']);?>
    </div>
    <?php }?>


    <form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
    <label>Image file, maximum 4MB. it can be jpg, gif, png:</label>

    <input name="image_upload_box" type="file" id="image_upload_box" size="40" />
    <input type="submit" name="submit" value="Upload image" />







    <label>Scale down image? (2592 x 1944 px max):</label>


    <input name="max_width_box" type="text" id="max_width_box" value="1024" size="4">
    x

    <input name="max_height_box" type="text" id="max_height_box" value="768" size="4">
    px.




    <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">
    Notes:

    The image will not be resized to this exact size; it will be scalled down so that neider width or height is larger than specified.

    When uploading this script make sure you have a directory called &quot;image_files&quot; next to it and make that directory writable, permissions 777.

    After you uploaded images and made tests on our server please delete all uploaded images

    </p>



    <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
    </form>




    <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>



    [img]image_files/<?php echo $_REQUEST['show_image'];?>[/img]
    </p>
    <?php }?>




    </body>
    </html>

  8. #8
    <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
    <?php
    // upload the file
    if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {

    // file needs to be jpg,gif,bmp,x-png and 4 MB max
    if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
    {


    // some settings
    $max_upload_width = 2592;
    $max_upload_height = 1944;

    // if user chosed properly then scale down the image according to user preferances
    if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
    $max_upload_width = $_REQUEST['max_width_box'];
    }
    if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
    $max_upload_height = $_REQUEST['max_height_box'];
    }


    // if uploaded image was JPG/JPEG
    if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
    $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
    }
    // if uploaded image was GIF
    if($_FILES["image_upload_box"]["type"] == "image/gif"){
    $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
    }
    // if uploaded image was PNG
    if($_FILES["image_upload_box"]["type"] == "image/png"){
    $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
    }
    // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
    // if uploaded image was BMP
    if($_FILES["image_upload_box"]["type"] == "image/bmp"){
    $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
    }


    $remote_file = "image_files/".$_FILES["image_upload_box"]["name"];
    imagejpeg($image_source,$remote_file,100);
    chmod($remote_file,0644);



    // get width and height of original image
    list($image_width, $image_height) = getimagesize($remote_file);

    if($image_width>$max_upload_width || $image_height >$max_upload_height){
    $proportions = $image_width/$image_height;

    if($image_width>$image_height){
    $new_width = $max_upload_width;
    $new_height = round($max_upload_width/$proportions);
    }
    else{
    $new_height = $max_upload_height;
    $new_width = round($max_upload_height*$proportions);
    }


    $new_image = imagecreatetruecolor($new_width , $new_height);
    $image_source = imagecreatefromjpeg($remote_file);

    imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
    imagejpeg($new_image,$remote_file,100);

    imagedestroy($new_image);
    }

    imagedestroy($image_source);


    header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=". $_FILES["image_upload_box"]["name"]);
    exit;
    }
    else{
    header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
    exit;
    }
    }
    ?>

    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Xat - Image - Upload</title>
    <style type="text/css">
    <!--
    body,td,th {
    font-family: Arial, Helvetica, sans-serif;
    color: #333333;
    font-size: 12px;
    }

    .upload_message_success {
    padding:4px;
    background-color:#009900;
    border:1px solid #006600;
    color:#FFFFFF;
    margin-top:10px;
    margin-bottom:10px;
    }

    .upload_message_error {
    padding:4px;
    background-color:#CE0000;
    border:1px solid #990000;
    color:#FFFFFF;
    margin-top:10px;
    margin-bottom:10px;
    }

    .style1 {
    font-size: medium;
    }

    -->
    </style></head>

    <body>

    <h1 style="margin-bottom: 0px">Xat - Image - Upload</h1>


    <?php if(isset($_REQUEST['upload_message'])){?>
    <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
    <?php echo htmlentities($_REQUEST['upload_message']);?>
    </div>
    <?php }?>


    <form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
    <label>Image file, maximum 4MB. it can be jpg, gif, png:</label>

    <input name="image_upload_box" type="file" id="image_upload_box" size="40" />




    <input type="submit" name="submit" value="Upload image" />







    .







    <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
    </form>




    <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>
    <p class="style1">
    Immagine Caricata:</p>



    [img]image_files/<?php echo $_REQUEST['show_image'];?>[/img]
    </p>
    <p class="style1">
    Link Diretto Dell&#39;Immagine:</p>
    <textarea name="codice gioco" style="height: 33px; width: 332px">
    http://image_files/<?php echo $_REQUEST['show_image'];?></textarea>



    <span class="style1">Codice HTML Per integrare l&#39;Immagine nel tuo Sito,
    Blog o Forum:

    </span>


    <textarea name="codice gioco" style="height: 71px; width: 267px">
    <img src="http:///image_files/<?php echo $_REQUEST['show_image'];?></textarea>
    <?php }?>




    </body>
    </html>

    Ecco guarda questo codice che ho trovato sul web, funziona benissimo, però non mi fa caricare le immagini in png, secondo voi il problema qual'è?

  9. #9
    Niente ragazzi, sono riuscito, ma vi devo esporre un ulteriore problema, in poche parole, ho notato che la gente caricando le immagini, può caricare delle immagini con lo stesso nome di altre, così facendo le immagini precedenti si cancellerebbero per dare spazio alle nuove, avevo pensato, si potrebbe fare o si mettono dei messaggi di errore se succede una cosa del genere, oppure si mette un codice php che ti fa cambiare direttamente i nomi dei file quando si caricano.. avete delle fonti per risolvere il mio problema? Grazie

  10. #10

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.