Avatar of PredatorGR
PredatorGR

asked on 

How can i rename a file being uploaded to my server using PHP?

Hi, i have this script to upload images to specific directories in my server, no increments, just overwriting the same image every time someone uploads a jpg.

Question is, how it is possible to rename the file test.jpg to untest.jpg when i press the "Upload" button using PHP code?

It's a little bit dirty but it works like i want to, please don't mind the save paths as this scripts runs from a protected folder but i want to display the uploaded picture in public.

Thanks
<form action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm"> •¾ÎÆÅ»»¿:
        <input name="new_image" value="‘½±¶®Ä·Ã·" id="new_image" size="30" type="file" class="fileUpload" />
        <button name="submit" type="submit" class="submitButton"> Á¿Ã¸®º· •¾ÎÆÅ»»¿Å</button> <strong> ¡Ÿ£Ÿ§—: — ¿½¿¼±Ã¯± Ä¿Å ±Áǵ¯¿Å ÀÁ­Àµ¹ ½± µ¯½±¹ "test.jpg" ³¹± ½± »µ¹Ä¿ÅÁ³®Ãµ¹ ÃÉÃĬ</strong>
</form>
<?php
             if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/".$imagename;
              move_uploaded_file($source, $target);
              
              $imagepath = $imagename;
              $save = "../images/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
                                                         
              $modwidth = 703; 
                                                         
              $diff = $width / $modwidth;
                                                        
              $modheight = 928; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                                                        
              imagejpeg($tn, $save, 100) ; 
 
             
            echo "<img src='../images/".$imagepath."'><br>"; 
 
        
          }
        }
 
 
         $path_to_images = "../images/";
   echo '<img src="'.$path_to_images.'test.jpg" />';
   
 
 
?>

Open in new window

PHP

Avatar of undefined
Last Comment
hielo

8/22/2022 - Mon