• Home
  • Forum
  • Fare Web
  • PHP

upload immagini problema

  • Creatore Discussione Creatore Discussione alexm66
  • Data di inizio Data di inizio 7 Giu 2011
  • Tag Tag
    unlink()
A

alexm66

Nuovo Utente
7 Giu 2011
3
0
0
  • 7 Giu 2011
  • #1
salve a tutti, premetto che di php ne capisco ben poco... e quindi chiedo un aiutino . ho scaricato un form per uplodare immagini direttamente sul server, funziona perfettamente.... però cè solo una cosa che vorrei facesse... ora quando carico l'immagine gli da un nome a random con dei numeri, mentre io vorrei che rimanesse il nome originale dell'immagine che vado a caricare, e se esiste gia la sovvrascrivesse. Potete aiutarmi? questo è il codice:
PHP:
<?php
//load the config file
include("configup.php");

//if the for has submittedd
if (isset($_POST['upForm'])){

       $file_type = $_FILES['imgfile']['type'];
       $file_name = $_FILES['imgfile']['name'];
       $file_size = $_FILES['imgfile']['size'];
       $file_tmp = $_FILES['imgfile']['tmp_name'];

       //check if you have selected a file.
       if(!is_uploaded_file($file_tmp)){
          echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit(); //exit the script and don't do anything else.
       }
       //check file extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit();
       }
       //get the file extension.
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

       //create a random file name
       $rand_name = md5(time());
      $rand_name= rand(0,999999999);
       //get the new width variable.
      $ThumbWidth = $img_thumb_width;

       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list width and height and keep height ratio.
           list($width, $height) = getimagesize($file_tmp);
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //save image
           ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
           //print message
           echo "<br>Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>";
        }

        //upload the big image
        move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

        echo "<br>Image Big: <a href=\"$path_big/$rand_name.$file_ext\">$path_big/$rand_name.$file_ext</a>";

        echo "<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

}else{ //if the form hasn't been submitted.

      //print the form
      echo "<script>
      function view_img(img_name){
         document[img_name].src = upForm.imgfile.value;
            document[img_name].width = 150;
      }
      </script>\n\n
      <br><h3>:: Browse an Image to Upload:</h3>\n
      <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">\n
      <input type=\"file\" name=\"imgfile\" onchange=\"javascript:view_img('img_vv');\"> <img src='' name='img_vv' width='0'><br>\n
      Image width will resize to <b>$img_thumb_width</b> with height ratio.
      <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n
      </form>
      <a href=\"view_gallery.php\">View Images</a>";

}




?>
 
Ultima modifica di un moderatore: 7 Giu 2011

Eliox

Utente Attivo
25 Feb 2005
4.390
3
0
  • 8 Giu 2011
  • #2
elimina la parte:
PHP:
$rand_name = md5(time());
$rand_name= rand(0,999999999);

poi sostituisci nel resto del codice $rand_name con $getExt[0] che è poi il nome del file senza estensione.
Non puoi sovrascrivere un file, ma puoi prima eliminare l'originale con unlink() e poi eseguire l'upload.
 
A

alexm66

Nuovo Utente
7 Giu 2011
3
0
0
  • 8 Giu 2011
  • #3
Ciao Claudio, ti ringrazio x la risposta funziona perfettamente..... cè solo un piccolo particolare...... come mai mi traduce il nome???? mi spiego, ho caricato l'immagine che si chiama pinguini. jpg, sono andato a guardare e me l'ha chiamata penguins.jpg
 
A

alexm66

Nuovo Utente
7 Giu 2011
3
0
0
  • 8 Giu 2011
  • #4
retifico... anche se mi sembra strano... boh.... me lo fa solo con le immagini campione di win.... ho caricato delle mie immagini ed è tutto a posto, mantiene il nome che gli do . Quindi ti ringrazio ancora, ciaoooooo
 
Devi accedere o registrarti per poter rispondere.

Discussioni simili

N
[PHP] Problema upload immagini wordpress
  • nicola93.rinaldi
  • 23 Giu 2017
  • PHP
Risposte
2
Visite
2K
PHP 25 Giu 2017
nicola93.rinaldi
N
Problema upload immagini
  • Rikk73
  • 7 Apr 2015
  • PHP
Risposte
4
Visite
2K
PHP 8 Apr 2015
Rikk73
F
Problema con upload di immagini che superano 2MB
  • Fabio Grasso
  • 23 Nov 2014
  • PHP
Risposte
14
Visite
3K
PHP 2 Dic 2014
Fabio Grasso
F
A
Problema con Upload immagini
  • aledero75
  • 3 Ott 2014
  • PHP
Risposte
9
Visite
2K
PHP 13 Nov 2014
aledero75
A
A
problema con upload immagini multiple
  • aganju
  • 5 Feb 2013
  • PHP
Risposte
0
Visite
1K
PHP 5 Feb 2013
aganju
A
N
problema script php mysql multi upload immagini
  • nobilebarabba
  • 10 Ott 2012
  • PHP
  • 2
Risposte
31
Visite
5K
PHP 15 Ott 2012
borgo italia
G
Problema con upload immagini e IE7
  • gianluca72
  • 31 Gen 2010
  • PHP
Risposte
6
Visite
2K
PHP 5 Feb 2010
Eliox
N
Problema upload immagini! HELP!
  • nikit0
  • 7 Mar 2008
  • PHP
Risposte
0
Visite
1K
PHP 7 Mar 2008
nikit0
N
C
problema upload immagini grandi con 4images
  • cirrito.f
  • 27 Ago 2007
  • Programmazione
Risposte
0
Visite
2K
Programmazione 27 Ago 2007
cirrito.f
C
P
Script upload immagini
  • pippuccio76
  • 9 Feb 2021
  • jQuery
Risposte
0
Visite
3K
jQuery 9 Feb 2021
pippuccio76
P
[PHP] Uploadify: upload immagini
  • felino
  • 24 Lug 2018
  • PHP
Risposte
0
Visite
1K
PHP 24 Lug 2018
felino
[PHP] Upload e successiva visualizzazione immagini in DB
  • MarcoGrazia
  • 28 Nov 2016
  • PHP
Risposte
3
Visite
2K
PHP 28 Nov 2016
MarcoGrazia
S
PHP: Aiuto con upload immagini che si auto tuotano
  • sebastianoweb
  • 21 Lug 2016
  • PHP
  • 2
Risposte
24
Visite
7K
PHP 28 Lug 2016
sebastianoweb
S
A
Upload multiplo di immagini in PHP
  • alemian95
  • 29 Mar 2016
  • PHP
Risposte
3
Visite
2K
PHP 30 Mar 2016
criric
C
Upload immagini con classe non funzionante
  • controsenso
  • 21 Gen 2016
  • PHP
Risposte
1
Visite
1K
PHP 29 Gen 2016
davide.arduini
P
Upload immagini
  • pippuccio76
  • 31 Mar 2015
  • Javascript
Risposte
0
Visite
1K
Javascript 31 Mar 2015
pippuccio76
P
P
upload immagini tramite ajax
  • processore
  • 2 Ott 2014
  • Ajax
Risposte
7
Visite
3K
Ajax 22 Ott 2014
filomeni
P
upload immagini con antepria
  • processore
  • 3 Giu 2014
  • jQuery
Risposte
1
Visite
1K
jQuery 3 Giu 2014
criric
H
Estrarre il nome delle immagini dall'upload
  • Hackx
  • 27 Mar 2014
  • PHP
Risposte
10
Visite
3K
PHP 28 Mar 2014
Hackx
H
R
upload immagini multiplo e ridimensionamento
  • rober23
  • 14 Gen 2014
  • PHP
Risposte
0
Visite
1K
PHP 14 Gen 2014
rober23
R
Condividi:
Facebook X (Twitter) LinkedIn WhatsApp e-mail Condividi Link
  • Home
  • Forum
  • Fare Web
  • PHP
  • Italiano
  • Termini e condizioni d'uso del sito
  • Policy Privacy
  • Aiuto
  • Home
Community platform by XenForo® © 2010-2024 XenForo Ltd. | Traduzione a cura di XenForo Italia
Menu
Accedi

Registrati

  • Home
  • Forum
    • Nuovi Messaggi
    • Cerca...
  • Novità
    • Featured content
    • Nuovi Messaggi
    • Ultime Attività
X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?

X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?